# do post\_tasks execute when a role fails?

**URL:** <https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153>\
**Category:** Ansible Project\
**Created:** [December 1, 2016, 1:31pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153 "2016-12-01T13:31:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergio.guillen](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sergio.guillen](https://forum.ansible.com/u/sergio.guillen)\
**Post date:** [December 1, 2016, 1:31pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/1 "2016-12-01T13:31:18Z")

</div>

Hi, I created a role that has a rescue block where role is marked as failed:

`  
— #myrole

- block:  
…  
rescue:
- fail: msg=‘some message’ #mark as fail ansible\_failed\_task is defined

`

On my playbook the idea is to send notification if ansible\_failed\_task is defined that’s why “myrole” fails on rescue section. So on my playbook if that variable is defined I send a notification:

`

- hosts: automation  
gather\_facts: True

post\_tasks:

- include: notification.yml status=unstable action=“Tests unstable”  
when: ansible\_failed\_task is defined

roles:

- { role: myrole, tags: tests}

`

When I run this playbook and “myrole” fails post\_tasks section is never executed, so my question is that: Will post\_tasks execute no matter whether role has failed or not? if not what should I use so post\_tasks get executed no matter role’s result?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 1, 2016, 8:10pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/2 "2016-12-01T20:10:29Z")

</div>

To clarify as the question indicates some misunderstanding, roles can only fail on import, what is failing is a task (which happens to be in a role)​.

Tasks in post\_tasks, are just like any other task, the only difference is ordering, they get executed after all previous tasks/role sections.

Any normal task won’t execute for a host if that host is in a failed state.

There is only one way to execute tasks after a failed state is via locating them inside the ‘rescue’ section of a block which contains the failing task.

A different approach is negating the fail state to begin with, using failed\_when or ignore\_errors.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 1, 2016, 8:12pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/3 "2016-12-01T20:12:13Z")

</div>

I forgot, there is also a ‘always’ section to blocks that runs tasks no matter the previous state.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 1, 2016, 8:13pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/4 "2016-12-01T20:13:21Z")

</div>

> Hi, I created a role that has a rescue block where role is marked as failed:
> 
> --- #myrole  
> - block:  
> &nbsp;&nbsp;...  
> &nbsp;&nbsp;rescue:  
> &nbsp;&nbsp;&nbsp;&nbsp;- fail: msg='some message' #mark as fail ansible\_failed\_task is defined

When ever Ansible run the fail module, it will immediately stop, no other task will be executed for that host.

> On my playbook the idea is to send notification if ansible\_failed\_task is  
> defined that's why "myrole" fails on rescue section. So on my playbook if  
> that variable is defined I send a notification:
> 
> - hosts: automation  
> &nbsp;&nbsp;gather\_facts: True
> 
> &nbsp;&nbsp;post\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- include: notification.yml status=unstable action="Tests unstable"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_failed\_task is defined
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { role: myrole, tags: tests}
> 
> When I run this playbook and "myrole" fails post\_tasks section is never  
> executed, so my question is that: Will post\_tasks execute no matter whether  
> role has failed or not? if not what should I use so post\_tasks get executed  
> no matter role's result?

The post\_tasks is just a way of group tasks to be run at some sequence.  
In a play you can have the following and they will run in the order they are listed bellow  
1. pre\_tasks  
2. roles  
3. tasks  
4. post\_tasks

In your example when it hits fail: it will stop an no more task will be executed.  
So the rest of the roles section, tasks and post\_tasks will not run for that host.

---

<div class="post-metadata">

**Author:** ![ssbarnea](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ssbarnea/32/4923_2.png) [@ssbarnea](https://forum.ansible.com/u/ssbarnea)\
**Post date:** [August 31, 2017, 2:36pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/5 "2017-08-31T14:36:12Z")

</div>

Sorry to revive this old thread but I (and many others) got the same problem which seems not to be addressed yet. My original post was at [https://stackoverflow.com/questions/45983636/how-to-always-run-some-ansible-roles-after-previous-failures](https://stackoverflow.com/questions/45983636/how-to-always-run-some-ansible-roles-after-previous-failures)

If you you wrote is still true (ansible 2.3.x), it means that ansible _roles_ are not really as useful as they were marketed as because once you get a failure inside a role it is impossible to run some other code after.

This “design limitation” is forcing people to abuse the use of “ignore\_errors” in order to still be able to do something after the failure, ending up with tons of roles full of ignore\_errors which we all know how nice they look in the console logs, where they are NOT colored red, hiding the original error.

So how are we supposed to re-use roles when is impossible to use them? Starting to replace role: foo with something like include: “…/roles/foo/tasks/main.yaml” ?

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [September 3, 2017, 12:20pm UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/6 "2017-09-03T12:20:35Z")

</div>

Does 'meta: clear\_host\_errors' help in this situation?  
[http://docs.ansible.com/ansible/latest/playbooks\_error\_handling.html#resetting-unreachable-hosts](http://docs.ansible.com/ansible/latest/playbooks_error_handling.html#resetting-unreachable-hosts)  
only mentions this as affecting unreachable hosts, but  
[http://docs.ansible.com/ansible/latest/meta\_module.html](http://docs.ansible.com/ansible/latest/meta_module.html) mentions that it  
clears the failed state in general.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)  
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Niels\_Bertram](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@Niels\_Bertram](https://forum.ansible.com/u/Niels_Bertram)\
**Post date:** [December 3, 2017, 11:51am UTC](https://forum.ansible.com/t/do-post-tasks-execute-when-a-role-fails/23153/7 "2017-12-03T11:51:15Z")

</div>

Hmm if I had to develop specific error handling around roles, I would use the [block statement](http://docs.ansible.com/ansible/latest/playbooks_blocks.html). Have not been able to come up with something that I could not handle. N

tasks:

- block:
- include\_role:  
name: myrole-that-fails  
rescue:
- debug: msg=“Help me, I just melted”  
always:
- debug: msg=“I run no matter what”
