How do I exit Ansible play without error on a condition

Question:

I want to exit without an error (I know about assert and fail modules) when I meet a certain condition. The following code exits but with a failure:

Answer:

Since Ansible 2.2, you can use end_play with the meta module:

You can also specify when for conditionally ending the play:

Note, though, that the task is not listed in the output of ansible-playbook, regardless of whether or not the play actually ends. Also, the task is not counted in the recap. So, you could do something like:

which will announce the end of the play right before ending it, only when the condition is met. If the condition is not met, you’ll see the task named end play if nothing to upgrade appropriately skipped, which would provide more info to the user as to why the play is, or is not, ending.

Of course, this will only end the current play and not all remaining plays in the playbook.

Leave a Reply