Re: [ansible-project] Question about Ansible tests

19 views
Skip to first unread message

Kai Stian Olstad

unread,
Oct 31, 2017, 11:33:50 AM10/31/17
to ansible...@googlegroups.com
On 31. okt. 2017 16:24, Dave wrote:
> Is it possible to test for conditions based on partial matches? I'm trying
> to test for variables that match a specific /24 range and I have the
> following so far:
>
> - name: If the IP address is destined for the .1 subnet...
>
> when: set_machinename | match("192.168.1.")
>
> I tried a wildcard as shown here
> <http://docs.ansible.com/ansible/latest/playbooks_tests.html> but that
> didn't seem to work either. Any ideas?


Take a look at this page,
https://docs.ansible.com/ansible/latest/playbooks_filters_ipaddr.html


--
Kai Stian Olstad
Message has been deleted

Dave

unread,
Oct 31, 2017, 3:02:11 PM10/31/17
to Ansible Project
Doesn't seem to be working when I implement it in my code:

   - name: If the IP address is destined for the .1 subnet...
     when: set_machinename | ipaddr('192.168.1.0/24')

It still skips over this range even if I specify set_machinename to '192.168.1.4'.

Kai Stian Olstad

unread,
Oct 31, 2017, 3:16:02 PM10/31/17
to ansible...@googlegroups.com
On 31. okt. 2017 19:58, Dave wrote:
> Doesn't seem to be working when I implement it in my code:
>
> - name: If the IP address is destined for the .1 subnet...
> when: set_machinename | ipaddr('192.168.1.0/32')
>
> It still skips over this range even if I specify set_machinename to
> '192.168.1.4'.


In your earlier mail you wrote /24 now you are using /32?

192.168.1.4 is not part of 192.168.1.0/32, only 192.168.1.0 is that.


--
Kai Stian Olstad

Dave

unread,
Oct 31, 2017, 3:28:03 PM10/31/17
to Ansible Project
Yea, no I corrected that mistake. I meant to say /24, not /32. Still gets bypassed when I run the ansible script.

Kai Stian Olstad

unread,
Oct 31, 2017, 3:36:03 PM10/31/17
to ansible...@googlegroups.com
On 31. okt. 2017 20:28, Dave wrote:
> Yea, no I corrected that mistake. I meant to say /24, not /32. Still gets
> bypassed when I run the ansible script.

What I can say it's working for me.

test.yml
---
- hosts: localhost
tasks:
- debug: msg="Yay I'm running"
when: set_machinename | ipaddr('192.168.1.0/24')


$ ansible-playbook test.yml -e set_machinename=192.168.1.4
PLAY [localhost] *******************************************************************

TASK [debug] ***********************************************************************
ok: [localhost] => {}

MSG:

Yay I'm running



$ ansible-playbook test.yml -e set_machinename=192.168.2.4
PLAY [localhost] *******************************************************************

TASK [debug] ***********************************************************************
skipping: [localhost]


Maybe you should run it with -vvv and see if something jumps or post the output here.


--
Kai Stian Olstad

Dave

unread,
Oct 31, 2017, 4:01:24 PM10/31/17
to Ansible Project
I got it. Thanks Kai. Wrong variable referenced.
Reply all
Reply to author
Forward
0 new messages