我正在使用appuser
执行Ansible剧本,而我希望在本地主机上与用户webuser
创建文件夹。
ssh密钥是在我的本地主机上为webuser设置的。因此,使用appuser
登录后,我可以简单地通过ssh webuser@localhost
将用户切换为webuser
。
注意:我没有sudo特权,所以我无法sudo
从appuser切换到webuser。
下面是与用户appuser一起运行的我的剧本,但需要使用04May2020
在本地主机上创建文件夹webuser
- name: "Play 1"
hosts: localhost
remote_user: "webuser"
vars:
ansible_ssh_extra_args: -o StrictHostKeyChecking=no
ansible_ssh_private_key_file: /app/misc_automation/ssh_keys_id_rsa
tasks:
- name: create folder for today's print
file:
path: "/webWeb/htdocs/print/04May2020"
state: directory
remote_user: webuser
但是,输出显示文件夹是用appuser
而不是webuser
创建的。请参见显示与appuser
而非webuser
的ssh连接的输出。
ansible-playbook /app/Ansible/playbook/print_oracle/print.yml -i /app/Ansible/playbook/print_oracle/allhosts.hosts -vvv
TASK [create folder for today] ***********************************
task path: /app/Ansible/playbook/print_oracle/print.yml:33
Using module file /usr/lib/python2.7/site-packages/ansible/modules/files/file.py
Pipelining is enabled.
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: appuser
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
您能不能建议没有sudo
的情况吗?
您可以尝试的一种方法是将ansible_connection设置为localhost。为此,请在运行ansible命令的目录中创建host_vars目录。在该子目录中,创建一个名为localhost的文件,其中包含以下行:ansible_connection:smart