# Finding key in list of dicts in when

**URL:** <https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579>\
**Category:** Ansible Project\
**Created:** [May 26, 2016, 1:03pm UTC](https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579 "2016-05-26T13:03:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [May 26, 2016, 1:03pm UTC](https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579/1 "2016-05-26T13:03:42Z")

</div>

Hi,

Related to the recent mount deprecation thread:

- command: mount /srv  
when: “‘/srv’ not in [x[‘mount’] for x in ansible\_mounts]”

But it seems that when statement is too complex. Any ideas how to express that in a valid way?

Ideas about how to use mount: state=mounted without touching fstab also welcome

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [May 30, 2016, 7:19am UTC](https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579/2 "2016-05-30T07:19:22Z")

</div>

> But it seems that when statement is too complex. Any ideas how to express that in a valid way?

I ended up writing a filter that lets me do: when: not ansible\_mounts|is\_mounted(‘/srv’)

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Daniel\_Nagele](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Daniel\_Nagele](https://forum.ansible.com/u/Daniel_Nagele)\
**Post date:** [May 30, 2016, 7:46am UTC](https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579/3 "2016-05-30T07:46:04Z")

</div>

I've had some success using the following expression (to check wether  
ansible is in a list of installed packages or if it is from pip/manual  
install):

"{{ 'ansible' in pkgs.results|default()|map(attribute='name') }}"

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [May 31, 2016, 7:49am UTC](https://forum.ansible.com/t/finding-key-in-list-of-dicts-in-when/21579/4 "2016-05-31T07:49:51Z")

</div>

> “{{ ‘ansible’ in pkgs.results|default()|map(attribute=‘name’) }}”

Thank you Daniel, that avoids the heavy-handed approach of a filter plugin.

Cheers,  
Paul
