# Using include\_role multiple times

**URL:** <https://forum.ansible.com/t/using-include-role-multiple-times/37219>\
**Category:** Ansible Project\
**Created:** [October 13, 2022, 7:58am UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219 "2022-10-13T07:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimanoren](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kimanoren](https://forum.ansible.com/u/kimanoren)\
**Post date:** [October 13, 2022, 7:58am UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219/1 "2022-10-13T07:58:48Z")

</div>

Need help with this one!  
Why are {{ ldap\_config\_endpoint }} not set the second time i call the include\_role  
"fatal: [10.141.112.2]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: {{ ldap\_config\_endpoint }}: {{ item.endpoint }}: ‘dict object’ has no attribute 'endpoint”

**----- ldap.yaml --------**

- name: Apply ldap configuration  
include\_tasks: tasks/apply\_ldap\_config.yml  
vars:  
ldap\_config: “{{ item.config }}”  
ldap\_config\_endpoint: “{{ item.endpoint }}”  
loop:
- { endpoint: ‘cm/service/roles/mgmt-NAVIGATORMETASERVER/config’, config: “{{ cloudera\_navigator\_ldap\_config }}” }
- { endpoint: ‘cm/config’, config: “{{ cloudera\_manager\_ldap\_config }}” }  
when: scm\_enable\_ldap

**----- apply\_ldap\_config.yml ------**

- name: Get current LDAP config  
include\_role:  
name: cloudera.cm\_api  
vars:  
cm\_api\_action: “{{ ldap\_config\_endpoint }}”  
cm\_api\_method: “GET”

- set\_fact:  
\_current\_ldap\_config: “{{ cm\_api\_response.json[‘items’] | items2dict(key\_name=‘name’, value\_name=‘value’) }}”

- name: Check if any config should be changed and notify cloudera manager server to restart if that is the case  
debug:  
msg: “Stale LDAP configuration”  
loop: “{{ ldap\_config | dict2items(key\_name=‘name’, value\_name=‘value’)}}”  
when: \_current\_ldap\_config[item.name] != item.value  
changed\_when: \_current\_ldap\_config[item.name] != item.value  
notify: Restart cloudera manager server  
no\_log: true

- name: Apply LDAP configuration  
include\_role:  
name: cloudera.cm\_api  
vars:  
cm\_api\_action: “{{ ldap\_config\_endpoint }}”  
cm\_api\_method: “PUT”  
cm\_api\_body:  
items: “[{{ item }}]”  
loop: “{{ ldap\_config | dict2items(key\_name=‘name’, value\_name=‘value’)}}”  
when: \_current\_ldap\_config[item.name] != item.value  
no\_log: true

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 13, 2022, 2:21pm UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219/2 "2022-10-13T14:21:20Z")

</div>

you are using 2 loops and the 2nd one does not seem have items with  
'endpoint' in them

use loop\_control to rename at least one of the loop variables so they  
don't overlap.

---

<div class="post-metadata">

**Author:** ![kimanoren](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kimanoren](https://forum.ansible.com/u/kimanoren)\
**Post date:** [October 13, 2022, 3:12pm UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219/3 "2022-10-13T15:12:01Z")

</div>

Thanks for your feedback!

ldap\_config\_endpoint is a var set in the loop for include\_tasks(apply\_ldap\_config.yml) in ldap.yaml  
So the var should be available for all of the tasks in apply\_ldap\_config.yml including both the include\_role calls, right?  
When calling the first include\_role the value is set/valid and everything works fine. When getting to the second include\_role call in the same file the value seems to be empty.

/Kim

torsdag 13. oktober 2022 kl. 16:23:02 UTC+2 skrev Brian Coca:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 13, 2022, 3:16pm UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219/4 "2022-10-13T15:16:10Z")

</div>

You are misreading the error, see this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.endpoint }}: 'dict object' has no endpoint

the problem is the "ITEM" variable as you have 2 loops, the 2nd one is  
overwriting the value, yet you expect the value to look like the first

---

<div class="post-metadata">

**Author:** ![kimanoren](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kimanoren](https://forum.ansible.com/u/kimanoren)\
**Post date:** [October 13, 2022, 3:29pm UTC](https://forum.ansible.com/t/using-include-role-multiple-times/37219/5 "2022-10-13T15:29:01Z")

</div>

Of course! THANK YOU!  
Sometimes you overlook the obvious…

Kinda embarrassing to use time on something like this… hehe

torsdag 13. oktober 2022 kl. 17:18:03 UTC+2 skrev Brian Coca:
