我正在以
的形式运行简单的剧本---
- hosts: "{{ host }}"
remote_user: "{{ ansible_user }}"
tasks:
# - name: Move Network for peer
# copy:
# src: ./../network/
# dest: /home/{{ user }}/network/
- name: Set JAVA_HOME
blockinfile:
path: /etc/environment
backup: yes
state: present
block: |
Match User ansible-agent
PasswordAuthentication 4
become: yes
因此,在使用comand运行此ansible时
ansible-playbook -i ./inventory_yaml/single_host_inventory.yaml ./playlist/mytest.yaml --extra-vars "{\"host\":\"192.168.0.39\"}"
我得到的错误结果为
PLAY [192.168.0.39] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.0.39]
TASK [Set JAVA_HOME] ***********************************************************
fatal: [192.168.0.39]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.0.39 closed.\r\n", "module_stdout": "sudo: a password is required\r\n", "msg": "MODULE FAILURE", "rc": 1}
to retry, use: --limit @/home/pankaj/go/src/ConfigTool/Go/playlist/mytest.retry
PLAY RECAP *********************************************************************
192.168.0.39 : ok=1 changed=0 unreachable=0 failed=1
所以现在我该如何获取消息“ sudo:需要密码”作为上述命令的输出。
谢谢。
答案 0 :(得分:1)
不确定您想要什么,但是您可以设置ignore_errors
,捕获结果并添加一个fail
任务:
- name: Set JAVA_HOME
blockinfile:
path: /etc/environment
backup: yes
state: present
block: |
Match User ansible-agent
PasswordAuthentication 4
become: yes
register: command_result
ignore_errors: true
- name: fail if the previous command did not succeed
fail:
msg: "{{ command_result.module_stdout }}"
when: command_result.failed == true
这是最好的清理方法。