Ansible template - Extract value from Dict

Hello

I was not able to figure out how to access the ‘vlan_name’ wthin the jinja2 template.

I’ve already tried a numerous filters, map, selectattr, … but none of them seems to work.

I could extract the key value items with a for loop but there must be another way to just select the vlan_name.

{% for key, val in vlan_defs.vlan_defines[vl].items() %}
key : {{ key }}
value: {{ val }}
{% endfor %}

vlan 17
name {‘vlan_name’: ‘SRV’, ‘vlan_state’: ‘present’}
!
key : vlan_name
value: P-MOBILE
key : vlan_state
value: present

vlan 18
name {‘vlan_name’: ‘P-MOBILE’, ‘vlan_state’: ‘present’}
!
key : vlan_name
value: P-GUEST
key : vlan_state
value: present

I was not able to figure out how to access the 'vlan_name' wthin the jinja2
template.

I've already tried a numerous filters, map, selectattr, ... but none of
them seems to work.

<snip />

-------------------------
The following template is working but writes out the dictionary:

{% for vl in switch_vlans %}
vlan {{ vl }}
name {{ vlan_defs.vlan_defines[vl] }}
!
{% endfor %}

RESULT:

vlan 17
  name {'vlan_name': 'SRV', 'vlan_state': 'present'}
!
vlan 18
  name {'vlan_name': 'P-MOBILE', 'vlan_state': 'present'}
!

--------------------------
Template leads to error:

{% for vl in switch_vlans %}
vlan {{ vl }}
name {{ vlan_defs.vlan_defines[vl].vlan_name }}
!
{% endfor %}

With the template this should work

  {% for vl in vlan_defs.vlan_defines %}
  vlan {{ vl }}
  name {{ vlan_defs.vlan_defines[vl].vlan_name }}
  !
  {% endfor %}

Using map you'll need to convert it to a list first, with Ansible 2.6 you have dict2items

  {{ vlan_defs.vlan_defines | dict2items | map(attribute='value.vlan_name') | list }}

This will return vlan_name in a list