# including roles from roles?

**URL:** <https://forum.ansible.com/t/including-roles-from-roles/22023>\
**Category:** Ansible Project\
**Created:** [July 15, 2016, 12:59am UTC](https://forum.ansible.com/t/including-roles-from-roles/22023 "2016-07-15T00:59:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo\_Pisani](https://avatars.discourse-cdn.com/v4/letter/l/7feea3/32.png) [@Lorenzo\_Pisani](https://forum.ansible.com/u/Lorenzo_Pisani)\
**Post date:** [July 15, 2016, 12:59am UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/1 "2016-07-15T00:59:15Z")

</div>

I’m quite sure this has been asked before. But I can’t find any discussions on it and the current functionality seems a bit crippled. Please let me know if I am missing something.

I would love to be able to define a role and use it from various other roles as if it was a module. For example, many of my roles need to create upstart scripts (after installing a few other things and before starting the service). I would love to simply include an upstart role that takes a few variables and creates the needed template(s). This would allow me to avoid having an upstart template in all my roles that need to create one. Here is what I understand to be my current set of options for doing something like this and why I don’t think it’s quite as clean:

**Create the upstart role and define it as a dependency**  
This mostly work. But I don’t always want the roles in question to run first. In the example of the upstart role, I would prefer running it after all the various packages have been installed. Another issue with this one is the fact that I cannot use loops like when **include** is used. It doesn’t allow me to have a variable passed in as the list of dependencies. For example, if a role needs to define a variable number of upstart scripts, I can’t have a variable number of dependencies (I don’t think).

**Place the logic in a task outside of roles and include that**  
This doesn’t allow me to have a reusable role for all upstart related logic. I lose templates, defaults, files, etc. I need to know exactly where this loose task file is and set up all the related files manually.

I was hoping to be able to do something along these lines:

`  
— #in some role

# some tasks here

- include: upstart  
vars:  
name: ‘{{ item.name }}’  
command: ‘{{ item.command }}’  
root: /path/to/app  
instance\_count: ‘{{ item.instance\_count }}’  
with\_items: ‘{{ my\_services }}’

# some other tasks here

`

The example is kind of bare but I hope it makes sense. I can expand on it if needed.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [July 18, 2016, 3:31pm UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/2 "2016-07-18T15:31:31Z")

</div>

Don’g believe it is possible nor do I think it will be implemented. Currently you can get close approximation by using dependencies: [http://docs.ansible.com/ansible/playbooks\_roles.html#role-dependencies](http://docs.ansible.com/ansible/playbooks_roles.html#role-dependencies)

as far as looping over includes:

`  
=====\> test.yml

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Pisani](https://avatars.discourse-cdn.com/v4/letter/l/7feea3/32.png) [@Lorenzo\_Pisani](https://forum.ansible.com/u/Lorenzo_Pisani)\
**Post date:** [July 18, 2016, 5:22pm UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/3 "2016-07-18T17:22:23Z")

</div>

Yeah I know you can loop over includes. And that’s why I would like to use it for including roles. Currently it doesn’t seem possible to do with dependencies.

Any reason you think this wouldn’t be implemented? Is it a technical limitation or philosophical?

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [July 19, 2016, 10:15pm UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/4 "2016-07-19T22:15:05Z")

</div>

I believe in the past similar topic came up and it was more of a philosophical issue (my memory could be wrong though)

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 20, 2016, 12:26pm UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/5 "2016-07-20T12:26:09Z")

</div>

Someone mentioned a while back (here I think) a way to get dependent roles to run after roles is to create a wrapper role that references your dependent role - no tasks in it, just a meta/main.yml to point at the role that you want to run.  
Not a trick I have tried myself but it sounds like you could use that to run your upstart role later in your plays.

Hope this helps,  
Jon

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Pisani](https://avatars.discourse-cdn.com/v4/letter/l/7feea3/32.png) [@Lorenzo\_Pisani](https://forum.ansible.com/u/Lorenzo_Pisani)\
**Post date:** [July 20, 2016, 7:01pm UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/6 "2016-07-20T19:01:46Z")

</div>

I think I get what you’re saying and I might go that route for now but it still gives us to limited dependencies functionality instead of being able to include thing with loops. Unless I misunderstood.

If I have role A and B, would I just make a role C with dependencies being [A,B] in order to control the order that way?

---

<div class="post-metadata">

**Author:** ![Mirko\_Friedenhagen](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mirko\_Friedenhagen](https://forum.ansible.com/u/Mirko_Friedenhagen)\
**Post date:** [July 22, 2016, 6:16am UTC](https://forum.ansible.com/t/including-roles-from-roles/22023/7 "2016-07-22T06:16:44Z")

</div>

Hello,

we do this a lot for e.g. runit scripts. Our roles are partitioned in install software, configure runit and configure software

Regards  
Mirko
