合并两个以上的列表时,我无法找到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 }}"
我的问题是,当使用with_together时,您可以简单地追加列表,并使用迭代数字引用它们,但我无法找到用于loop + zip的方法。我试过了:
loop: "{{ list_one|zip(list_two)|zip(list_three)|zip(list_four)list }}"
没有成功。
您可以在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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句