Ansible: working with Return Codes

Ansible-playbooks has own output style. In this blog post I have just give yaml file that explains how to handle return codes in ansible-playbooks

So I have clear example that explains and handles errors of Ansible.

- 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 }}"
    #- debug: var=pingout
 
    - name: run this command if everything is okey
      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 }}"
    #- debug: var=pingout2
 
    - name: run this command if something wrong
      shell: echo "Something goes wrong :)"
      register: output2
      when:  pingout2.rc  != 0
    - debug: var=output2

 

Note: that command does not going to show you any fail at the end because of ignore_errors: yes


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

 

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:

 

Big thanks to this blog author

Latest blog

Web Analytics