# Bail out of a play based on a condition

**URL:** <https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271>\
**Category:** Ansible Project\
**Created:** [May 19, 2014, 6:51pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271 "2014-05-19T18:51:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 19, 2014, 6:51pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/1 "2014-05-19T18:51:24Z")

</div>

I was just wondering if there's a bail-out or break module/keyword  
that I can use to stop processing a play on that host after a certain  
condition is met.

For instance, I'm doing some setup on some servers where some certain  
things need to happen based on how the machine was provisioned  
(hardware setup). The condition doesn't really belong in a group and  
doesn't change the server's role, just whether or not I need to run  
the rest of the play. I can determine this condition and I've used  
register + set\_fact to make it available, but as it stands now I have  
to put a "when: do\_all\_the\_things" on each and every subsequent task.

Not a biggies, just wondering if there's a better way.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 19, 2014, 7:47pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/2 "2014-05-19T19:47:46Z")

</div>

Yes, there is a “fail” module which will terminate execution, which you can couple with the “when” keyword to target certain conditions.

[http://docs.ansible.com/fail\_module.html](http://docs.ansible.com/fail_module.html)

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 19, 2014, 7:58pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/3 "2014-05-19T19:58:07Z")

</div>

That's close but not quite what I want. I don't want to halt execution  
entirely, just skip the rest of this play for this host. There are  
other plays involved that should be run (part of a multiple role  
setup). Also, if you use fail and it's combined with serial and  
max\_fail\_percentage it will cause the entire run to halt and for this  
play to not even be attempted on other hosts. I don't want this  
condition to trigger a "failure". It's more like "next" or "continue"  
in most programming languages. I know ansible is not a full language,  
but it already has lots of flow control capabilities I was just  
wondering if it had this one too.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 19, 2014, 8:29pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/4 "2014-05-19T20:29:35Z")

</div>

Ahh, if you’re not wanting to completely bail out of the play entirely then you have a few options:

1. start using tags to label your tasks and use the --tags or --skip-tags options to control which parts are run/skipped.
2. move those tasks into a new file and use “include” with “when” to control when those tasks are run.
3. split your playbook into roles (which can also use tags and when to conditionally control execution).

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [May 19, 2014, 8:49pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/5 "2014-05-19T20:49:25Z")

</div>

> Ahh, if you're not wanting to completely bail out of the play entirely then  
> you have a few options:
> 
> 1) start using tags to label your tasks and use the --tags or --skip-tags  
> options to control which parts are run/skipped.

This doesn't work since the rest of the play is conditional on  
something that is dynamically discovered on the machine (let's say a  
battery-backed cache or something like that). We don't know if  
something is going to be skipped until the play has reached this  
point.

> 2) move those tasks into a new file and use "include" with "when" to control  
> when those tasks are run.

That has some promise. I'll report back after playing around.

> 3) split your playbook into roles (which can also use tags and when to  
> conditionally control execution).

Not something that is known ahead of time and can change. The play  
being executed is already in a role. Can a role conditionally include  
another role while it's executing? Also seems clumsy to have a logical  
role split into 2 separate roles. Makes having the logical 2nd half  
share the vars/handlers/etc of the 1st half trickier.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 19, 2014, 8:59pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/6 "2014-05-19T20:59:43Z")

</div>

Plays cannot be included in roles, only the other way around

Includes are never conditional, conditionals get applied to the tasks included, but not to the include itself.  
​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 21, 2014, 9:53pm UTC](https://forum.ansible.com/t/bail-out-of-a-play-based-on-a-condition/15271/7 "2014-05-21T21:53:30Z")

</div>

The idea of a “skip” task has been proposed.

It would need to be implemented by something that probably set a flag in the fact cache that playbook/ **init**.py knew about, and some automagic, and then cleared between starting new plays.

Pull requests might be entertained if it looked like:

- skip: msg=“I’m not that kind of Doctor”  
when: timelord

etc

Could be useful for security roles that opt out of running when certain apps aren’t installed! (think STIG and such).

If someone wants to take a crack at it, have at!
