# Including roles dynamically make dependencies runs multiple time

**URL:** <https://forum.ansible.com/t/including-roles-dynamically-make-dependencies-runs-multiple-time/30255>\
**Category:** Ansible Project\
**Created:** [May 3, 2019, 8:58am UTC](https://forum.ansible.com/t/including-roles-dynamically-make-dependencies-runs-multiple-time/30255 "2019-05-03T08:58:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bleacks](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@Bleacks](https://forum.ansible.com/u/Bleacks)\
**Post date:** [May 3, 2019, 8:58am UTC](https://forum.ansible.com/t/including-roles-dynamically-make-dependencies-runs-multiple-time/30255/1 "2019-05-03T08:58:35Z")

</div>

Hello everyone,

(this is my first post BTW, I’ll try to be as clear as possible)

I want to include roles dynamically using this bit of code in my `playbook.yml` :

`  
tasks:

- name: “Including roles dynamically”  
remote\_user: “{{ target\_user }}”  
include\_role:  
name: “{{ item }}”  
with\_items: “{{ roles\_to\_install | unique }}”  
`

I have roles like following:  
common

role1 → common  
role2 → role1

My goal is to always include every role (but I also have another use case where I want to specify which roles will be included).

And using this method, if I try with roles\_to\_install = [‘role1’, ‘role2’]  
These roles will be included :

- role 2 : 1 time

- role 1 : 2 times

- common : 3 times

I searched through issues posted on git repo, I found some which may seem duplicates for me, but no solution was provided. ([24326](https://github.com/ansible/ansible/issues/24326), [47374](https://github.com/ansible/ansible/issues/47374) ).

ansible --version: 2.7.10

I also tried with `allow_duplicates: no` but the problem is not related to role inclusion, but to the dependency inclusion.

Do you have an idea of what I could have forgotten? Maybe there is another way of treating the problem?

Thank you for your time,  
If you need information I forgot, I’ll be glad to add them

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [May 3, 2019, 2:18pm UTC](https://forum.ansible.com/t/including-roles-dynamically-make-dependencies-runs-multiple-time/30255/2 "2019-05-03T14:18:19Z")

</div>

I’m not sure if it’s the best way, but when including roles I’ll do something like:

- name: Include Some Role if Necessary  
include\_role:  
name: some\_role  
when: SOME\_ROLE\_RUN is not defined

Then as the last task in “some\_role” I’ll have a task like this:

- set\_fact:  
SOME\_ROLE\_RUN: True

–Steve

---

<div class="post-metadata">

**Author:** ![Bleacks](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@Bleacks](https://forum.ansible.com/u/Bleacks)\
**Post date:** [May 6, 2019, 10:58am UTC](https://forum.ansible.com/t/including-roles-dynamically-make-dependencies-runs-multiple-time/30255/3 "2019-05-06T10:58:01Z")

</div>

Thank you for your answer Steve.

I have tried you proposition, it is working as expected, dependencies are now imported only once.

But still, I’m looking for a cleaner approach (if possible). Besides, I have some problems with this solution :

- There is noise in the logs (with all the “skipping” tasks between rightful imports)

- The intensive use of “set\_fact” could become a source of problem later, with name collision etc…

- I want to create a project that could be forked by other users, for them to add their roles. And I wanted to limit constraint for them to be integrated in my project.

Thank you again for your help, I’ll try to find something, and if I can’t I’ll use your proposition
