# Jinja2 extract single value for key/value pair without looping

**URL:** <https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561>\
**Category:** Ansible Project\
**Created:** [June 27, 2014, 8:27am UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561 "2014-06-27T08:27:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [June 27, 2014, 8:27am UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/1 "2014-06-27T08:27:10Z")

</div>

I have the following defined in vars or defaults:

fusemq\_authentication:

- { ‘username’:‘admin’, 'password’:‘blah’, ‘groups’:‘admin’ }
- { ‘username’:‘LOGSTASH’, 'password’:‘bing’, ‘groups’:‘LOGSTASH’ }

I want to know the password for LOGSTASH in a template. Today I loop until I find it. Is there a simpler way?

{% for value in fusemq\_authentication %}  
{% if value.username==“LOGSTASH” %}  
fuseMQ\_password={{ value.password }}  
{% endif %}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 27, 2014, 9:14am UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/2 "2014-06-27T09:14:39Z")

</div>

You could transform your data definition like this: Then in a template you could access the password for ‘logstash’ user like this: or

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [June 27, 2014, 9:23am UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/3 "2014-06-27T09:23:56Z")

</div>

jepper \<jespmark@gmail.com\> napisał:

> I have the following defined in vars or defaults:
> 
> fusemq\_authentication:  
> - { 'username':'admin', 'password’:'blah', 'groups':'admin'  
> }  
> - { 'username':'LOGSTASH', 'password’:'bing', 'groups':'LOGSTASH' }

This should be a dictionary

> I want to know the password for LOGSTASH in a template. Today I loop  
> until  
> I find it. Is there a simpler way?
> 
> {% for value in fusemq\_authentication %}  
> {% if value.username=="LOGSTASH" %}  
> fuseMQ\_password={{ value.password }}  
> {% endif %}  
> {% endfor %}

Yes, with fusemq\_authentication as a dict it would be:  
&nbsp;&nbsp;&nbsp;&nbsp;fuseMQ\_password={{ fusemq\_authentication. LOGSTASH.password }}

---

<div class="post-metadata">

**Author:** ![kesten\_broughton1](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kesten\_broughton1](https://forum.ansible.com/u/kesten_broughton1)\
**Post date:** [June 27, 2014, 6:13pm UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/4 "2014-06-27T18:13:03Z")

</div>

I’ve hit this before,

The problem is, sometimes it’s nice to have a list to loop over especially if the client code doesn’t know the keys.  
Other times it would be nice to have a dict if you have the key since writing loops can be messy.

I think it would be very nice in these cases to have a dict\_to\_list adapter and list\_to\_dict adapter as jinja2 plugins, so the original datastructure can be whichever.

I got started on this but never finished.  
[https://gist.github.com/7a2109be1f0006bae89a.git](https://gist.github.com/7a2109be1f0006bae89a.git)

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [June 27, 2014, 6:18pm UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/5 "2014-06-27T18:18:19Z")

</div>

​You can already easily use a dict as a list:

dict.keys()  
dict.values()  
dict.items()

​

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [June 27, 2014, 8:13pm UTC](https://forum.ansible.com/t/jinja2-extract-single-value-for-key-value-pair-without-looping/15561/6 "2014-06-27T20:13:42Z")

</div>

Theoretically in Jinja 2.8 when it's released you could perform this operation with something like this:

{{ fusemq\_auth|selectattr('username','equalto','LOGSTASH')|map(attribute='password')|first }}

selectattr and map are new to version 2.7. The 'equalto' test is the problem you'll run in to.

So the map function with an optional selectattr provides the dict\_to\_list function you are looking for. (Your gist wouldn't load.) I don't see how a general purpose list\_to\_dict would be possible without some specific logic be written.

\<tim/\>
