# with\_items collapses lists

**URL:** <https://forum.ansible.com/t/with-items-collapses-lists/13487>\
**Category:** Ansible Project\
**Created:** [September 16, 2013, 5:46pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487 "2013-09-16T17:46:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [September 16, 2013, 5:46pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487/1 "2013-09-16T17:46:19Z")

</div>

Running HEAD as of several minutes ago:

&nbsp;&nbsp;&nbsp;&nbsp;- hosts: foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg={{item}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- [foo, hey]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- [doo, noway]

Prints:

&nbsp;&nbsp;&nbsp;&nbsp;ok: [foo] =\> (item=foo) =\> {"item": "foo", "msg": "foo"}  
&nbsp;&nbsp;&nbsp;&nbsp;ok: [foo] =\> (item=hey) =\> {"item": "hey", "msg": "hey"}  
&nbsp;&nbsp;&nbsp;&nbsp;ok: [foo] =\> (item=doo) =\> {"item": "doo", "msg": "doo"}  
&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<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:** [September 16, 2013, 6:00pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487/2 "2013-09-16T18:00:38Z")

</div>

​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

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [September 16, 2013, 6:35pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487/3 "2013-09-16T18:35:22Z")

</div>

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?

---

<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:** [September 16, 2013, 6:43pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487/4 "2013-09-16T18:43:13Z")

</div>

​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

---

<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:** [September 16, 2013, 9:21pm UTC](https://forum.ansible.com/t/with-items-collapses-lists/13487/5 "2013-09-16T21:21:47Z")

</div>

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
