# Ansible template - Extract value from Dict

**URL:** <https://forum.ansible.com/t/ansible-template-extract-value-from-dict/28352>\
**Category:** Ansible Project\
**Created:** [August 17, 2018, 6:34am UTC](https://forum.ansible.com/t/ansible-template-extract-value-from-dict/28352 "2018-08-17T06:34:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![j1f0x](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@j1f0x](https://forum.ansible.com/u/j1f0x)\
**Post date:** [August 17, 2018, 6:34am UTC](https://forum.ansible.com/t/ansible-template-extract-value-from-dict/28352/1 "2018-08-17T06:34:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 17, 2018, 7:49am UTC](https://forum.ansible.com/t/ansible-template-extract-value-from-dict/28352/2 "2018-08-17T07:49:41Z")

</div>

> 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  
> &nbsp;&nbsp;name {'vlan\_name': 'SRV', 'vlan\_state': 'present'}  
> !  
> vlan 18  
> &nbsp;&nbsp;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

&nbsp;&nbsp;{% for vl in vlan\_defs.vlan\_defines %}  
&nbsp;&nbsp;vlan {{ vl }}  
&nbsp;&nbsp;name {{ vlan\_defs.vlan\_defines[vl].vlan\_name }}  
&nbsp;&nbsp;!  
&nbsp;&nbsp;{% endfor %}

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

&nbsp;&nbsp;{{ vlan\_defs.vlan\_defines | dict2items | map(attribute='value.vlan\_name') | list }}

This will return vlan\_name in a list
