Ansible/Jinja2 - 在列表中映射嵌套键 [英] Ansible/Jinja2 - Map nested key in list

查看:24
本文介绍了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

注意 sum(start=[]) – 用于扁平化列表列表.

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

附言为什么将可能的(?)多个路径加入字符串?

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

P.P.S 你的数据结构看起来很奇怪.为什么要定义像list这样的tcp属性,而不仅仅是:

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天全站免登陆