由于sudo密码问题,我遇到Ansible服务模块失败的问题:
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
如果我把那个盒子作为家庭辅助,那么我跑:
sudo systemctl stop home-assistant@homeassistant
家庭助手@ homeassistant服务将在不要求sudo密码的情况下彻底停止。
知道为什么systemctl命令能够像盒子上的用户一样完美地运行,但是在service / systemd模块中失败了吗?
答案 0 :(得分:2)
尝试在目标计算机上配置无密码sudo:
homeassistant ALL=NOPASSWD: ALL
在NOPASSWD
中使用/etc/sudoers
标记配置特定命令不适用于Ansible。
答案 1 :(得分:2)
好的,请修改你的剧本如下:
hosts: 192.168.1.10
remote_user: home-assistant
become: true
become_method: sudo
become_user: root
tasks:
- name: Stop Homeassistant
become: true
service: name=home-assistant@homeassistant state=stopped enabled=yes
现在,
以ansible-playbook <playbook-name>
运行。
如果上述命令因密码而失败,请以
运行 ansible-playbook playbook.yml --user=<username> --extra-vars "ansible_sudo_pass=<yourPassword>"