Ansible 如何为包含列表中的with_项创建列表
我有以下清单:Ansible 如何为包含列表中的with_项创建列表,ansible,jinja2,Ansible,Jinja2,我有以下清单: "mylist": [ { "files": [{"path": "path11"}, {"path": "path12"}] }, { "files": [{"path": "path21"}, {"path": "path22"}] } ] 我还需要运行带有_项的角色,其中的项应该是列表中的路径元素 例如: - debug: msg="{{ item }}" with_items: "{{ mylist | some_magic }}" 所需产出: TA
"mylist": [
{ "files": [{"path": "path11"}, {"path": "path12"}] },
{ "files": [{"path": "path21"}, {"path": "path22"}] }
]
我还需要运行带有_项的角色,其中的项应该是列表中的路径元素
例如:
- debug: msg="{{ item }}"
with_items: "{{ mylist | some_magic }}"
所需产出:
TASK [test : debug] **********************************
ok: [host] => (item=path11 ) => {
"msg": "path11"
}
ok: [host] => (item=path12 ) => {
"msg": "path12"
}
ok: [host] => (item=path21 ) => {
"msg": "path21"
}
...
可能吗
这是我已经尝试过的: 结构如下所示:
- debug: msg="{{ item }}"
with_items: "{% for files in mylist | map(attribute='files') %}{% for file in files %}{{file.path}}{% endfor %}{% endfor %}"
- debug: msg="{{ item }}"
with_items: "{{ mylist | map(attribute='files') | map(attribute='path') | list }}"
按预期返回的值不是列表
错误的构造如下所示:
- debug: msg="{{ item }}"
with_items: "{% for files in mylist | map(attribute='files') %}{% for file in files %}{{file.path}}{% endfor %}{% endfor %}"
- debug: msg="{{ item }}"
with_items: "{{ mylist | map(attribute='files') | map(attribute='path') | list }}"
这是一项遗产
使用循环
关键字(Ansible 2.5及更高版本),您可以迭代:
- debug:
msg: "{{ item.1.path }}"
loop: "{{ mylist | subelements('files') }}"
或者使用JMESPath(这显示了如何从整个数据结构中创建列表):
非常感谢。第二种方法对我来说更好,因为屏幕上没有洪水。