Ansible/Jinja2-列表中的地图嵌套键 [英] Ansible/Jinja2 - Map nested key in list

查看:69
本文介绍了Ansible/Jinja2-列表中的地图嵌套键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌套变量列表中映射属性时,我无法检索其键.

When map'ing a attribute in a list of nested variables, I am not able to retrieve its key.

我想从以下强调文本变量中检索"tls_cert_file"的键:

I want to retrieve the key of "tls_cert_file" from followingemphasized text variables:

vault_config_listener:
  - tcp:
    - address: "0.0.0.0:8200"
    - tls_cert_file: "/etc/ssl/wildcard.crt"
    - tls_key_file: "/etc/ssl/private/wildcard.key"
    - tls_require_and_verify_client_cert: "false"
  - tcp:
    - address: "127.0.0.1:8200"
    - tls_disable: true

调试任务:

- debug:
    msg: "{{ (vault_config_listener | selectattr('tcp', 'defined') | map(attribute='tcp')) | selectattr('tls_cert_file','defined') | map(attribute='tls_cert_file') | join('') | dirname }}"

输出:

ok: [test] => {
    "msg": ""
}

在"tcp"之前,我一直在运行地图,但是没有其他...逻辑上有什么问题吗?

I got the map'ing working until "tcp", but no further... What is wrong at the logic?

推荐答案

要获取tls_cert_file的列表,可以使用

To get a list of tls_cert_file you can use

vault_config_listener | selectattr('tcp', 'defined') | map(attribute='tcp') | sum(start=[]) | selectattr('tls_cert_file','defined') | map(attribute='tls_cert_file') | list

note sum(start=[]) –用于展平列表列表.

note sum(start=[]) – it is used to flatten list of lists.

P.S.为什么将多个可能的路径连接成字符串?

P.S. Why do you join possible(?) multiple paths into string?

P.P.S您的数据结构似乎很奇怪.为什么要定义tcp属性(例如list),而不仅仅是:

P.P.S Your data structure seems rather weird. Why do you define tcp properties like list, and not just:

tcp:
  address: 0.0.0.0:8200
  tls_cert_file: /etc/ssl/wildcard.crt
  tls_key_file: /etc/ssl/private/wildcard.key
  tls_require_and_verify_client_cert: false

这篇关于Ansible/Jinja2-列表中的地图嵌套键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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