Ansible apt module to execute with sudo



我正在尝试让 ansible 执行sudo apt-get update.我已经有了无需密码即可运行的sudoers设置,如果我以用户 ansible 正在使用并执行sudo apt-get update登录,它可以工作。如果我使用以下 ansible 剧本

---
- name: updates
hosts: pis
tasks:
- name: Update APT
become: true
apt:
update_cache: yes

它会给我以下错误

fatal: [127.0.0.1]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 127.0.0.1 closed.rn", "module_stdout": "sudo: a password is requiredrn", "msg": "MODULE FAILUREnSee stdout/stderr for the exact error", "rc": 1}

但以下剧本工作正常

---
- name: updates
hosts: pis
tasks:
- name: Update APT
command: sudo apt-get update

使用become: yes时,连接将尝试以 root 身份生成 shell(因为您尚未指定become_user(。

似乎您可以在没有密码的情况下运行sudo apt-get update,即:

/etc/sudodoers可能包含类似以下内容的内容:

ansible ALL=(ALL) NOPASSWD: /bin/apt-get update

要复制问题,请以用户ansible身份登录并发出命令sudo -i

如果您使用专用用户(ansible?(进行所有 ansible 连接,您可能希望通过 sudo 授予该用户访问所有命令的权限,而无需密码。阅读此处,但这不是最佳实践,出于安全原因,您最好授予对特定命令的访问权限

使用以下行添加到/etc/sudoers使用visudo /etc/sudoers

ansible ALL=(ALL) NOPASSWD:ALL

如果您选择这样做,我还建议您对此特权帐户使用密钥对身份验证,而不是密码身份验证。更多信息

最新更新