![](/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.