我们有一个像这样的任务
- name: Migrate Zookeeper settings
zoo_import:
version: "{{ item[0] }}"
content: "{{ item[1] }}"
with_items: "{{ zk_import | dictsort }}"
zoo_import模块需要sting版本和dict内容,我猜dictsort会生成元组列表。
那么,如何将列表项传递给模块呢?最明显的变体内容:{{dict(item[1](}}以"字典更新序列元素#0的长度为1;需要2"结尾
谢谢。
PS如果它很重要,排序前的zk_import字典就像
zk_import:
v20200420:
to_update:
'/path1/key1/': 'value2'
'/path2/key1/': 'other value'
to_delete:
'/path/key/': 'value2'
'/path/key1/subkey': 'other value'
v20200425:
etc...
在Ansible>=2.5中,应该使用loop
而不是with_items
:
loop: "{{ zk_import | dictsort }}"
在Ansible中<=2.4,您需要使用:
with_items:
- "{{ zk_import | dictsort }}"
这是with_items
的一种特殊行为,已记录在案:
请注意,
with_items
会使其提供的列表的第一个深度变平,如果您传递由列表组成的列表,则可能会产生意外的结果。你可以通过将你的嵌套列表包装在一个列表中来解决这个问题:# This will run debug once with the three items - debug: msg: "{{ item }}" vars: nested_list: - - one - two - three with_items: - "{{ nested_list }}"