服务和systemd模块请求sudo密码

时间:2017-06-10 17:25:56

标签: ansible

由于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模块中失败了吗?

2 个答案:

答案 0 :(得分:2)

尝试在目标计算机上配置无密码sudo:

homeassistant ALL=NOPASSWD: ALL

NOPASSWD中使用/etc/sudoers标记配置特定命令不适用于Ansible。

详细信息:https://github.com/ansible/ansible/issues/5712

答案 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>"