# conditional for Ansible to exit success (sys.exit(0))

**URL:** <https://forum.ansible.com/t/conditional-for-ansible-to-exit-success-sys-exit-0/17428>\
**Category:** Ansible Project\
**Created:** [February 3, 2015, 12:15am UTC](https://forum.ansible.com/t/conditional-for-ansible-to-exit-success-sys-exit-0/17428 "2015-02-03T00:15:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robb\_Wagoner](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Robb\_Wagoner](https://forum.ansible.com/u/Robb_Wagoner)\
**Post date:** [February 3, 2015, 12:15am UTC](https://forum.ansible.com/t/conditional-for-ansible-to-exit-success-sys-exit-0/17428/1 "2015-02-03T00:15:58Z")

</div>

I have a condition under which I need Ansible to exit without an error (i.e. sys.exit(0)). Is there a means by which to do this? I.e. the complement to the **fail** module.

Thanks!  
Robb

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 3, 2015, 10:39am UTC](https://forum.ansible.com/t/conditional-for-ansible-to-exit-success-sys-exit-0/17428/2 "2015-02-03T10:39:47Z")

</div>

That's curious situation. Perhaps you can call ansible this way so the  
result of the subshell is always success?

$(ansible-playbook play.yml; exit 0); echo $?  
0

After a quick look in the plugins code I don't see a way to hook into  
Ansible and change the exit code. If you want to mess with the code  
directly, edit ansible-playbook and change the return code in case of  
errors. However, it's a non-standard change you'll have to keep track of  
between updates.

Giovanni

---

<div class="post-metadata">

**Author:** ![Robb\_Wagoner](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Robb\_Wagoner](https://forum.ansible.com/u/Robb_Wagoner)\
**Post date:** [February 3, 2015, 4:21pm UTC](https://forum.ansible.com/t/conditional-for-ansible-to-exit-success-sys-exit-0/17428/3 "2015-02-03T16:21:43Z")

</div>

> > I have a condition under which I need Ansible to exit without an error  
> > (i.e. sys.exit(0)). Is there a means by which to do this? I.e. the  
> > complement to the _fail_ module.
> 
> That’s curious situation. Perhaps you can call ansible this way so the  
> result of the subshell is always success?
> 
> $(ansible-playbook play.yml; exit 0); echo $?  
> 0

Thanks for the input Giovanni. A sub-shell approach is interesting - I’ll experiment with trap inheritance settings. I need to exit success because I have Bash trap $mutex\_remove\_cmd ERR, on actual error conditions which removes a target environment’s mutex. In my situation, if the mutex exists, then I want Ansible to stop and provide a polite message that the target environment is being updated by Ansible elsewhere, without removing the mutex. If there is an actual error/failure, the mutex should be removed, and the state of the environment restored. The typical use-case is a shared testing environment, where CI/CD could be doing a deployment/update or an engineer could be testing their own updates. Only one gets access to the testing environment at any one time.

> After a quick look in the plugins code I don’t see a way to hook into  
> Ansible and change the exit code. If you want to mess with the code  
> directly, edit ansible-playbook and change the return code in case of  
> errors. However, it’s a non-standard change you’ll have to keep track of  
> between updates.

> Giovanni

I’ve done something similar with several of the ec2\_ core modules due to the pace of PR acceptance in the ansible-modules-core project. But that is a bit easier to managed/track upstream, via site-local library/ pathing, IMO, than doing the same to Ansible itself.

Regards,  
Robb
