with_items: two YAML lists

13,521 views
Skip to first unread message

Edgars

unread,
Oct 1, 2013, 8:08:08 AM10/1/13
to ansible...@googlegroups.com
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

C. Morgan Hamill

unread,
Oct 1, 2013, 8:25:31 AM10/1/13
to ansible-project
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.
--
Morgan Hamill

Edgars

unread,
Oct 1, 2013, 8:31:54 AM10/1/13
to ansible...@googlegroups.com
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

Serge van Ginderachter

unread,
Oct 1, 2013, 8:38:44 AM10/1/13
to ansible...@googlegroups.com

On 1 October 2013 14:31, Edgars <edgars...@gmail.com> wrote:
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

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

Edgars

unread,
Oct 1, 2013, 9:12:13 AM10/1/13
to ansible...@googlegroups.com
Thanks, with_flattened works.. but where is it documented??

Edgars

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

Nick

unread,
Oct 1, 2013, 9:14:37 AM10/1/13
to ansible...@googlegroups.com
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:

  apache_vhost:
    id: whatever
    server_name: whatever.com
    server_aliases:
      - list
      - of
      - aliases
    state: present

One can concatenate lists like this (the last line is what I mean):

  - name: Ensure virtual hostname is (un)defined in /etc/hosts
    action: lineinfile
            backup=yes
            dest=/etc/hosts
            regexp="^{{ansible_default_ipv4.address}}.*\s{{item}}"
            line="{{ansible_default_ipv4.address}}    {{item}}"
            state="{{ 'absent' if apache_vhost.state == 'absent' else 'present' }}"
    with_items: apache_vhost.server_aliases + [apache_vhost.server_name]

So in your case you might consider:

  with_items: list1 + list2 + list3


Cheers,

N

Michael DeHaan

unread,
Oct 1, 2013, 9:31:10 AM10/1/13
to ansible...@googlegroups.com
"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?





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nick

unread,
Oct 1, 2013, 10:27:49 AM10/1/13
to ansible...@googlegroups.com
On 01/10/13 14:31, Michael DeHaan wrote:
"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

Michael DeHaan

unread,
Oct 1, 2013, 11:50:39 AM10/1/13
to ansible...@googlegroups.com
  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 :)





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Nick

unread,
Oct 1, 2013, 12:22:16 PM10/1/13
to ansible...@googlegroups.com
On 01/10/13 16:50, Michael DeHaan wrote:

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 :)
Sorry, yes, just bad choice of name for the illustration.


  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)

Which implies we can't do the following then?

  vars:
    elem:
      alpha: one
      beta: two

  tasks:
   ...
       with_items:
         - list
         - [ elem ]


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:

        with_items: list + [elem]

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:

  vars:
    elem:
      - alpha
      - beta

  tasks:
   ...
       with_flattened:
         - list
         - [ elem ]

Or would "with_flattened" flatten elem as well, giving the same as the following?

       with_items: list + [alpha] + [beta]


Cheers,

N
Reply all
Reply to author
Forward
0 new messages