"de-duplicating" roles

In my site.yml, I have the following:

  • include: role1.yml

  • include: role2.yml

  • include: …

In each of the roleX.yml files, I have:

  • hosts: roleXservers
    sudo: yes
    roles:
  • common
  • roleX

Now, in my inventory, if I have a server to which I’ve assigned multiple roles, the “common” role is getting applied twice, once for each roleX.yml file that applies to the server.

Is there any way to “de-duplicate” roles before applying? Or perhaps a different way I can structure things to avoid having my “common” role applied multiple times?

-Erik

Erik, this is occurring because these roles are spanning multiple plays (which is what the “include: whatever.yml” syntax does) and we do not currently de-dupe roles across plays (only within plays). This question has come up in the past, such as this thread:

https://groups.google.com/forum/#!topic/ansible-project/nd4wMLsb0Jw

There are some suggestions there on how to avoid multiple runs via the use of set_fact, which currently is the best way to avoid this.

Other than speed(because it takes longer to run duplicate roles), is this really a problem? A playbook result should always have a constant, final output, when it is done with all it's tasks. A duplicate role is simliar to just running a playbook multiple times, which shouldn't really cause any changes.

The most fundamental problem is you shouldn’t be doing playbooks by role, you should be including playbooks by group.

The playbook then applies the roles to the groups.

That will remove the duplication question entirely.

–Michael

i.e. if there are a set of servers that are FooAndBar

  • hosts: my_special_group_of_servers_that_do_foo_and_bar
    roles:

  • common

  • foo

  • bar

  • hosts: my_special_group_of_servers_that_just_baz_and_glorp
    roles:

  • common

  • baz

  • glorp