Ansible - 组合两个以上列表时循环 + zip 的正确语法是什么?

Tra*_*vis 5 loops ansible

组合超过 2 个列表时,我无法找到 loop + zip 的语法。

由于Ansible 2.5,如图这里,下面的语法内容替换with_together带环+ 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 }}"
Run Code Online (Sandbox Code Playgroud)

我的问题是,虽然在使用 with_together 时,您可以简单地附加列表,并用迭代数字引用它们,但我无法找到与循环 + zip 一起使用的方法。我试过了:

loop: "{{ list_one|zip(list_two)|zip(list_three)|zip(list_four)list }}"
Run Code Online (Sandbox Code Playgroud)

没有成功。

Nic*_*ick 6

您可以在 zip 过滤器本身内附加其他数组。

zip(list, list, list, ...)
Run Code Online (Sandbox Code Playgroud)

例如:

- 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 }}"
Run Code Online (Sandbox Code Playgroud)

运行时:

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  
Run Code Online (Sandbox Code Playgroud)