我想使用ansible创建字典列表。我可以创建词典,但无法将它们保存在列表中。
- name: Create list of dictionaries
set_fact:
file_set: "{{ file_set|default({}) | combine({ item.1:lookup('ini', '{{ item.1 }} section={{ item.0 }} file={{ properties_file }}') }) }}"
with_nested:
- [ "section1", "section2" ]
- [ "key1", "key2", "key3" ]
section_names-是ini文件properties_file
中的节列表
例如,属性文件
[section1]
key1=value1
key2=value2
key3=value3
[section2]
key1=value4
key2=value5
key3=value6
file_set
是仅包含section2
值的字典。我想创建词典列表。
将字典放入列表"[]"中,并连接"+"列表。例如
file_set: "{{ file_set|default([]) + [{item.1: lookup( ... )}] }}"
下面的任务
- set_fact:
file_set: "{{ file_set|default([]) +
[{item.1: lookup('ini', item.1 ~
' section=' ~ item.0 ~
' file=properties_file')}] }}"
with_nested:
- [section1, section2]
- [key1, key2, key3]
- debug:
var: file_set
给出
file_set:
- key1: value1
- key2: value2
- key3: value3
- key1: value4
- key2: value5
- key3: value6