with_items: two YAML lists

Hi

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?

Thanks
Edgars

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:

    with_items:
      - somelist
      - otherlist

This will do what you want, I believe.

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:

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

Thanks, with_flattened works… but where is it documented??

Edgars

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

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

“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?

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

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 :slight_smile:

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