IP List Contain

Hopefully someone can assist - this seems like it should be a simple thing.

vars:
ips: [1.1.1.1,2.2.2.2]

tasks:
-name: Get Tacacs info
cisco.ios.ios_command
commands: “show tacacs”
register: tacacas
-name: Run command based on tacacs var
cisco.ios.config:
lines:

  • do stuff
    when tacacs.stdout[0] contains ips

which gives me a template error.

Also tried:

when: (“‘1.1.1.1’ in tacacs.stdout[0]”) or

(“‘2.2.2.2’ in tacacs.stdout[0]”)

this comes back true for all of the hosts regardless if the IPs are in the output or not.

My goal is to have to run the command when it finds 1.1.1.1 or 2.2.2.2 in the output. Any help would be appreciated.

Thanks

My goal is to have to run the command when it finds 1.1.1.1 or 2.2.2.2 in
the output.
...
vars:
ips: [1.1.1.1,2.2.2.2]

If tacacs.stdout.0 is a list (not very likely) try this

  when: ips|intersect(tacacs.stdout.0)|length > 0

when: ("'1.1.1.1' in tacacs.stdout[0]") or
      ("'2.2.2.2' in tacacs.stdout[0]")

If tacacs.stdout.0 is a text (more likely) try this

  when: tacacs.stdout.0 is search(ips.0|regex_escape()) or
        tacacs.stdout.0 is search(ips.1|regex_escape())

when tacacs.stdout[0] contains ips

There is no *contains* test in Ansible but you can write one
https://github.com/vbotka/ansible-plugins/blob/master/test_plugins/list.py

If tacacs.stdout.0 is a text with IP value only the previous
solution should work. Or, with the custom test, try this

  when: ips contains(tacacs.stdout.0)

The next option is Jinja2 test *in*
https://jinja.palletsprojects.com/en/master/templates/#in

      when: tacacs.stdout.0 is in(ips)

Thanks again Vladimir. I ended up finding a simpler approach.

This was my fault because the fake IP addresses that I made didn’t really represent the real IP addresses.

1.1.1.1
1.1.2.1

That is more of an accurate representation of the IP addresses that I am looking for.

So I went with this:

tacacs is search(“1.1.//d//.1”)

Sorry for the confusion and thank you again for taking the time and looking into this for me.