ansible/jinja2获得唯一的子元素 [英] ansible/jinja2 get unique subelements

查看:60
本文介绍了ansible/jinja2获得唯一的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的列表:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆