# catch  return code

**URL:** <https://forum.ansible.com/t/catch-return-code/28605>\
**Category:** Ansible Project\
**Created:** [September 19, 2018, 10:16am UTC](https://forum.ansible.com/t/catch-return-code/28605 "2018-09-19T10:16:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [September 19, 2018, 10:16am UTC](https://forum.ansible.com/t/catch-return-code/28605/1 "2018-09-19T10:16:39Z")

</div>

how to i check the return code of a shell command correctly? see error below. looks like a chicken/egg situation to me…create\_oml\_users should be registerd if rc is 0…any ideas?

`

- shell: lsuser -R files lsam 2\>/dev/null  
register: create\_oml\_users  
when: create\_oml\_users.rc == 0  
ignore\_errors: true

`

`  
fatal: [AIXWUKIT]: FAILED! =\> {}

MSG:

The conditional check ‘create\_oml\_users.rc == 0’ failed. The error was: error while evaluating conditional (create\_oml\_users.rc == 0): ‘create\_oml\_users’ is undefined

The error appears to have been in ‘/etc/ansible/aix/roles/manage\_ldap\_users/tasks/main.yml’: line 13, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- shell: lsuser -R files lsam 2\>/dev/null  
^ here

…ignoring  
Enter code here…  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 19, 2018, 11:02am UTC](https://forum.ansible.com/t/catch-return-code/28605/2 "2018-09-19T11:02:07Z")

</div>

You must do that in the next task, not in the task itself.

---

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [September 19, 2018, 11:37am UTC](https://forum.ansible.com/t/catch-return-code/28605/3 "2018-09-19T11:37:03Z")

</div>

wow…guess i need some sleep. sorry for this.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 19, 2018, 1:09pm UTC](https://forum.ansible.com/t/catch-return-code/28605/4 "2018-09-19T13:09:23Z")

</div>

No worries, good night and sleep tight 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 19, 2018, 1:16pm UTC](https://forum.ansible.com/t/catch-return-code/28605/5 "2018-09-19T13:16:04Z")

</div>

when: executes 'before the command' but you can use  
failed\_when/changed\_when to check the code 'after' in the same task.
