# Roles and sub Roles: A feature or a bug that will be fixed?

**URL:** <https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534>\
**Category:** Ansible Project\
**Created:** [September 23, 2013, 3:23pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534 "2013-09-23T15:23:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan7](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Dan7](https://forum.ansible.com/u/Dan7)\
**Post date:** [September 23, 2013, 3:23pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/1 "2013-09-23T15:23:15Z")

</div>

Hey,

I noticed that I can create roles inside roles in dirs of roles.

For example I can have the following structure:

- roles/php5-fpm
- roles/php5-fpm/pool

And then, inside a playbook I can do:

Roles:

- { role: php5-fpm, var1=something, var2=something }
- { role: php5-fpm/pool, var3=asdasd }

Such structure seems really logical for me.  
Using that structure I can also share tasks.

For example in /roles/php5-fpm/handlers/main.yml I can do:  
include: …/handlers/main.yml

And that way I won’t need to rewrite some stuff…

I didn’t see it documented anywhere, so I’m curious, will it be a good practice to use such structure?

Thanks.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 23, 2013, 4:01pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/2 "2013-09-23T16:01:49Z")

</div>

Yes, it’s perfectly valid to pass a path as the role name, which allows you to more easily keep a common directory for your roles rather than requiring they be located in the same directory as the playbook.

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [September 23, 2014, 5:23pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/3 "2014-09-23T17:23:30Z")

</div>

Hi,

I’m currently trying this but running into some problems.

My plan is to package groups of roles then include them with the project playbooks and roles.

i.e

roles/role1/  
roles/role2/  
roles/projectA/role3  
roles/projectA/role4

roles/projectA/role5  
…

The issue I have found is that, whilst I can reference projectA/role3

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [September 23, 2014, 6:15pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/4 "2014-09-23T18:15:03Z")

</div>

The folder the roles get added to was created as part of the build process in Maven.

I had a think and decided that if I move that back into Ansible and reference it in all the role dependencies then it should work

I originally wanted to let the calling ansible project dictate the structure of the shared roles but I think doing this makes sense

Open to all suggestions

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 24, 2014, 4:25pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/5 "2014-09-24T16:25:26Z")

</div>

It’s beside the point, but I really don’t like role dependencies in ansible - they were needed to end particular academic mailing list discussions about composability, and I think they are appropriate in some cases, but in _MOST_ cases they overcomplicate setup.

It would usually be easier to just list roles that need to run first ahead of other roles, and that way the list is more clearly shown in the playbook, rather than somewhat obfuscated in terms of order. Still, they aren’t going away.

All being said, tagging a role should tag the dependent roles - if that appears to not be working, definitely file a bug on GitHub for it.

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [September 24, 2014, 5:53pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/6 "2014-09-24T17:53:24Z")

</div>

Hi,

I see you point and I guess it should be improve the execution time to define a role once before the others rather than having each role check its dependency even if it is idempotent.

I think the idea was that each role would be more encapsulated and that it ought to define the dependencies instead of somebody else having to work it out.

I solved my initial problem by ensuring that all my shared roles reference the directory they were packaged in. Instead of defining it in the project that imports it.

All works fine and yes the tagging works perfectly.

Will look to reduce the role dependencies.

Thanks for your help

James

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 24, 2014, 6:29pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/7 "2014-09-24T18:29:19Z")

</div>

> Hi,
> 
> I see you point and I guess it should be improve the execution time to  
> define a role once before the others rather than having each role check its  
> dependency even if it is idempotent.
> 
> I think the idea was that each role would be more encapsulated and that it  
> ought to define the dependencies instead of somebody else having to work it  
> out.

There is already the "allow\_duplicates" setting in Ansible that will  
prevent a role from being loaded more than once, if called from the same  
parameters.

This should apply within the same play.

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [September 24, 2014, 7:12pm UTC](https://forum.ansible.com/t/roles-and-sub-roles-a-feature-or-a-bug-that-will-be-fixed/13534/8 "2014-09-24T19:12:08Z")

</div>

Ok great will have a look into that thanks
