# Is there a better way to search through a list using when and regex\_search

**URL:** <https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916>\
**Category:** Ansible Project\
**Created:** [May 12, 2020, 7:59pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916 "2020-05-12T19:59:20Z")\
**Posts on this page:** 5\
**Page:** 2

<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:** [May 14, 2020, 9:09pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/21 "2020-05-14T21:09:00Z")

</div>

It should create a file on each remote host when used correctly. Quoting from  
your last email today:

&nbsp;&nbsp;"I have something I can work with now..."

&nbsp;&nbsp;- name: search\_snmp  
&nbsp;&nbsp;&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;- name: write output to a file if snmp if found  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: ./{{ansible\_play\_name}}\_device\_list.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

If you use template instead of lineinfile the file shall be created on each  
of the "all" remote hosts. Try absolute path to find the file easier. For  
example

&nbsp;&nbsp;- name: write output to a file if snmp if found  
&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: output.txt.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/tmp/{{ ansible\_play\_name }}\_output.txt"

It's difficult to guess all details and help you without minimal, complete,  
verifiable, example [mcve]. Try for example [https://gist.github.com/](https://gist.github.com/) and post  
the complete code if you have problem. Btw, good catch with the output's  
nested lists.

[mcve] [https://stackoverflow.com/help/minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Scott\_Fella](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Scott\_Fella](https://forum.ansible.com/u/Scott_Fella)\
**Post date:** [May 14, 2020, 10:58pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/22 "2020-05-14T22:58:08Z")

</div>

Vlad,

Thanks for the feedback on [mcve]. Hopefully this is right.

gist: [https://gist.github.com/fella5/6842043ccc35f013c9092c6520dfb593](https://gist.github.com/fella5/6842043ccc35f013c9092c6520dfb593)

**Summary** :  
There are two playbooks that I was working off of. search\_snmp\_v1.yml (jinja template) and search\_snmp\_v2.yml (lineinfile)

**Function of the Playbook** :

1. SSH to each host and run the show snmpcommunity and check if one of the snmp strings match the oldSnmp list

2. output two files

- one file is just a list of host(s) which contains one of the snmp strings in the oldSnmp list (search\_snmp\_device\_list.txt). Host that do not have any matching strings will not be listed on the file.

- one file is the output from all host that contains one of the snmp strings in the oldSnmp list (search\_snmp\_output.txt). Host that do not have any matching strings will not have the output appended to the file.

**Problem Description** :  
The search\_snmp\_v1.yml uses a jinja template. The output get overwritten and I don’t want a file for each host. search\_snmp\_v2.yml uses lineinfile and seems to work for adding the hostname of the device if the snmp string matches one of the listed strings in oldSnmp. The search\_snmp\_output.txt does have all the output for each device, however, it seems like there is duplicate entries.

Thanks,  
-Scott

---

<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:** [May 15, 2020, 9:49am UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/23 "2020-05-15T09:49:47Z")

</div>

See [https://gist.github.com/vbotka/32616e8d896ef22d985bdb707915c9f0](https://gist.github.com/vbotka/32616e8d896ef22d985bdb707915c9f0)

Split the playbook search\_snmp\_v1.yml to two plays. Collect the data in the  
first play. Then "delegate\_to" localhost and "run\_once" the templates in  
the second play. Remove `strategy: free` in the second play. You've  
already found out:

&nbsp;&nbsp;&nbsp;&nbsp;[WARNING]: Using run\_once with the free strategy is not currently  
&nbsp;&nbsp;&nbsp;&nbsp;supported. This task will still be executed for every host in the  
&nbsp;&nbsp;&nbsp;&nbsp;inventory list.

Change the templates and test it.

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Scott\_Fella](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Scott\_Fella](https://forum.ansible.com/u/Scott_Fella)\
**Post date:** [May 15, 2020, 3:25pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/24 "2020-05-15T15:25:40Z")

</div>

Vlad,

I have used your templates and playbook and ran it against around 170+ devices. The changes works well, except when there is a host that has failed in which the files are not generated. If I exclude the hosts that have failed and re-run the playbook, the output gets generated.

Thanks,  
-Scott

---

<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:** [May 15, 2020, 9:11pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/25 "2020-05-15T21:11:37Z")

</div>

FWIW. See how to create a group of reachable hosts first.  
[https://github.com/vbotka/ansible-lib/blob/master/tasks/al\_hosts\_reachable.yml](https://github.com/vbotka/ansible-lib/blob/master/tasks/al_hosts_reachable.yml)

[Previous page](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916.md?page=1)
