# "de-duplicating" roles

**URL:** <https://forum.ansible.com/t/de-duplicating-roles/14463>\
**Category:** Ansible Project\
**Created:** [January 30, 2014, 6:54pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463 "2014-01-30T18:54:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [January 30, 2014, 6:54pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463/1 "2014-01-30T18:54:31Z")

</div>

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

---

<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:** [January 30, 2014, 7:17pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463/2 "2014-01-30T19:17:24Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [January 30, 2014, 8:14pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463/3 "2014-01-30T20:14:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 30, 2014, 11:03pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463/4 "2014-01-30T23:03:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 30, 2014, 11:05pm UTC](https://forum.ansible.com/t/de-duplicating-roles/14463/5 "2014-01-30T23:05:13Z")

</div>

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
