DEV Community

koh-sh
koh-sh

Posted on • Edited on • Originally published at koh-sh.hatenablog.com

How to check if an IP address is within a range in Ansible Playbook

I was trying to have tasks for each site and multiple regions in Cloud respectively.
My idea was to use the IP address to determine where the host is, but it was kind of struggling to create the condition.
So here is my memo down below.

TL;DR

This when statement checks if the IP address is within the specified range.

- name: test
  debug:
    msg: work
  when: ansible_default_ipv4.address | ipaddr('10.0.2.0/24') | ipaddr('bool')
Enter fullscreen mode Exit fullscreen mode

This task prints "work" if ansible_default_ipv4.address is within 10.0.2.0/24

Explanation

I am using ipaddr filter
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html

First half

[IP list] | ipaddr('172.30.0.0/16')
Enter fullscreen mode Exit fullscreen mode

Above syntax extracts IP addresses in [IP list] which is within 172.30.0.0/16

% cat test.yml
---
- hosts: Vag1
  gather_facts: True
  tasks:
    - set_fact:
        testip:
          - "192.168.1.2"
          - "10.0.2.15"

    - name: test
      debug:
        msg: "{{ testip | ipaddr('10.0.2.0/24') }}"
Enter fullscreen mode Exit fullscreen mode

By Running test.yml above.

[koh@kohs-MacBook-Pro] ~/vag_test
% ansible-playbook test.yml
PLAY [Vag1] ***************************************************************************************************************************

TASK [set_fact] ***********************************************************************************************************************
ok: [Vag1]

TASK [test] ***************************************************************************************************************************
ok: [Vag1] => {
    "msg": [
        "10.0.2.15"
    ]
}

PLAY RECAP ****************************************************************************************************************************
Vag1                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[koh@kohs-MacBook-Pro] ~/vag_test
%
Enter fullscreen mode Exit fullscreen mode

There are 2 IPs in testip and only 10.0.2.15 was printed since it is within 10.0.2.0/24

The second half

[IP address] | ipaddr('bool')
Enter fullscreen mode Exit fullscreen mode

It returns True if [IP address] is a valid IP address.

% cat test2.yml
---
- hosts: Vag1
  gather_facts: True
  tasks:
    - set_fact:
        testip:
          - "192.168.1.2"
          - "10.0.2.15.14.22"
          - "hogehoge"
          - ""

    - name: test
      debug:
        msg: "{{ item | ipaddr('bool')  }}"
      with_items: "{{ testip }}"
Enter fullscreen mode Exit fullscreen mode

By Running test2.yml above.

[koh@kohs-MacBook-Pro] ~/vag_test
% ansible-playbook test2.yml

PLAY [Vag1] ****************************************************************************************************************************

TASK [set_fact] ****************************************************************************************************************************
ok: [Vag1]

TASK [test] ****************************************************************************************************************************
ok: [Vag1] => (item=192.168.1.2) => {
    "msg": true
}
ok: [Vag1] => (item=10.0.2.15.14.22) => {
    "msg": false
}
ok: [Vag1] => (item=hogehoge) => {
    "msg": false
}
ok: [Vag1] => (item=) => {
    "msg": false
}

PLAY RECAP ****************************************************************************************************************************
Vag1                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[koh@kohs-MacBook-Pro] ~/vag_test
%
Enter fullscreen mode Exit fullscreen mode

With these 2 syntaxes, you can check if an IP address of hosts is within a range automatically.

Reference

https://stackoverflow.com/questions/38725204/regular-expression-matching-for-ip-range-in-ansible-playbooks-for-grouping

Top comments (3)

Collapse
 
caodawei3 profile image
David Cao

howtouselinux.com/post/get-ip-addr...

- debug:
    msg: "matched pattern 2"
  when: ansible_default_ipv4.address is search("192.168")
Enter fullscreen mode Exit fullscreen mode

docs.ansible.com/ansible/latest/pl...

Collapse
 
aaronjaegerva profile image
Aaron Jaeger

Thanks for this.

It looks like your link to docs.ansible.com is broken. Also, I could not find a reference to the ipaddr('bool') you are using as your second filter.

Collapse
 
koh_sh profile image
koh-sh

Thanks for the heads up.
Now the link is fixed.

And about ipaddr('bool').
I could not find it in the official doc either but you can find it from the source.
github.com/ansible-collections/ans...