# Warning about '{{' in loops, while no other option

**URL:** <https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714>\
**Category:** Ansible Project\
**Created:** [October 18, 2013, 11:57am UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714 "2013-10-18T11:57:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 18, 2013, 11:57am UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/1 "2013-10-18T11:57:46Z")

</div>

I have some loops that take a combined string + variable, not just a variable.  
In this case, AFAIK, i’ts needed to use the {{ var notation, yet I still get

[WARNING]: It is unneccessary to use ‘{{’ in loops, leave variables in loop  
expressions bare.

e.g.:

with\_fileglob: files/ssh\_keys/{{ app }}/\*  
with\_items: tomcat\_libs.{{ tomcat\_version }}

I don’t see any alternative to write this without ‘{{’, and if that is confirmed, it might be good to tweak the WARNING code to not trigger on thise cases?

PS I know I could rewrite the second example to

with\_items: tomcat\_libs[tomcat\_version]

but that doesn’t work, this gets me

fatal: [server] =\> with\_items expects a list

( which might be a bug on itself? tomcat\_libs is defined as:

tomcat\_libs:  
7:

- acdprobe
- libpostgresql9.1-jdbc3-acd-java
- libpostgis-1.5-acd-java
- libactivemq5.5-acd-java

)

Let me know if and which bug tickets I should file, or if I’m missing an alternative way of doing things.

Thanks,

Serge

---

<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:** [October 18, 2013, 1:28pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/2 "2013-10-18T13:28:36Z")

</div>

This can pretty easily be fixed by splitting on the “:” and checking for the beginning/ending character.

Please file this on github. Thanks!

---

<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:** [October 18, 2013, 1:29pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/3 "2013-10-18T13:29:40Z")

</div>

Don’t see why the following has any issues:

with\_items: tomcat\_libs[tomcat\_version]

It may be that tomcat version is itself a template operation?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 18, 2013, 1:36pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/4 "2013-10-18T13:36:59Z")

</div>

> This can pretty easily be fixed by splitting on the ":" and checking for  
> the beginning/ending character.
> 
> Please file this on github. Thanks!

[https://github.com/ansible/ansible/issues/4582](https://github.com/ansible/ansible/issues/4582)

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 18, 2013, 2:10pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/5 "2013-10-18T14:10:31Z")

</div>

I stand corrected. It does work. My example just didn't have tomcat\_version  
== 7

The issue at hand here is a little different.

What happens, is the notation tomcat\_libs[tomcat\_version] returns that  
"not list" error when tomcat\_libs[tomcat\_version] does not exits, while  
while the tomcat\_libs.{{ tomcat\_version }} notation seems to return some  
kind of empty list, and the task is skipped.

So:

tomcat\_libs:  
&nbsp;&nbsp;7:  
&nbsp;&nbsp;&nbsp;&nbsp;- acdprobe  
&nbsp;&nbsp;&nbsp;&nbsp;- libpostgresql9.1-jdbc3-acd-java  
&nbsp;&nbsp;&nbsp;&nbsp;- libpostgis-1.5-acd-java  
&nbsp;&nbsp;&nbsp;&nbsp;- libactivemq5.5-acd-java

-\> with\_items: tomcat\_libs[tomcat\_version]  
this notation doesn't return a list if tomcat\_version == 6  
and adding that non-existing key with an empty list doesn't make it work  
either:

tomcat\_libs:  
&nbsp;&nbsp;6:   
&nbsp;&nbsp;7:  
&nbsp;&nbsp;&nbsp;&nbsp;- acdprobe  
&nbsp;&nbsp;&nbsp;&nbsp;- libpostgresql9.1-jdbc3-acd-java  
&nbsp;&nbsp;&nbsp;&nbsp;- libpostgis-1.5-acd-java  
&nbsp;&nbsp;&nbsp;&nbsp;- libactivemq5.5-acd-java

The workaround I found now, is:

with\_items: tomcat\_libs[tomcat\_version] | default()

Which makes the task just skipped then.

Thanks for the pointer 🙂

Serge

---

<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:** [October 18, 2013, 2:55pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/6 "2013-10-18T14:55:17Z")

</div>

"tomcat\_libs.{{ tomcat\_version }} "

You can’t build variable names dynamically.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 18, 2013, 3:02pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/7 "2013-10-18T15:02:50Z")

</div>

​Obviously. But somehow this results in some kind of empty list when used  
in with\_items.  
​

---

<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:** [October 18, 2013, 3:11pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/8 "2013-10-18T15:11:28Z")

</div>

Where’s my ticket? 🙂

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 20, 2013, 4:01pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/9 "2013-10-20T16:01:39Z")

</div>

​Only now had the time to make that ticket.​ ​When I started making test  
playbooks to document​ it, I noticed another behaviour than I first  
reported in this list:  
I tested it with the very latest devel version, with Michael's updates on  
variable referencing.

So I made a ticket describing those, basically there is a difference in how  
with\_items resolves things noted as dict.{{ key }} and dict.key:

​[https://github.com/ansible/ansible/issues/4606​](https://github.com/ansible/ansible/issues/4606%E2%80%8B)

​HTH,

Serge​

---

<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:** [October 20, 2013, 4:16pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/10 "2013-10-20T16:16:30Z")

</div>

dict.{{ foo }}

was never meant to be supported syntax, FWIW.

I’ve added some comments on the ticket about what you’ve said about adding some more warnings about invalid variable keys and so forth.

Should be easy to fix but could use some clarification.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 20, 2013, 4:37pm UTC](https://forum.ansible.com/t/warning-about-in-loops-while-no-other-option/13714/11 "2013-10-20T16:37:37Z")

</div>

Yeah, I didn’t know dict.{{ foo }} was not supported.

Which makes that ticket a lot simpler. I answered on the ticket.

The only issue at hand now, is that, given

tomcat\_libs: 7: ​ - item1 - item2 referencing a NON existing key like this: - with\_items: tomcat\_libs.6 returns → “item”: “tomcat\_libs.6”

where I would expect this to error about tomcat\_libs.6 not being an existing variable/key and certainly not being a list.

Putting the 6 key in a variable, yields the expected ‘not a list’ error, though.

Serge
