linux - 无法使用 sudo 用户运行 Ansible

标签 linux shell ansible ansible-2.x

我们在服务器上有一个用户 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/

相关文章:

c - Linux 内核中 rt.c/deadline.c 中的空函数

linux -/bin/sh "embedded linux"while do done - 需要 sleep 命令 - CPU 较少

bash - wc 作为变量的结果

c++ - 一次打印从套接字接收到的一个字符

linux - Google Assistant 获取 SSL 错误

Bash 脚本不访问路径中包含空格的文件

linux - 在已经运行的 shell 中通过控制台运行 erlang shell 命令

ansible - 使用列表迭代哈希

linux - 如何使用 Bamboo 或 jenkins 运行 ansible playbook

security - 如何阻止 Ansible 中的 extra_vars 覆盖变量?