# roles with pre\_tasks and post\_task

**URL:** <https://forum.ansible.com/t/roles-with-pre-tasks-and-post-task/14459>\
**Category:** Ansible Project\
**Created:** [January 29, 2014, 11:50pm UTC](https://forum.ansible.com/t/roles-with-pre-tasks-and-post-task/14459 "2014-01-29T23:50:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [January 29, 2014, 11:50pm UTC](https://forum.ansible.com/t/roles-with-pre-tasks-and-post-task/14459/1 "2014-01-29T23:50:29Z")

</div>

I have a patch locally that enables pre and post processing for roles. The directory names are pre\_tasks and post\_tasks, of course. The flow is:

- hosts: targets  
&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role-1  
&nbsp;&nbsp;&nbsp;&nbsp;- role-2  
&nbsp;&nbsp;&nbsp;pre\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: one  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: two  
&nbsp;&nbsp;&nbsp;post\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: three

and evaluates as:

role-1:pre\_tasks  
role-2:pre\_tasks  
play:pre\_tasks  
role-1:tasks  
role-2:tasks  
play:tasks  
role-1:post\_tasks  
role-2:post\_tasks  
play:post-tasks

I use it to have a role nginx-frontend, then then wants to know about ofbiz-backend and php-backend, and other hosts that then have those roles. I then have the backend role notify a handler, that then updates /etc/hosts with the address found in $hostvars.

I'm wondering if this makes sense to anyone else, and if it would be wanted? I haven't yet modified docsite, because I'm asking here first.

The following examples show a complex backend registration system; I hit some snags with ordering, so I added the pre/post support for roles, which should allow the main-line tasks to then query for the list of backend mappings. I haven't yet modified my backend role(shown below) with this new feature.

== examples:playbook.yml

- hosts: targets  
&nbsp;&nbsp;&nbsp;accelerate: true  
&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- etckeeper  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: nginx-frontend, when: "inventory\_hostname in (groups['frontend'] | default())" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: php-backend, when: "inventory\_hostname in (groups['php-backend'] | default())" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: mysql-backend, when: "inventory\_hostname in (groups['mysql-backend'] | default())" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: postgresql-backend, when: "inventory\_hostname in (groups['postgresql-backend'] | default())" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: ofbiz-backend, when: "inventory\_hostname in (groups['ofbiz-backend'] | default())" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: image-processor, when: "inventory\_hostname in (groups['image-processor'] | default())" }

== examples: nginx-frontend/meta.yml

dependencies:  
&nbsp;&nbsp;- role: backend  
&nbsp;&nbsp;&nbsp;&nbsp;tags: ['nginx']  
&nbsp;&nbsp;&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: nginx-frontend  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roles: ['ofbiz-backend', 'php-backend']

== examples: php-backend/meta.yml

dependencies:  
&nbsp;&nbsp;- role: backend  
&nbsp;&nbsp;&nbsp;&nbsp;tags: ['php']  
&nbsp;&nbsp;&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: php-backend  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roles: ['mysql-backend']

== examples: backend/tasks.yml

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host\_roles: "{{host\_roles is defined and (host\_roles.append(args.role) or host\_roles) or [args.role]}}"  
&nbsp;&nbsp;&nbsp;when: args.roles is defined  
- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wanted\_roles: "{{wanted\_roles is defined and (wanted\_roles.extend(args.roles) or wanted\_roles) or args.roles}}"  
&nbsp;&nbsp;&nbsp;notify:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- roles-scan {{ args.role }}

== examples: backend/handlers.yml

- name: roles-scan {{ args.role }}  
&nbsp;&nbsp;&nbsp;when: item[1] in wanted\_roles  
&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;dest=/etc/hosts  
&nbsp;&nbsp;&nbsp;&nbsp;regexp='^\S+\s+.\*?{{item[1]}}.universe'  
&nbsp;&nbsp;&nbsp;&nbsp;line='{{item[0].ansible\_eth0.ipv4.address}} {{item[1]}}.universe'  
&nbsp;&nbsp;&nbsp;notify:  
&nbsp;&nbsp;&nbsp;&nbsp;- etckeeper commit  
&nbsp;&nbsp;&nbsp;register: roles\_scan\_result  
&nbsp;&nbsp;&nbsp;with\_subelements:  
&nbsp;&nbsp;&nbsp;&nbsp;- hostvars.values() | selectattr('host\_roles', 'defined') | list  
&nbsp;&nbsp;&nbsp;&nbsp;- 'host\_roles'

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [January 30, 2014, 10:40pm UTC](https://forum.ansible.com/t/roles-with-pre-tasks-and-post-task/14459/2 "2014-01-30T22:40:14Z")

</div>

Hi Adam,

I suggest taking this to the developer list for further discussion. Features and ideas are regularly discussed there with the Ansible team.

[https://groups.google.com/forum/#!forum/ansible-devel](https://groups.google.com/forum/#!forum/ansible-devel)

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [January 31, 2014, 12:10am UTC](https://forum.ansible.com/t/roles-with-pre-tasks-and-post-task/14459/3 "2014-01-31T00:10:04Z")

</div>

Oh, oops, my bad, I wasn't aware there was a -dev list.
