# 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:** 20\
**Page:** 1

<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 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/1 "2020-05-12T19:59:20Z")

</div>

Still learning Ansible, but getting somewhat better. I’m trying to use a list that I have in a playbook just for ease of testing. Is there a simple way of writing to a file when one of the items in the list is found in the output?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 13, 2020, 1:20am UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/2 "2020-05-13T01:20:45Z")

</div>

This basically looks like grep? What is your goal.

Can you post a real output of ‘show snmpcommunity’? I’m a bit rusty on snmp.

---

<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 13, 2020, 1:35am UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/3 "2020-05-13T01:35:23Z")

</div>

I can share that later, not in front of my laptop. What I was looking for is not just with snmp, I just started with snmp since I was working on other playbooks. I was looking to see if I can simplify the “when” to check if the output has the any of the variables defined in the list. What I have is working, but is there a better way?

-Scott

---

<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 13, 2020, 2:25pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/4 "2020-05-13T14:25:46Z")

</div>

Here is a sample output when I run a show snmpcommunity. The one with ‘\*’ are hashed and read-only, so I will not be able to match on those, it is the read-only strings i’m worried about.

rusred991c55p1

IPSec mode: Disabled / Profile: none

SNMP Community Name Client IP Address Client IP Mask Access Mode Status

* * *

\*\*\*\*\*\*\*\*\*\* 10.200.250.0 255.255.255.0 Read/Write Enable  
\*\*\*\*\*\*\*\*\*\* 10.100.10.208 10.100.10.208 Read/Write Enable  
xxxxxxxxxx 0.0.0.0 0.0.0.0 Read Only Enable  
yyyyyyyyyy 0.0.0.0 0.0.0.0 Read Only Enable  
zzzzzzzzzz 0.0.0.0 0.0.0.0 Read Only Enable

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 13, 2020, 3:18pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/5 "2020-05-13T15:18:45Z")

</div>

Is there any chance you can have a single var that is a list for those strings?  
I.e.:

snmp\_strings:  
&nbsp;&nbsp;- aaaaaaaaa  
&nbsp;&nbsp;- bbbbbbbbb  
&nbsp;&nbsp;- cccccccccc

---

<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 13, 2020, 5:27pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/6 "2020-05-13T17:27:14Z")

</div>

So you mean define it like the following in a playbook:

vars:  
&nbsp;&nbsp;snmp\_strings:  
&nbsp;&nbsp;&nbsp;- aaaaaaaaa  
&nbsp;&nbsp;&nbsp;- bbbbbbbbb  
&nbsp;&nbsp;&nbsp;- cccccccccc

-Scott

---

<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 13, 2020, 5:44pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/7 "2020-05-13T17:44:18Z")

</div>

Okay, this is what I have now that is working, but can I simplify the when?

---

<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 13, 2020, 8:26pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/8 "2020-05-13T20:26:56Z")

</div>

> ... can I simplify the when?  
> (when one of the items in the list is found in the output?)

It's possible to create a list of the "search" results and use the test  
"any". For example the playbook  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#testing-strings](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings)  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#test-if-a-list-contains-a-value](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#test-if-a-list-contains-a-value)

&nbsp;&nbsp;&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_output: "aa bb cc"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: [aa, bb, xx]  
&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;my\_search: "{{ my\_search|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[my\_output is search(item)] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_search  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Write to file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: my\_search is any

gives

&nbsp;&nbsp;&nbsp;&nbsp;"my\_search": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;]

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Write to file"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 13, 2020, 10:16pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/9 "2020-05-13T22:16:23Z")

</div>

Add these tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matches: "{{ matches|default('') +  
preOutputSummary.stdout\_lines | regex\_search(item + '\\s+.\*\\n') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ oldSnmp }}"

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "./{{ ansible\_play\_name }}\_output.txt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ matches }}"

This should give this result:

xxxxxxxxxx 0.0.0.0 0.0.0.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Read Only Enable  
yyyyyyyyyy 0.0.0.0 0.0.0.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Read Only Enable  
zzzzzzzzzz 0.0.0.0 0.0.0.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Read Only Enable

---

<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 13, 2020, 10:16pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/10 "2020-05-13T22:16:30Z")

</div>

Vladimir,

I’m a bit confused on how I can use the register: preOutputSummary in the set\_facts search. My output when running the command: show snmpcommunity outputs this:

IPSec mode: Disabled / Profile: none

SNMP Community Name Client IP Address Client IP Mask Access Mode Status

* * *

* * *

\*\*\*\*\*\*\*\*\*\* 10.200.250.0 255.255.255.0 Read/Write Enable  
\*\*\*\*\*\*\*\*\*\* 10.100.10.208 10.100.10.208 Read/Write Enable  
xxxxxxxxxx 0.0.0.0 0.0.0.0 Read Only Enable  
yyyyyyyyyy 0.0.0.0 0.0.0.0 Read Only Enable  
zzzzzzzzzz 0.0.0.0 0.0.0.0 Read Only Enable

I do understand what you posted in regard to the test, but don’t know how to integrate your feedback to my playbook.

-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 14, 2020, 7:58am UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/11 "2020-05-14T07:58:22Z")

</div>

Searching "preOutputSummary.stdout[0]" doesn't make sense.

To clarify the difference between ".stdout" and ".stdout\_lines" of registered  
output. ".stdout" is a string and ".stdout\_lines" is a list of the lines. As  
a result ".stdout[0]" is the first character of the output. For example

&nbsp;&nbsp;&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- command: cat /etc/passwd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: output  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: output.stdout\_lines[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: output.stdout[0]

gives

&nbsp;&nbsp;&nbsp;&nbsp;"output.stdout\_lines[0]": "root:x:0:0:root:/root:/bin/bash"  
&nbsp;&nbsp;&nbsp;&nbsp;"output.stdout[0]": "r"

In your case 1) use "preOutputSummary.stdout" to search the items from the  
list "oldSnmp" and 2) use "preOutputSummary.stdout\_lines" to write the file.

Try this

---

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

</div>

This can be simplified further

&nbsp;&nbsp;&nbsp;&nbsp;- name: write output to a file if snmp if found  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: output.txt.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ ansible\_play\_name }}\_output.txt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: oldSnmp|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select('in', preOutputSummary.stdout)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length \> 0

---

<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, 12:53pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/13 "2020-05-14T12:53:05Z")

</div>

Thanks for the follow up. I will give this a try today and let you know. I’m not familiar with the set\_facts and using templates.

-Scott

---

<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, 12:58pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/14 "2020-05-14T12:58:26Z")

</div>

Thanks for this! I will give this a try later also.

-Scott

---

<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, 2:22pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/15 "2020-05-14T14:22:28Z")

</div>

I gave it a try and got an error I don’t know how to resolve it. The set\_facts my\_search is over my head:)

**search\_snmp.yml**

---

<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, 2:33pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/16 "2020-05-14T14:33:45Z")

</div>

I have tried your example also and got the same error using Vlad’s example also.

**search\_snmp.yml**

---

<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, 5:28pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/17 "2020-05-14T17:28:08Z")

</div>

Okay, well from doing some online searching and tinkering, maybe the aireos module does something different. I saw this post stating the following:

> you need intermediate `join` here, because for `ios`-family modules `stdout` is a list of strings, and `stdout_lines` is a list of lists (whereas for usual `command` module `stdout` is a string and `stdout_lines` is a list of strings).

I have also posted the output using stdout, stdout\_lines with the aireos\_command module, but here is my playbook that seems to work. Curious what I would need to do if using set\_facts.

---

<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, 5:30pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/18 "2020-05-14T17:30:04Z")

</div>

This is where I got the info from: [https://stackoverflow.com/questions/45737295/using-when-conditional-to-match-string-in-output-register-ansible](https://stackoverflow.com/questions/45737295/using-when-conditional-to-match-string-in-output-register-ansible)

---

<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, 5:45pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/19 "2020-05-14T17:45:06Z")

</div>

Okay sorry for all the post… I think I have something I can work with now. Here is my playbook that is working with the set\_fact:

---

<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, 8:16pm UTC](https://forum.ansible.com/t/is-there-a-better-way-to-search-through-a-list-using-when-and-regex-search/32916/20 "2020-05-14T20:16:53Z")

</div>

Vlad,

When trying to use the jinja2 template and running this playbook over multiple hosts, the file gets overwritten. This works for a single device if there is a match.

{{ inventory\_hostname }}

{% for line in preOutputSummary.stdout\_lines[0] %}  
{{ line }}  
{% endfor %}

-Scott

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