首页 > 解决方案 > 为什么我的 sudo 命令适用于 ansible 但不适用于 ansible-playbook?

问题描述

我正在尝试运行 Ansible 脚本来设置 Microsoft Azure VM,出于某种原因,我可以通过“ansible”运行 sudo 命令,但不能通过“ansible-playbook”运行它们。

例如,我希望以下命令在不指定 --become 和 --ask-become-pass 标志的情况下失败,并且确实如此:

[user@localhost AnsibleScripts]$ ansible -i azure_rm.yml test_hosts -a "touch /testFile" -u testuser

testVM_da13 | FAILED | rc=1 >>
touch: cannot touch ‘/testFile’: Permission deniednon-zero return code

但我可以通过添加“-b”和“-K”参数来使其工作:

[user@localhost AnsibleScripts]$ ansible -i azure_rm.yml test_hosts -a "touch /testFile" -u testuser -b -K
SUDO password:

testVM_da13 | CHANGED | rc=0 >>

现在,当我尝试运行在剧本中配置的相同命令时,我得到以下输出:

[user@localhost AnsibleScripts]$ ansible-playbook -i azure_rm.yml install_test.yml -u testuser -b -K
SUDO password:

PLAY [Install and configure test] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
fatal: [testVM_da13]: FAILED! => {"changed": false, "module_stderr": "Sorry, try again.\n[sudo via ansible, key=******************************] password: \nsudo: 1 incorrect password attempt\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

install_test.yml 的内容是:

---
- name: Install and configure test
  hosts: test_hosts
  connection: local

  tasks:
   - name: test
     shell: touch /testFile
...

有人对我如何让我的 sudo 命令通过 ansible-playbook 工作有任何建议吗?

感谢您花时间查看我的问题。

标签: azureansible

解决方案


connection: local意味着execute every command on the localhost

将其从您的剧本中删除,然后重试。


推荐阅读