IP List Contain

23 views
Skip to first unread message

colli...@gmail.com

unread,
Nov 3, 2020, 5:00:49 PM11/3/20
to Ansible Project
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

Vladimir Botka

unread,
Nov 3, 2020, 6:14:16 PM11/3/20
to colli...@gmail.com, ansible...@googlegroups.com
On Tue, 3 Nov 2020 14:00:49 -0800 (PST)
"colli...@gmail.com" <colli...@gmail.com> wrote:

> 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)

--
Vladimir Botka

Vladimir Botka

unread,
Nov 4, 2020, 4:38:20 AM11/4/20
to colli...@gmail.com, ansible...@googlegroups.com
On Tue, 3 Nov 2020 14:00:49 -0800 (PST)
"colli...@gmail.com" <colli...@gmail.com> wrote:

> vars:
> ips: [1.1.1.1,2.2.2.2]
>
> tasks:
> ...
> when tacacs.stdout[0] contains ips

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

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


--
Vladimir Botka

J C

unread,
Nov 5, 2020, 11:35:52 AM11/5/20
to Vladimir Botka, ansible...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages