ansible/jinja2获得唯一的子元素 [英] ansible/jinja2 get unique subelements
问题描述
我有一个这样的列表:
host_depends:
- host: abc
depends:
- name: item1
- name: item4
type: asdf
- name: item6
- host: def
depends:
- name: item2
- name: item4
- name: item6
我需要遍历depends
元素的唯一名称,因此在此示例中,我要遍历
I need to loop over the unique name of the depends
elemnents, so in this example I want to loop over
- item1
- item2
- item4
- item6
基本上是什么
debug: var=item.1.name
with_subelements:
- "{{ host_depends }}"
- depends
可以,但仅包含唯一元素.
does, but with unique elements only.
如何获取所有host_depends
项目的depends
,以便可以对它们运行unique
过滤器并将其与with_items
一起使用?
How can I get the depends
of all host_depends
items so I can run a unique
filter over them and use them with with_items
?
我设法获得所有depends
个项目的列表,如下所示:
I manage to get a list of all depends
items like this:
host_depends|map(attribute='depends')|list
但是从那以后,我无法将此列表简化为name
个项目.
But from there, I fail to reduce this list to the name
items.
推荐答案
host_depends|map(attribute='depends')|list
返回列表列表,取决于列表.要将列表列表展平/组合为一个列表,请使用内置的展平查找:
returns a list of lists, as depends is a list. To flatten/combine this list of lists into one list, use the builtin flatten lookup:
lookup('flattened', host_depends|map(attribute='depends')) |map(attribute='name')|unique|list
这篇关于ansible/jinja2获得唯一的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!