基本上与@vladimir 的答案相同,但不需要循环:
- name: manipulate dict
hosts: localhost
gather_facts: false
vars:
# Your var definition on a single line for legibility
test_list_of_dicts: [{"inner_dict":[{"f1":"a","f2":"b"},{"f1":"c","f2":"d"}],"id":"id1"},{"inner_dict":[{"f1":"e","f2":"f"},{"f1":"g","f2":"h"}],"id":"id2"}]
new_list_of_dicts: "{{ lookup('subelements', test_list_of_dicts, 'inner_dict')
| map('combine') | list }}"
tasks:
- name: Show result
debug:
var: new_list_of_dicts
同样给予:
PLAY [manipulate dict] *****************************************************************************************************************************************************************************************************************
TASK [show result] *********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"new_list_of_dicts": [
{
"f1": "a",
"f2": "b",
"id": "id1"
},
{
"f1": "c",
"f2": "d",
"id": "id1"
},
{
"f1": "e",
"f2": "f",
"id": "id2"
},
{
"f1": "g",
"f2": "h",
"id": "id2"
}
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0