# using loop with a 'when'

**URL:** <https://forum.ansible.com/t/using-loop-with-a-when/27746>\
**Category:** Ansible Project\
**Created:** [June 8, 2018, 9:05am UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746 "2018-06-08T09:05:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hbdhdhdhdjd](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@hbdhdhdhdjd](https://forum.ansible.com/u/hbdhdhdhdjd)\
**Post date:** [June 8, 2018, 9:05am UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/1 "2018-06-08T09:05:28Z")

</div>

I would like for ‘when’ to take the item to evaluate from the loop. How do i do it? Is it even possible?

---

<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:** [June 8, 2018, 1:35pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/2 "2018-06-08T13:35:58Z")

</div>

You have 'item' as the representation of the 'current item in the  
loop', but you are doing an indirection by 'letter' being the name of  
a variable, the 'vars' lookup can resolve that, so you end up with:

when: lookup('vars', item['letter']) == True

---

<div class="post-metadata">

**Author:** ![hbdhdhdhdjd](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@hbdhdhdhdjd](https://forum.ansible.com/u/hbdhdhdhdjd)\
**Post date:** [June 12, 2018, 3:45pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/3 "2018-06-12T15:45:12Z")

</div>

It’s ugly but worked like a charm. Thank you very much.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 12, 2018, 4:07pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/4 "2018-06-12T16:07:48Z")

</div>

You could use this if that look better

&nbsp;&nbsp;&nbsp;when: vars[item.letter] == True

And since the value is True or False you could also just do this

&nbsp;&nbsp;&nbsp;when: vars[item.letter]

---

<div class="post-metadata">

**Author:** ![hbdhdhdhdjd](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@hbdhdhdhdjd](https://forum.ansible.com/u/hbdhdhdhdjd)\
**Post date:** [June 12, 2018, 6:12pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/5 "2018-06-12T18:12:42Z")

</div>

> You could use this if that look better
> 
> when: vars[item.letter] == True

Thank you for that. Now it’s working AND it’s pretty. Where is this documented? The lookup is “kind of” documented, though not anywhere near ‘loop’. So it would never have occurred to me to connect the two.

> And since the value is True or False you could also just do this
> 
> when: vars[item.letter]

As I said originally, this was just an oversimplified example. The real playbook has more values than just boolean true/false.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 22, 2018, 12:57pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/6 "2018-06-22T12:57:01Z")

</div>

vars is only documented one place, in the FAQ  
[https://docs.ansible.com/ansible/latest/reference\_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names)

"if you need to use a dynamic variable use the hostvars or vars dictionary as appropriate"

vars is the same as hostvars[inventory\_hostname]

---

<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:** [June 22, 2018, 2:11pm UTC](https://forum.ansible.com/t/using-loop-with-a-when/27746/7 "2018-06-22T14:11:05Z")

</div>

No, don't use vars[varname], it is NOT the same as hostvars, it  
includes vars not present in hostvars and it does not template the  
variables in the same way. It is undocumented for a reason, it is not  
meant for users, it was used internally (not anymore) and it is  
scheduled for removal.

You CAN use the 'vars' lookup, i. e lookup('vars', 'varname') to deal  
with dynamic variables, this also includes non host specific variables  
not present in hostvars
