# How to filter dictionary entries using Jinja

**URL:** <https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325>\
**Category:** Ansible Project\
**Created:** [April 26, 2017, 2:20pm UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325 "2017-04-26T14:20:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [April 26, 2017, 2:20pm UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325/1 "2017-04-26T14:20:48Z")

</div>

Dear all,

I'm at a loss as to how to filter entries fom a dictionry based von specific host variables. In the concrete case we have a long list of possible NFS mounts which should be applied according to the hosts' class.

Our hostlist would be like:

host1 class=cluster  
host2 class=cluster  
host3 class=server

our dictionary of possible mounts would e.g. be:

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [April 27, 2017, 7:17am UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325/2 "2017-04-27T07:17:55Z")

</div>

If each mount was managed by a role, you could just apply those  
roles to the (groups holding) the relevant servers.

You'll need to have multiple data structures, one per role, but that's  
probably going to 'work with the grain' of Ansible better.

---

<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:** [April 27, 2017, 10:40am UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325/3 "2017-04-27T10:40:58Z")

</div>

when: class in item.value.class

---

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [April 28, 2017, 6:10pm UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325/4 "2017-04-28T18:10:20Z")

</div>

I haven't used roles yet. If a host can assume several roles, then this could work. Otherwise I'll have to deal with redundant dictionaries, which is exactly what I want to avoid.

Thanks for the hint

frank

---

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [April 28, 2017, 6:11pm UTC](https://forum.ansible.com/t/how-to-filter-dictionary-entries-using-jinja/24325/5 "2017-04-28T18:11:15Z")

</div>

Wow, so simple? 🙂 Thanks a lot.  
frank
