including roles from roles?

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.

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

as far as looping over includes:

`
=====> test.yml

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?

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

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

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?

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