Using Ansible command return code

I was using the command module in Ansible but the command I was calling at the other end would cause the module to fail even on informational message. I believe this was partly due to the ‘rc’ value in the output being non-zero.

Googling and reading documentation showed a multitude or complex and not very accurate or not even working methods of responding to this return code.

The module output was:

{"changed": true, "cmd": ["/opt/IBM/ldap/V6.4/bin/idslink", "-g", "-i", "-l", "32", "-f"], "delta": "0:00:01.881979", "end": "2022-12-21 15:08:38.942974", "failed_when_result": false, "msg": "non-zero return code", "rc": 4, […]

The solution was quite simple:

name: Relink 64 bit libraries

when: ansible_os_family == "AIX"
command: /opt/IBM/ldap/V6.4/bin/idslink -g -i -l 64 -f
register: output
failed_when: output.rc not in [0, 2, 4 ]

This ensures reeturn code 0, 2 and 4 are not treated as errors.

This entry was posted in Ansible, Automation and tagged , , . Bookmark the permalink.

Leave a Reply