Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - What's the proper syntax for loop + zip when combining more than two lists?

Tags:

loops

ansible

I haven't been able to find the syntax for loop + zip when combining more than 2 lists.

Since Ansible 2.5, as shown here, the following syntax replaces with_together with loop + zip:

- name: with_together
  debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  with_together:
    - "{{ list_one }}"
    - "{{ list_two }}"

- name: with_together -> loop
  debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ list_one|zip(list_two)|list }}"

My question is, whereas when using with_together, you could simply append lists, and reference them with iterating numbers, I haven't been able to find the method to use with loop + zip. I have tried:

loop: "{{ list_one|zip(list_two)|zip(list_three)|zip(list_four)list }}"

Without success.

like image 341
Travis Avatar asked Mar 28 '19 21:03

Travis


People also ask

Can we loop over two parallel sets in Ansible?

Ansible's syntax also supports the idea of nested looping. Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task.

What is {{ item }} Ansible?

item is not a command, but a variable automatically created and populated by Ansible in tasks which use loops. In the following example: - debug: msg: "{{ item }}" with_items: - first - second. the task will be run twice: first time with the variable item set to first , the second time with second .

Which loop can be used to iterate over files in a directory Ansible?

Ansible offers the loop , with_<lookup> , and until keywords to execute a task multiple times.

How does loop work in Ansible?

The loop keyword executes the same task multiple times. It stores the value of each item in a variable called item . So, instead of specifying the names of the users to be added, simply specify a variable called item enclosed between double curly braces as shown below.


1 Answers

You can append additional arrays inside the zip filter itself.

zip(list, list, list, ...)

For example:

- hosts: localhost
  become: false
  gather_facts: false
  tasks:
  - vars:
      list_one:
      - one
      - two
      list_two:
      - three
      - four
      list_three:
      - five
      - six
    debug:
      msg: "{{ item.0 }} {{ item.1 }} {{ item.2 }}"
    loop: "{{ list_one | zip(list_two, list_three) | list }}"

When run:

PLAY [localhost] *********************************************************************************************************************************************

TASK [debug] *************************************************************************************************************************************************
ok: [localhost] => (item=['one', 'three', 'five']) => {
    "msg": "one three five"
}
ok: [localhost] => (item=['two', 'four', 'six']) => {
    "msg": "two four six"
}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0  
like image 69
Nick Avatar answered Nov 15 '22 10:11

Nick