# Role dependencies across multiple plays

**URL:** <https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073>\
**Category:** Ansible Project\
**Created:** [December 5, 2013, 3:40pm UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073 "2013-12-05T15:40:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [December 5, 2013, 3:40pm UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/1 "2013-12-05T15:40:28Z")

</div>

According to the documentation, role dependencies would no longer run more than once, unless the setting allow\_duplicates: yes was used. I assume that is if a role is included more than once with the same play.

In my scenario a role called common is included from almost every other role and hence gets executed every time which makes the whole thing pretty lengthy. Is there a way to stop this from happening?

The main playbook looks more or less like this:

- name: “Apache Servers”  
roles:

- apache

- name: “MySQL Servers”  
roles:

- mysql

- name: “Some other Servers”  
roles:

- something

All the roles (apache, mysql, something) are dependent on the role common and if one of my hosts has more than one role, then both roles get executed and therefore common gets executed more than once. Can I change this?

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [December 5, 2013, 9:52pm UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/2 "2013-12-05T21:52:21Z")

</div>

I ended up having my common role use set\_fact on its first run, then it gets skipped on subsequent runs based on that fact, but it’s not an ideal solution. I’d be interested in a better answer for this as well.

Nathan

---

<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:** [December 5, 2013, 10:31pm UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/3 "2013-12-05T22:31:39Z")

</div>

Jürgen, the allow\_duplicates setting, along with all role data, is per-play. Whether it was included or not is tracked in the play itself, not per-host, so getting that information to persist across plays within a playbook would be tricky when each play included different host lists.

Nathan’s solution is probably the best work-around for this, which you could also do by registering a variable as the last task in the common role.

---

<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:** [December 5, 2013, 10:34pm UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/4 "2013-12-05T22:34:50Z")

</div>

Yeah so what Nathan’s said is this:

- hosts: all  
roles:

- common

- hosts: other  
roles:

- foo

- hosts: more  
roles:

- bar

- baz

And if you want to just run hosts in the group “more”, you can do that with “–limit more”

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [December 6, 2013, 7:51am UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/5 "2013-12-06T07:51:34Z")

</div>

Very nice approach. FWIW here is how I’ve been doing it and it works, thanks to Nathan.

/roles/common/tasks/main.yml (at the very end):

- name: ‘Common | Remember that this role had been run’  
set\_fact: role\_common\_completed=true

/roles/OTHERS/meta/main.yml  
dependencies:

- { role: common, when: role\_common\_completed is not defined }

Perfect !

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [December 6, 2013, 8:04am UTC](https://forum.ansible.com/t/role-dependencies-across-multiple-plays/14073/6 "2013-12-06T08:04:17Z")

</div>

Here’s a little further improvement that I have, so that I don’t need the condition on the role dependency. I move all the common role tasks to roles/common/tasks/common.yml, and then my roles/common/tasks/main.yml is just this:
