我有一本这样的字典:
list_of_dicts:
config:
key1: value1
key2: value2
当我循环字典时,我想组合键/值对。
set_facts:
list_of_dicts: {{ list_of_dicts | default({}) | combine (item.key: item.value) }}
with_dict: "{{ list_of_dicts }}"
我如何获取 item.value.key 的值?
答案1
使用下面的 filter_plugin(来源编码墙)
> cat filter_plugins/hash_utils.py
def hash_to_tuples(h):
return h.items()
def hash_keys(h):
return h.keys()
def hash_values(h):
return h.values()
class FilterModule(object):
''' utility filters for operating on hashes '''
def filters(self):
return {
'hash_to_tuples' : hash_to_tuples
,'hash_keys' : hash_keys
,'hash_values' : hash_values
}
表演
- debug:
msg: "{{ item|hash_keys }}"
loop: "{{ list_of_dicts.values() }}"
给出(删节版):
"msg": [
"key2",
"key1"
]
你可能想尝试hash_to_tuples和hash_values篩選器。