# setting variables inside a loop

**URL:** <https://forum.ansible.com/t/setting-variables-inside-a-loop/24415>\
**Category:** Ansible Project\
**Created:** [May 8, 2017, 2:47am UTC](https://forum.ansible.com/t/setting-variables-inside-a-loop/24415 "2017-05-08T02:47:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [May 8, 2017, 2:47am UTC](https://forum.ansible.com/t/setting-variables-inside-a-loop/24415/1 "2017-05-08T02:47:34Z")

</div>

Hi,

I think I’m missing something obvious, so please point me in the right direction (ansible 2.3).

- name: load and run services  
include\_role:  
name: “resource\_{{ item.resource }}”  
vars:  
resource: “{{ item }}”  
with\_items:
- “{{ services }}”

How do I set the var (‘resource’) to something dependent on my item? Whatever syntax I try I always end up with a literal and not the value. Is setting variables using loop values not supported?

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 12, 2017, 2:52am UTC](https://forum.ansible.com/t/setting-variables-inside-a-loop/24415/2 "2017-05-12T02:52:00Z")

</div>

you want:

loop\_control:  
&nbsp;&nbsp;&nbsp;loop\_var: resource

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [May 12, 2017, 3:30am UTC](https://forum.ansible.com/t/setting-variables-inside-a-loop/24415/3 "2017-05-12T03:30:27Z")

</div>

Turns out it’s actually a bug in ‘include\_role’ - a bunch of tickets have been opened about this. The general issue is that various types of variables are not visible inside the ‘include\_role’ scope. (see here: [https://github.com/ansible/ansible/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20include\_role%20vars](https://github.com/ansible/ansible/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20include_role%20vars) )

For now I’ve replaced the ‘include\_role’ with ‘include’ and a bunch of hacks to get the paths right.

kind regards  
Pshem
