When map'ing a attribute in a list of nested variables, I am not able to retrieve its key.
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
The debug task:
- debug:
msg: "{{ (vault_config_listener | selectattr('tcp', 'defined') | map(attribute='tcp')) | selectattr('tls_cert_file','defined') | map(attribute='tls_cert_file') | join('') | dirname }}"
The output:
ok: [test] => {
"msg": ""
}
I got the map'ing working until "tcp", but no further... What is wrong at the logic?
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=[])
– it is used to flatten list of lists.
P.S. Why do you join possible(?) multiple paths into string?
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