Running HEAD as of several minutes ago:
- hosts: foo
tasks:
- debug: msg={{item}}
with_items:
- [foo, hey]
- [doo, noway]
Prints:
ok: [foo] => (item=foo) => {"item": "foo", "msg": "foo"}
ok: [foo] => (item=hey) => {"item": "hey", "msg": "hey"}
ok: [foo] => (item=doo) => {"item": "doo", "msg": "doo"}
ok: [foo] => (item=noway) => {"item": "noway", "msg": "noway"}
Is this expected behavior? I though this is what "with_flattened" is
for and "with_items" was supposed to preserve structures like this.
Actually, it is. While I wrote the flattened plugin, I didn't notice this
behaviour of the items plugin.
The difference between both is that flattened basically goes several levels
deeper, and will template at any level,
while the items plugin only templates something like "with items: var' (on
one line).
If I had realized this behaviour earlier, I might have proposed to
integrate those extra features from flalttened into items; not sure if this
has implications though.
(Possibly unrelated, nut that being said, I'm noticing several lookup
plugins don't template stuff in the same con,sequent way; I have been
wondering if that is a good thing; personally I find this confusing)
Serge
Excerpts from Serge van Ginderachter's message of 2013-09-16 14:00:38 -0400:
Actually, it is. While I wrote the flattened plugin, I didn't notice this
behaviour of the items plugin.
The difference between both is that flattened basically goes several levels
deeper, and will template at any level,
while the items plugin only templates something like "with items: var' (on
one line).
That's...pretty subtle stuff. Certainly bumped up against my intuition.
If it's too late to change, in the sense of maintaining backwards
compatibility, then the workaround of using a list of hashes instead of
a list of lists works fine for me.
If I had realized this behaviour earlier, I might have proposed to
integrate those extra features from flalttened into items; not sure if this
has implications though.
Probably does.
(Possibly unrelated, nut that being said, I'm noticing several lookup
plugins don't template stuff in the same con,sequent way; I have been
wondering if that is a good thing; personally I find this confusing)
Out of curiosity, how do they differ?
Some plugins will automatically resolve complex variables;
some might just template them (and give you a string);
and others will only template if you explicitly use jinja2 notation {{ }}.
It can also happen (resolve) only if you note "with_LOOKUP: variable" on
one line, whilst others might do it within the list items.
Disclaimer: this is my general feeling, I didn't make a full overview of
that, but at least some plugins behave differently in this way. items and
flattened being the best example.
Serge
with_items is meant to walk over lists of lists one item at a time, so you can do things like define a list of two packages and do
yum: name={{item}} state=latest
with_items:
- base_packages
- app_packages
and so on