# How to abort just one role and let the others run

**URL:** <https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254>\
**Category:** Ansible Project\
**Created:** [September 17, 2014, 8:22am UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254 "2014-09-17T08:22:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![P11](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@P11](https://forum.ansible.com/u/P11)\
**Post date:** [September 17, 2014, 8:22am UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/1 "2014-09-17T08:22:51Z")

</div>

Hi all,

we have a playbook with several roles in it:  
roles:

- role1
- role2
- role3
- role4
- role5
- roleX  
…

These roles install some software but each checks at the very begin if the software is already installed  
and if it is - it aborts (using fail module). It turns out that i.e. if the role3 fails (because its software is already installed)  
the whole playbook also failes (ansible 1.7.1). Is it possible to abort just particular role  
and let run the rest of the playbook ?

---

<div class="post-metadata">

**Author:** ![Karl\_E\_Jorgensen](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Karl\_E\_Jorgensen](https://forum.ansible.com/u/Karl_E_Jorgensen)\
**Post date:** [September 17, 2014, 12:08pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/2 "2014-09-17T12:08:46Z")

</div>

Hi

> Hi all,
> 
> we have a playbook with several roles in it:  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;- role1  
> &nbsp;&nbsp;- role2  
> &nbsp;&nbsp;- role3  
> &nbsp;&nbsp;- role4  
> &nbsp;&nbsp;- role5  
> &nbsp;&nbsp;- roleX  
> ...
> 
> These roles install some software but each checks at the very begin if the  
> software is already installed  
> and if it is - it aborts (using fail module). It turns out that i.e. if the  
> role3 fails (because its software is already installed)  
> the whole playbook also failes (ansible 1.7.1).

This is the expected behaviour. A failure is a failure...

> Is it possible to abort just particular role  
> and let run the rest of the playbook ?

I would recommend changing the roles so that installation of new  
software is skipped (instead of failing) when the software is already  
present...

---

<div class="post-metadata">

**Author:** ![P11](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@P11](https://forum.ansible.com/u/P11)\
**Post date:** [September 17, 2014, 12:15pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/3 "2014-09-17T12:15:57Z")

</div>

> Hi
> 
> > Hi all,
> > 
> > we have a playbook with several roles in it:  
> > roles:
> > 
> > - role1
> > - role2
> > - role3
> > - role4
> > - role5
> > - roleX  
> > …
> > 
> > These roles install some software but each checks at the very begin if the  
> > software is already installed  
> > and if it is - it aborts (using fail module). It turns out that i.e. if the  
> > role3 fails (because its software is already installed)  
> > the whole playbook also failes (ansible 1.7.1).
> 
> This is the expected behaviour. A failure is a failure…

Why ?!?! I use fail module in a role so the role should fail and not the whole playbook.

> > Is it possible to abort just particular role  
> > and let run the rest of the playbook ?
> 
> I would recommend changing the roles so that installation of new  
> software is skipped (instead of failing) when the software is already  
> present…

What do you mean by “skipped” ? If I check at the top of a role that the software is installed  
how can I skip the rest of role ?

---

<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:** [September 17, 2014, 2:45pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/4 "2014-09-17T14:45:06Z")

</div>

You will need to handle failures and skipping on a per-task basis - that logic does not exist at the role level.

As Karl noted, you should not use the fail module in this case. With Ansible (and just about every other configuration management system out there now), you define the state of the system as you want it to be, and Ansible will make the changes required to bring your system to that state. The fact that the software is already installed means your system is in the state expected, which is not a failure.

---

<div class="post-metadata">

**Author:** ![Patrick\_Ansible-ML](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Patrick\_Ansible-ML](https://forum.ansible.com/u/Patrick_Ansible-ML)\
**Post date:** [September 17, 2014, 2:52pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/5 "2014-09-17T14:52:22Z")

</div>

You could add a 'when:' clause in the tasks following the software installation task to prevent them from running when the software is already installed. The software installation task will need to set a 'register:' clause with the result which can then be used by the 'when:' clause in the following tasks.

[http://docs.ansible.com/playbooks\_conditionals.html](http://docs.ansible.com/playbooks_conditionals.html)

HTH,  
Patrick

---

<div class="post-metadata">

**Author:** ![P11](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@P11](https://forum.ansible.com/u/P11)\
**Post date:** [September 17, 2014, 2:55pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/6 "2014-09-17T14:55:17Z")

</div>

I used fail module because I cannot find anything else.  
All I need is to check if something exists and if does skip the rest of role.  
Currently if I want to skip all the rest 30 tasks I need to repeat 30 lines of:

when: something is not defined or exists or whatever …

It would be good to have something really simple like that:  
abort\_role:  
when: condition is met

---

<div class="post-metadata">

**Author:** ![P11](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@P11](https://forum.ansible.com/u/P11)\
**Post date:** [September 17, 2014, 3:02pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/7 "2014-09-17T15:02:39Z")

</div>

I use it in my playbooks but when a role has many, many tasks you need to repeat  
the same “when:” line in every task which is not very handy.

Also in more complicated cases your ansible code will go very unreadable.  
Imagine you are going to install software related to hardware.  
You don’t need to install it under VM. So the most intuitive way  
(at least for me 😉 ) would be:

if this is VM:  
abort role  
if the software is installed:  
abort role  
install software  
…

---

<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:** [September 17, 2014, 4:53pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/8 "2014-09-17T16:53:28Z")

</div>

In that case, you may wish to do something in your roles like:

- name: check to see if something is already done  
whatever: a=b c=d  
register: is\_done

- name: include the rest of the role tasks from another file if the above thing isn’t done  
include: role\_tasks.yml  
when: not is\_done

---

<div class="post-metadata">

**Author:** ![Patrick\_Ansible-ML](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Patrick\_Ansible-ML](https://forum.ansible.com/u/Patrick_Ansible-ML)\
**Post date:** [September 17, 2014, 5:14pm UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/9 "2014-09-17T17:14:27Z")

</div>

Thanks James. That's an excellent way to get rid of the repetition.

Cheers,  
Patrick

---

<div class="post-metadata">

**Author:** ![P11](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@P11](https://forum.ansible.com/u/P11)\
**Post date:** [September 18, 2014, 6:40am UTC](https://forum.ansible.com/t/how-to-abort-just-one-role-and-let-the-others-run/16254/10 "2014-09-18T06:40:55Z")

</div>

Hi James,

I used this syntax in my playbooks. But when you have to check several different conditions (and it’s not only about  
installing software !) the “when:” becomes quite complicated. And I still think that simple kind of “abort\_role”  
would make a code (especially in bigger playbooks/roles) much, much cleaner.

Anyway - thanks James - now I know I have no other options and have to stick with your way  
of doing it.  
Thanks again 🙂

Regards
