# Role inclusion mechanics

**URL:** <https://forum.ansible.com/t/role-inclusion-mechanics/17799>\
**Category:** Ansible Developer\
**Created:** [March 20, 2015, 3:40pm UTC](https://forum.ansible.com/t/role-inclusion-mechanics/17799 "2015-03-20T15:40:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuwan.ag](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@nuwan.ag](https://forum.ansible.com/u/nuwan.ag)\
**Post date:** [March 20, 2015, 3:40pm UTC](https://forum.ansible.com/t/role-inclusion-mechanics/17799/1 "2015-03-20T15:40:42Z")

</div>

Hi,

I’d like to make a feature request wrt to role dependencies. Currently, role dependencies must be defined via the dependencies section in meta/main.yaml and always execute prior to the tasks in the parent role. However, we are encountering a particular issue as follows:

We have RoleA which has a dependency defined on RoleB. However, we’d like to execute a task before RoleB is executed. We do not control RoleB, and therefore, cannot modify its tasks. With the current role dependency mechanics, there doesn’t seem to be a way to execute pre\_tasks before the execution of RoleB, short of creating a dummy role C which is included before RoleB.

Therefore, I was wondering whether the role inclusion mechanics could be made similar across playbooks and roles. That is, allow roles to include child roles, just as a playbook includes dependent roles, with pre\_tasks and post\_tasks. The advantages would be a simpler learning curve, since the mechanisms are more consistent, and also, provide more flexibility in terms of how roles can be composed, especially when dealing with third party roles.

cheers,

/Nuwan

---

<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:** [March 20, 2015, 4:01pm UTC](https://forum.ansible.com/t/role-inclusion-mechanics/17799/2 "2015-03-20T16:01:55Z")

</div>

Hi Nuwan,

It sounds like rather than making RoleB a dep on RoleA, you should simply include them in order, with any other pre-tasks you need to execute in another role, as follows:

roles:

- pre\_tasks\_role
- RoleB
- RoleA

Beyond that, I would be more inclined to add pre/post task support to roles rather than modifying the inclusion mechanism in any way. If all you’re executing is RoleA/RoleB, you could simply use the existing pre\_tasks structure too, which will execute before any role tasks are run.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![nuwan.ag](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@nuwan.ag](https://forum.ansible.com/u/nuwan.ag)\
**Post date:** [March 20, 2015, 4:37pm UTC](https://forum.ansible.com/t/role-inclusion-mechanics/17799/3 "2015-03-20T16:37:25Z")

</div>

Hi James,

Thanks for the reply. However, my understanding is that the suggestion you made wrt to pre\_tasks would only work if there was a playbook involved, which in our case, there isn’t. We only have two roles, and we want to publish both roles in Ansible Galaxy. RoleA’s meta/main.yaml looks as follows:

dependencies:

- { role: roleB }

However, since Ansible always executes the dependencies first, there is no way for us to insert a “pre\_task” before roleB is executed as I understand?

If so, the only other option is to create a dummy pre\_tasks\_role, and pollute Ansible Galaxy with it. This seems a bit wasteful, and inelegant. I suspect that more people will run into similar issues as they try to compose roles out of other roles?

cheers,  
/Nuwan
