如何在 ansible 中使用 jinja2 过滤器提取和构建字典项列表
How to extract and build list of dictionary items using jinja2 filter in ansible
我有这样的东西:
数据:
---
modules:
- name: m1
migrations:
- name: abc
attr: testabc
- name: def
attr: testdef
- name: m2
migrations:
- name: ghi
attr: testabc
- name: m3
migrations: []
Ansible 任务:
- name: All migrations
set_fact:
migrations: "{{modules|map(attribute='migrations')|list }}"
输出:
我看到的输出是这样的:
[
[
{
name: abc
attr: testabc
},
{
name: def
attr: testdef
}
],
[
{
name: ghi
attr: testabc
}
],
[]
]
我需要的是:
[
{
name: abc
attr: testabc
},
{
name: def
attr: testdef
},
{
name: ghi
attr: testabc
}
]
这可能对我设法获得字典列表的人有帮助:
- name: All migrations
set_fact:
migrations: "{% set migrations = migrations|default([]) + [item.1] %}{{migrations|list}}"
with_subelements:
- "{{ modules }}"
- migrations
可能会有更短的版本,但我还不知道!
我有这样的东西:
数据:
---
modules:
- name: m1
migrations:
- name: abc
attr: testabc
- name: def
attr: testdef
- name: m2
migrations:
- name: ghi
attr: testabc
- name: m3
migrations: []
Ansible 任务:
- name: All migrations
set_fact:
migrations: "{{modules|map(attribute='migrations')|list }}"
输出:
我看到的输出是这样的:
[
[
{
name: abc
attr: testabc
},
{
name: def
attr: testdef
}
],
[
{
name: ghi
attr: testabc
}
],
[]
]
我需要的是:
[
{
name: abc
attr: testabc
},
{
name: def
attr: testdef
},
{
name: ghi
attr: testabc
}
]
这可能对我设法获得字典列表的人有帮助:
- name: All migrations
set_fact:
migrations: "{% set migrations = migrations|default([]) + [item.1] %}{{migrations|list}}"
with_subelements:
- "{{ modules }}"
- migrations
可能会有更短的版本,但我还不知道!