# with\_items: two YAML lists

**URL:** <https://forum.ansible.com/t/with-items-two-yaml-lists/13586>\
**Category:** Ansible Project\
**Created:** [October 1, 2013, 12:08pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586 "2013-10-01T12:08:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [October 1, 2013, 12:08pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/1 "2013-10-01T12:08:08Z")

</div>

Hi

As per documentation, with\_items supports yaml or vars list:

```auto
with_items: somelist

```

What is syntax if I want to include two lists for with\_items?

Thanks  
Edgars

---

<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:** [October 1, 2013, 12:25pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/2 "2013-10-01T12:25:31Z")

</div>

Excerpts from Edgars's message of 2013-10-01 08:08:08 -0400:

> As per documentation, with\_items supports yaml or vars list:
> 
> with\_items: somelist
> 
> What is syntax if I want to include two lists for with\_items?

'with\_items' collapses the first level of lists into one big list:

&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- somelist  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- otherlist

This will do what you want, I believe.

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [October 1, 2013, 12:31pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/3 "2013-10-01T12:31:54Z")

</div>

It wont… It will take somelist and otherlist as values for {{ item }} variable. For example, if I do:

user: name={{ item }}  
with\_items:

- somelist
- otherlist

It will create two users: somelist and otherlist

Edgars

otrdiena, 2013. gada 1. oktobris 14:08:08 UTC+2, Edgars rakstīja:

---

<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 1, 2013, 12:38pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/4 "2013-10-01T12:38:44Z")

</div>

​IIRC, with\_items will do that for lists defined without variables (unless  
with\_items: variable)

The with\_flattened plugin will do what you want, suing multiple testing and  
variables.

Serge​

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [October 1, 2013, 1:12pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/5 "2013-10-01T13:12:13Z")

</div>

Thanks, with\_flattened works… but where is it documented??

Edgars

otrdiena, 2013. gada 1. oktobris 14:08:08 UTC+2, Edgars rakstīja:

---

<div class="post-metadata">

**Author:** ![Nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Nick](https://forum.ansible.com/u/Nick)\
**Post date:** [October 1, 2013, 1:14pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/6 "2013-10-01T13:14:37Z")

</div>

I’ve found that sometimes neither with\_items or with\_flattened did what I needed, at least on the face of it. But since with\_items support jinja2 syntax, I noticed there is another way to combine lists. An example snippet from a working playbook. Given vars define like this: One can concatenate lists like this (the last line is what I mean): So in your case you might consider: Cheers, N

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 1, 2013, 1:31pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/7 "2013-10-01T13:31:10Z")

</div>

“I’ve found that sometimes neither with\_items or with\_flattened did what I needed, at least on the face of it.”

Which is what exactly?

---

<div class="post-metadata">

**Author:** ![Nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Nick](https://forum.ansible.com/u/Nick)\
**Post date:** [October 1, 2013, 2:27pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/8 "2013-10-01T14:27:49Z")

</div>

The example I gave is intended as one such case: I have a list and an item I need to combine into a flattened list. I can’t remember all the detail of what I tried and didn’t work for me, however, which is why I was vague. IIRC, I tried with\_items: - list - item with\_items: - list - [item] …and possibly some others more desperate variations involving [{{item}}], [$item] and the like. However, checking the docs I think I am confusing “with\_flattened” for “with\_nested” (since the latter is documented and the former is not). So with\_flattened in fact probably was not one of the things I tried. But the main point I was making was that there is an alternative (Jinja2 expressions) which is implied but explicitly included in the docs. N

---

<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 1, 2013, 3:50pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/9 "2013-10-01T15:50:39Z")

</div>

with\_items:

- list
- [scalar]

Won’t work per se because it won’t know to replace the bareword inside brackets with a variable

with\_items:

- list
- [“{{scalar}}”]

Should be fine (untested)

Note that you should not use the name “item” in for a variable as that’s the name of the loop index, not sure if that was just illustrative or what 🙂

---

<div class="post-metadata">

**Author:** ![Nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Nick](https://forum.ansible.com/u/Nick)\
**Post date:** [October 1, 2013, 4:22pm UTC](https://forum.ansible.com/t/with-items-two-yaml-lists/13586/10 "2013-10-01T16:22:16Z")

</div>

Which implies we can’t do the following then? Unless we actually wanted the the final element to be “{ alpha: ‘one’, beta: ‘two’}”, of course - which is what “{{ elem }}” would give. But this would still work: And with\_flattened would work too, I assume. But what if scalar was a _list_ and we wanted to append it as the final item (as here)? The above would still work, but would “with\_flattened”? In other words, would this work: Or would “with\_flattened” flatten elem as well, giving the same as the following? Cheers, N
