合并两个以上的列表时,我无法找到loop + zip的语法。
自Ansible 2.5开始,如here所示,以下语法将loop_zip替换为with_together:
- 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 }}"
我的问题是,当使用with_together时,您可以简单地追加列表,并用迭代数字引用它们,但我无法找到用于loop + zip的方法。我尝试过:
loop: "{{ list_one|zip(list_two)|zip(list_three)|zip(list_four)list }}"
没有成功。
答案 0 :(得分:2)
您可以在zip过滤器本身内附加其他数组。
zip(list, list, list, ...)
例如:
- 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 }}"
运行时:
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