# How can I return a list from a dict based on a sub items value?

**URL:** <https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [August 7, 2019, 6:07pm UTC](https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980 "2019-08-07T18:07:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [August 7, 2019, 6:07pm UTC](https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980/1 "2019-08-07T18:07:33Z")

</div>

How can I return a list from a dict based on a sub items value?

If I have a dictionary like this:

> ospackages:  
> fail2ban:  
> state: “present”  
> Ubuntu:  
> 1604: “fail2ban”  
> 1804: “fail2ban”  
> Debian:  
> 9: “fail2ban”  
> CentOS:  
> 7: “fail2ban”  
> OracleLinux:  
> 7: “fail2ban”  
> curl:  
> state: “present”  
> Ubuntu:  
> 1604: “curl”  
> 1804: “curl”  
> Debian:  
> 9: “curl”  
> CentOS:  
> 7: “curl”  
> OracleLinux:  
> 7: “curl”  
> locate:  
> state: “absent”  
> Ubuntu:  
> 1604: “mlocate”  
> 1804: “mlocate”  
> Debian:  
> 9: “mlocate”  
> CentOS:  
> 7: “mlocate”  
> OracleLinux:  
> 7: “mlocate”  
> ftp:  
> state: “absent”  
> Ubuntu:  
> 1604: “ftp”  
> 1804: “ftp”  
> Debian:  
> 9: “ftp”  
> CentOS:  
> 7: “ftp”  
> OracleLinux:  
> 7: “ftp”

I’d like select only the items from that dictionary based on the value of “state”, and/or if a specific sub-dict has values.

I initially thought of this as a lookup plugin.

Something like:

`lookup('dict_cond', ospackages, 'item.value.state == "present" and item.value.Ubuntu is defined and item.value.Ubuntu.1804 is defined', item.value.Ubuntu.1804)`

would return:

`[fail2ban, curl]`

Use would be something like:

`

- name: install Ubuntu 18.04 packages  
apt:  
pkg: “{{ lookup(‘dict\_cond’, ospackages, ‘item.value.state == “present” and item.value.Ubuntu is defined and item.value.Ubuntu.1804 is defined’, item.value.Ubuntu.1804) }}”  
state: “present”  
update\_cache: “yes”  
cache\_valid\_time: “14400”

- name: yum - Install packages on CentOS 7  
yum:  
name: “{{ lookup(‘dict\_cond’, ospackages, ‘item.value.state == “present” and item.value.CentOS is defined and item.value.CentOS.7 is defined’, item.value.CentOS.7) }}”  
state: “present”  
update\_cache: True  
`

I took a look at plugins/lookup/dict.py and realized that I just don’t know enough Python to make a plugin myself.

Is there a way to do this with current plugins/modules/filters?

Any other ideas on how to get the same general result?

Would this be something to request as a new plugin via GitHub?

I’d be willing to try and code it myself if someone is willing to work with me. As long as you are willing to deal with newbie python questions. (I do know how to program, just not in Python.)

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [August 7, 2019, 6:44pm UTC](https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980/2 "2019-08-07T18:44:39Z")

</div>

I should note that I have not yet tried using a task to filter through the dictionary, and register the result to a variable or maybe use set\_fact in some manner.

I’d prefer not to get that complicated in the playbooks. Hence why I think a lookup plugin would be handy.

Plus, I really really dislike tasks that always say they’ve changed when running a play. I prefer it to stay idempotent as possible.

---

<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 7, 2019, 6:58pm UTC](https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980/3 "2019-08-07T18:58:17Z")

</div>

I would not recommend using dict in this matter, a easier solution is to use include\_vars  
[https://docs.ansible.com/ansible/latest/modules/include\_vars\_module.html](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html)

If you still wanna use dist it will look something like this

&nbsp;&nbsp;{{ ospackages | dict2items | selectattr('value.state', 'eq', 'present') | list | map(attribute='value.Ubuntu.1804') | list }}

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [August 7, 2019, 8:59pm UTC](https://forum.ansible.com/t/how-can-i-return-a-list-from-a-dict-based-on-a-sub-items-value/30980/4 "2019-08-07T20:59:34Z")

</div>

Thanks! That got me to where I wanted.

- David
