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

319 views
Skip to first unread message

Robb Wagoner

unread,
Feb 2, 2015, 7:15:58 PM2/2/15
to ansible...@googlegroups.com
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

Giovanni Tirloni

unread,
Feb 3, 2015, 5:39:54 AM2/3/15
to ansible...@googlegroups.com
On Mon, 02 Feb 2015 16:15 -0800, Robb Wagoner <robb.w...@gmail.com>
wrote:
> 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

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

Robb Wagoner

unread,
Feb 3, 2015, 11:21:44 AM2/3/15
to ansible...@googlegroups.com
On Tuesday, February 3, 2015 at 3:39:54 AM UTC-7, Giovanni Tirloni wrote:
On Mon, 02 Feb 2015 16:15 -0800, Robb Wagoner <robb.w...@gmail.com>
wrote:
> 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 
 
Reply all
Reply to author
Forward
0 new messages