![](/img/trans.png)
[英]Change linux password with Ansible playbook script when connecting as a non-root user without sudo privileges
[英]Ansible: sudo without password
我想在没有 sudo 密码的情况下使用用户 sa1 运行 ansible:
第一次正常:
[root@centos1 cp]# ansible cent2 -m shell -a "sudo yum -y install httpd"
cent2 | SUCCESS | rc=0 >>
第二次失败:
[root@centos1 cp]# ansible cent2 -s -m yum -a "name=httpd state=absent"
cent2 | FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "",
"module_stdout": "sudo: a password is required\r\n",
"msg": "MODULE FAILURE",
"parsed": false
}
请帮忙!
这是不可靠的,这是您服务器的配置。 确保 sudo 允许 ansible 在没有密码的情况下使用。
sudo visudo
打开 sudoers 文件centos ALL=(ALL) NOPASSWD:ALL
centos
您可以通过运行从服务器本身尝试:
sudo -u [yourusername] sudo echo "success"
如果这有效,它也应该从 ansible 工作。
默认情况下,ansible 使用以下标志运行 sudo: -H -S -n
以成为 root。 其中--non-interactive
将是选项-n
的相应长格式。 此选项似乎使 sudo 返回错误消息,而不尝试让身份验证模块执行其操作。
我设法通过创建一个 ~/.ansible.cfg 包含以下行来解决密码错误,用于最相关的 ansible 版本。
稳定的 2.4
[defaults]
sudo_flags = --set-home --stdin
安全的 2.9
[sudo_become_plugin]
flags = -H -S
这至少足以让 pam_ssh_agent_auth.so 运行并验证我的身份。
在 2.8 版之前,上面的例子可以工作,比 2.8 新的需要第二个例子。 新样式配置的文档可以在Ansible 用户指南 中找到。
这是剧本,以防您想要 ansible 为您制作
wheel
)- name: Make users passwordless for sudo in group wheel
lineinfile:
path: /etc/sudoers
state: present
regexp: '^%wheel'
line: '%wheel ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.