# Filter a dict with a list in a loop

**URL:** <https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392>\
**Category:** Ansible Project\
**Created:** [January 4, 2021, 4:46pm UTC](https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392 "2021-01-04T16:46:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wilfried\_HUMBERT](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wilfried\_HUMBERT](https://forum.ansible.com/u/Wilfried_HUMBERT)\
**Post date:** [January 4, 2021, 4:46pm UTC](https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392/1 "2021-01-04T16:46:14Z")

</div>

Hi,

I’m writing a playbook with the aim to search for IOC accross all managed hosts.

I’ve got a dict that contains several file as key (like /home/user/foo) with a checksum as value. The dict have been filled by a previous task.

I also have a list that contains well-known checksum.

I would like to parse the dict and compare values with the list, when one value matches, add the key:value pair in a new dict.

At the moment, i failed to get the expected result as the dictionary is overwritten at each iteration of the loop. Can’t find out how to append in it with jinja2.

Here’s the play for the task:

---

<div class="post-metadata">

**Author:** ![Daniel\_Caillibaud](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Daniel\_Caillibaud](https://forum.ansible.com/u/Daniel_Caillibaud)\
**Post date:** [January 4, 2021, 6:00pm UTC](https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392/2 "2021-01-04T18:00:25Z")

</div>

I'm very interested in the solution, I am looking for something similar (loop on hostvars and  
add in a list some hosts depending on theirs vars) !

I guess json\_query filter is the way to do it but I didn't succeed…

I tried to follow [https://jmespath.org/tutorial.html#filter-projections](https://jmespath.org/tutorial.html#filter-projections), to filter hostvars,  
with first a silly condition on inventory\_hostname to get hostvars['foo']

&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ hostvars|json\_query('\*[inventory\_hostname==`foo`].inventory\_hostname') }}"  
=\> empty list

---

<div class="post-metadata">

**Author:** ![Piya\_Attapatto](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@Piya\_Attapatto](https://forum.ansible.com/u/Piya_Attapatto)\
**Post date:** [January 4, 2021, 10:01pm UTC](https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392/3 "2021-01-04T22:01:53Z")

</div>

Hey Will -

As you say, in your script each iteration of the loop **with\_items: “{{ \_checksumToCheck }}”** in your set\_fact task overwrites the **final\_dict** variable.

If I understood correctly what you were trying to do, try changing the **final\_dict** line in in your **set\_fact**

---

<div class="post-metadata">

**Author:** ![Wilfried\_HUMBERT](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wilfried\_HUMBERT](https://forum.ansible.com/u/Wilfried_HUMBERT)\
**Post date:** [January 4, 2021, 10:35pm UTC](https://forum.ansible.com/t/filter-a-dict-with-a-list-in-a-loop/34392/4 "2021-01-04T22:35:15Z")

</div>

Hi Piya !

Your solution works just fine !

I did try combine before, but the syntax was incorrect, this was driving me nuts.

Thanks a lot,

Wilfried.
