# What approach should I take with this loop?

**URL:** <https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663>\
**Category:** Ansible Project\
**Created:** [November 14, 2019, 7:41pm UTC](https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663 "2019-11-14T19:41:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 14, 2019, 7:41pm UTC](https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663/1 "2019-11-14T19:41:51Z")

</div>

The following fails for sure, but you get the idea of what I am trying to do. How might I be able to loop this? Problem is with ‘searchme’ and probably trying to do something you can’t do

`

- name: Set fact based on host  
set\_fact:  
log\_source: “{{ item.src }}”  
when: “ansible\_hostname is search({{item.searchme}})”  
with\_items:
- { src: “/mnt/host1/error.log”, searchme: host1 }
- { src: “/mnt/var/log/httpd/other.log”, searchme: host2 }
- { src: “/mnt/host3/error.log”, searchme: host3 }

`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 14, 2019, 8:59pm UTC](https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663/2 "2019-11-14T20:59:01Z")

</div>

It's possible to use "JSON Query Filter"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#json-query-filter](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter)

For example the play below

&nbsp;&nbsp;&nbsp;&nbsp;- hosts: my\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {src: "/mnt/host1/error.log", searchme: "host1"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {src: "/mnt/var/log/httpd/other.log", searchme: "host2"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {src: "/mnt/host3/error.log", searchme: "host3"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log\_source: "{{ my\_list|json\_query(query)|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: "[?searchme=='{{ inventory\_hostname }}'].src"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: log\_source

gives

&nbsp;&nbsp;&nbsp;&nbsp;ok: [host1] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"log\_source": "/mnt/host1/error.log"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;ok: [host2] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"log\_source": "/mnt/var/log/httpd/other.log"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;ok: [host3] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"log\_source": "/mnt/host3/error.log"  
&nbsp;&nbsp;&nbsp;&nbsp;}

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 14, 2019, 10:15pm UTC](https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663/3 "2019-11-14T22:15:50Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [November 15, 2019, 5:00pm UTC](https://forum.ansible.com/t/what-approach-should-i-take-with-this-loop/31663/4 "2019-11-15T17:00:37Z")

</div>

Just change your when from:  
`  
when: “ansible\_hostname is search({{item.searchme}})”

`

to:

`when: inventory_hostname in item.searchme`
