# Find variable in dictonary based on a known key:value pair

**URL:** <https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306>\
**Category:** Ansible Project\
**Created:** [December 19, 2018, 8:44pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306 "2018-12-19T20:44:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 19, 2018, 8:44pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306/1 "2018-12-19T20:44:26Z")

</div>

So having a dictionary in a var file, how would I find a value, if I don’t know the list position, but I do know a value of another key:value pair?

`  
physical\_interface\_list:

- lan: 1  
slave: no  
route\_list:
- ip4: 1.1.1.1  
subnet4: 2.2.2.2  
gateway4: 3.3.3.3  
present: yes  
ip4: 4.4.4.4  
dev\_name: eth1

`

So if know that my variable lives somewhere in

`  
physical\_interface\_list[?][‘ip4’]

`

And I know that the dev\_name is eth1, how would I go about finding ip4 at the correct array position (assuming there are more than the one I am showing)? The variable file I am pulling from is enormous, and I have committed the 6 levels above it for brevity’s sake.

I have tried using map, but I must not understand how to use it correctly, and the documentation I have been reading doesn’t really tell me if it can be used that way…

---

<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:** [December 20, 2018, 1:36pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306/2 "2018-12-20T13:36:58Z")

</div>

{{ (physical\_interface\_list | selectattr('dev\_name', 'eq', 'eth1') | list).0.ip4 }}

---

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 20, 2018, 2:16pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306/3 "2018-12-20T14:16:30Z")

</div>

> Awesome! Thanks so much, that worked for me, but I had to tweak it just a little bit. I tried using ‘eq’ as the filter and it threw an error, but ‘equalto’ didn’t give it any problems. Otherwise this was exactly what I needed!

---

<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:** [December 20, 2018, 2:27pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306/4 "2018-12-20T14:27:22Z")

</div>

eq/equalto is a test not a filter.  
In Jinja 2.10 it's called eq and the alias is equalto, but in older version you only have the name equalto.

---

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 20, 2018, 3:00pm UTC](https://forum.ansible.com/t/find-variable-in-dictonary-based-on-a-known-key-value-pair/29306/5 "2018-12-20T15:00:30Z")

</div>

Gotcha, I need to work on getting my terminology right, still new at this. Thanks again!
