我们在服务器上有一个用户 userA
可以访问 sudo
。我可以登录到服务器并运行 sudo su - userA
以切换到新用户。但是,如果我使用 Ansible,它会抛出以下错误:
fatal: [node1]: FAILED! => {"changed": false, "module_stderr": "Shared connection to node1 closed.\r\n", "module_stdout": "\r\nSorry, user abc is not allowed to execute '/bin/sh -c echo BECOME-SUCCESS-pzwmlpcvzvwafleunmvpwioi; /usr/bin/python /var/tmp/ansible-tmp-1533926060.36-184244176073120/setup.py' as userA on node1.\r\n", "msg": "MODULE FAILURE", "rc": 1}
Ansible 文件:
---
- hosts: all
become: yes
become_user: userA
become_method: sudo
tasks:
- name: Create file
command: touch /home/userA/testing
我们无权访问 sudoers 文件。有没有办法在不更改 sudoers 文件的情况下解决这个问题?
最佳答案
我取决于...如果 sudoers 配置允许运行 /usr/bin/su - userA*
(在末尾使用通配符允许 -c
参数), 然后你可以通过以下方式将 become-configuration 添加到你的任务中:
- name: Create file
command: touch /home/userA/testing
vars:
ansible_become: true
ansible_become_method: su
ansible_become_user: userA
ansible_become_exe: 'sudo -p "Password: " su -'
如果 sudoers 需要密码,则必须使用 --ask-become-pass
(-K
) 运行 ansible-playbook
选项和连接用户的密码(对于 sudo
)。
前三个参数可以直接写成task/play的参数
否则你可能注定要运行 expect
模块(可能在 Controller 机器上),但会带来所有缺点。
关于linux - 无法使用 sudo 用户运行 Ansible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796405/