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

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

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

Please file this on github. Thanks!

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?

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

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:
  7:
    - acdprobe
    - libpostgresql9.1-jdbc3-acd-java
    - libpostgis-1.5-acd-java
    - 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:
  6:
  7:
    - acdprobe
    - libpostgresql9.1-jdbc3-acd-java
    - libpostgis-1.5-acd-java
    - 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 :slight_smile:

Serge

"tomcat_libs.{{ tomcat_version }} "

You can’t build variable names dynamically.

​Obviously. But somehow this results in some kind of empty list when used
in with_items.

Where’s my ticket? :slight_smile:

​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​

​HTH,

Serge​

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.

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