我遇到了 Ansible 服务模块由于 sudo 密码问题而失败的问题:
fatal: [192.168.1.10]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to 192.168.1.10 closed.\r\n", "module_stdout": "sudo: a password is required\r\n", "msg": "MODULE FAILURE", "rc": 1}
to retry, use: --limit @/Volumes/HD/Users/user/Ansible/playbooks/stop-homeassistant.retry
我的剧本只有一项任务,即停止服务。它看起来像:
---
- hosts: 192.168.1.10
tasks:
- name: Stop Homeassistant
become: true
service: name=home-assistant@homeassistant state=stopped enabled=yes
或者,对于 systemd:
systemd: state=stopped name=home-assistant@homeassistant enabled=yes
我正在像这样运行剧本:
ansible-playbook -u homeassistant playbooks/stop-homeassistant.yml
但是,在该盒子上为该用户设置了无密码 sudo(在/etc/sudoers.d 中):
homeassistant ALL=(ALL) NOPASSWD:/bin/systemctl restart home-assistant@homeassistant
homeassistant ALL=(ALL) NOPASSWD:/bin/systemctl stop home-assistant@homeassistant
如果我以 homeassistant 身份 ssh 进入该盒子,然后运行:
sudo systemctl stop home-assistant@homeassistant
home-assistant@homeassistant 服务将完全停止,无需询问 sudo 密码。
知道为什么 systemctl 命令可以作为用户在盒子上完美运行,但随后在 service/systemd 模块中失败吗?
最佳答案
尝试在目标计算机上配置无密码 sudo:
homeassistant ALL=NOPASSWD: ALL
在 /etc/sudoers
中使用 NOPASSWD
标志配置特定命令不适用于 Ansible。
关于ansible - 服务和 systemd 模块要求输入 sudo 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476097/