including roles from roles?

113 views
Skip to first unread message

Lorenzo Pisani

unread,
Jul 15, 2016, 7:59:19 PM7/15/16
to Ansible Project
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.

Dmitry Makovey

unread,
Jul 18, 2016, 11:31:31 AM7/18/16
to Ansible Project
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
---
 
- hosts: all
  tasks
:
     
- name: Ping-pong
        ping
:
     
- { include: "{{ item }}.yml", with_items: [ ping, pong ]}
=====> ping.yml
- name: Ping
  ping
:
=====> pong.yml
- name: Pong
  ping
:


which works as expected - first pings then pongs servers in inventory. 

Lorenzo Pisani

unread,
Jul 18, 2016, 1:22:23 PM7/18/16
to Ansible Project
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?

Dmitry Makovey

unread,
Jul 19, 2016, 6:15:05 PM7/19/16
to Ansible Project
I believe in the past similar topic came up and it was more of a philosophical issue (my memory could be wrong though)

J Hawkesworth

unread,
Jul 20, 2016, 8:26:09 AM7/20/16
to Ansible Project
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

Lorenzo Pisani

unread,
Jul 20, 2016, 3:01:46 PM7/20/16
to Ansible Project
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?

Mirko Friedenhagen

unread,
Jul 22, 2016, 2:16:53 AM7/22/16
to ansible-project

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
--
Sent from my mobile


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e6fabefc-3158-4141-b237-873579bcc351%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages