# Executing tasks before and after roles now possible - set\_up, tear\_down

**URL:** <https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705>\
**Category:** Ansible Project\
**Created:** [April 20, 2013, 8:12pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705 "2013-04-20T20:12:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 20, 2013, 8:12pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705/1 "2013-04-20T20:12:26Z")

</div>

As you may remember, roles are a 1.2 feature that allow for easy modularity and reuse of playbook steps.

I encountered something today where I wanted to do some tasks _before_ a role was invoked and then _after_ a role was invoked.

I came up with this:

[https://github.com/ansible/ansible/blob/devel/examples/playbooks/roletest.yml](https://github.com/ansible/ansible/blob/devel/examples/playbooks/roletest.yml)

The whole point of this is to be able to apply a role in a play for rolling updates.

Rolling updates of course worked before, but with the ability of free tasks surrounding the role to only come after the role steps, it wasn’t sufficient to use roles inside the rolling update idiom.

So basically, in set\_up, you would have

- signal outage window for host

- take host out of load balancer

Then you would apply your roles and any loose tasks

and in tear\_down, you would have

- put host back in load balancer

- end outage window for host

Naturally this is just a new thing that makes roles easier to use, and you don’t have to use it and it doesn’t affect your existing playbooks if you don’t.

Though I think this kind of being able to split out pre and post steps in a play could be useful for some people.

Let me know if this makes sense or not 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 20, 2013, 8:16pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705/2 "2013-04-20T20:16:04Z")

</div>

To make things simpler, I’m going to rename ‘set\_up’ to ‘pre\_tasks’ and ‘tear\_down’ to ‘post\_tasks’.

Teardown to put something back in production feels like a weird word to use.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 20, 2013, 10:05pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705/3 "2013-04-20T22:05:11Z")

</div>

This is all done.

I’ve also upgraded things such that handlers now trigger in between sections (like pre\_tasks and tasks, and so on).

This means that restarts triggered by handlers now work logically _before_ you put them back into a rotation, so you do not have to use tests with register + when to see when an item is changed, to restart it early.

There’s an internals trick to how this works if you want to use it yourself for any reason, and really want to run handlers “right now”.

tasks:

- foo…

- blarg…

- meta: flush\_handlers

- baz…

- glorp…

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [April 25, 2013, 10:59pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705/4 "2013-04-25T22:59:36Z")

</div>

Hi,

Thank you for mentioning this internal trick. It is handy and flexible.

Otherwise a generalized idea for pre\_tasks, roles, tasks and post\_tasks is to introduce user-definable stages, although to me something like flush\_handlers seems like a better solution (you do not have to define them explicitly, specify their order and just put it where you need it).

Greetings,  
gw

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 25, 2013, 11:11pm UTC](https://forum.ansible.com/t/executing-tasks-before-and-after-roles-now-possible-set-up-tear-down/12705/5 "2013-04-25T23:11:26Z")

</div>

Yeah we are not doing user defined flush-stages. That sounds like it would be approaching fluid dynamics.
