Is there a better way to search through a list using when and regex_search

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

  "I have something I can work with now..."

  - name: search_snmp
    hosts: all
    ...
    - name: write output to a file if snmp if found
      lineinfile:
      path: ./{{ansible_play_name}}_device_list.txt
      ...

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

  - name: write output to a file if snmp if found
    template:
      src: output.txt.j2
      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/ 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

HTH,

  -vlado

Vlad,

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

gist: 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

See 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:

    [WARNING]: Using run_once with the free strategy is not currently
    supported. This task will still be executed for every host in the
    inventory list.

Change the templates and test it.

HTH,

  -vlado

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

FWIW. See how to create a group of reachable hosts first.
https://github.com/vbotka/ansible-lib/blob/master/tasks/al_hosts_reachable.yml