Ansible-playbooks has own output style. In this blog post I have just shared YAML file that explains how to handle return codes in ansible-playbooks
So there is an example that handles errors of Ansible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- hosts: localhost gather_facts: no #strategy: debug tasks: - name: run this command and ignore the result shell: ping -c 1 8.8.8.8 register: pingout ignore_errors: yes - debug: msg="{{ pingout.rc }}" - name: Prepare well output raw: echo "What a wonderful world" register: output when: pingout.rc == 0 - debug: var=output - name: run this command and ignore the result raw: ping -c 1 8.8.8.7 register: pingout2 ignore_errors: yes - debug: msg="{{ pingout2.rc }}" - name: run this command if something wrong shell: echo "Something goes wrong :)" register: output2 when: pingout2.rc != 0 - debug: var=output2 |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
PLAY [localhost] *************************************************************** TASK [run this command and ignore the result] ********************************** changed: [127.0.0.1] TASK [debug] ******************************************************************* ok: [127.0.0.1] => { "msg": "0" } TASK [run this command if everything is okey] ********************************** changed: [127.0.0.1] TASK [debug] ******************************************************************* ok: [127.0.0.1] => { "output": { "changed": true, "cmd": "echo \"What a wonderful world\"", "delta": "0:00:00.001372", "end": "2016-08-18 11:16:11.745843", "rc": 0, "start": "2016-08-18 11:16:11.744471", "stderr": "", "stdout": "What a wonderful world", "stdout_lines": [ "What a wonderful world" ], "warnings": [] } } TASK [run this command and ignore the result] ********************************** fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "rc": 1, "stderr": "", "stdout": "PING 8.8.8.7 (8.8.8.7) 56(84) bytes of data.\r\n\r\n--- 8.8.8.7 ping statistics ---\r\n1 packets transmitted, 0 received, 100% packet loss, time 0ms\r\n\r\n", "stdout_lines": ["PING 8.8.8.7 (8.8.8.7) 56(84) bytes of data.", "", "--- 8.8.8.7 ping statistics ---", "1 packets transmitted, 0 received, 100% packet loss, time 0ms", ""]} ...ignoring TASK [debug] ******************************************************************* ok: [127.0.0.1] => { "msg": "1" } TASK [run this command if something wrong] ************************************* ok: [127.0.0.1] TASK [debug] ******************************************************************* ok: [127.0.0.1] => { "output2": { "changed": false, "rc": 0, "stderr": "", "stdout": "Something goes wrong :)\r\n", "stdout_lines": [ "Something goes wrong :)" ] } } PLAY RECAP ********************************************************************* 127.0.0.1 : ok=8 changed=2 unreachable=0 failed=0 |
That playbook will not return with failed status because of that we even we command output is different than 0. The reason of it ignore_errors: yes
As you see both outputs are different that each other. You can play with it to understand better way. Check the links for more information that explains debugging and statements.
For more:
- ansible statements
- ansible debug
- ansible test debug
- Nice gist that explain AND/OR (Recommended)
- If you work with Emacs there is good advices about dealing with yaml files such as indentation tricks or error handling