为什么我的sudo命令可以与ansible一起使用,而不能与ansible-playbook一起使用?

时间:2019-02-19 01:56:46

标签: azure ansible

我正在尝试运行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
...

有人对我如何通过ansible-playbook运行sudo命令有任何建议吗?

感谢您抽出宝贵的时间来浏览我的问题。

1 个答案:

答案 0 :(得分:1)

connection: local的意思是execute every command on the localhost

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