Ansible - Determine Network Address using ansible.utils.ipaddr
by
Jeremy Canfield |
Updated: February 27 2024
| Ansible articles
There are two similar but distinct "ipaddr" modules.
- ansible.netcommon.ipaddr
- ansible.utils.ipaddr
ansible.netcommon.ipaddr is being deprecated, so you should almost always use ansible.utils.ipaddr.
[DEPRECATION WARNING]:
Use 'ansible.utils.ipaddr' module instead.
This feature will be removed from ansible.netcommon in a release after 2024-01-01.
You may need to install the ansible.utils collections.
ansible-galaxy collection install ansible.utils
ansible.utils.ipaddr('network') can be used to determine the network address using an IP address in a subnet. For example, in 10.0.0.0/24
- the network address is 10.0.0.0/24
- the first available IP address is 10.0.0.1
- the last available IP address is 10.0.0.254
- the broadcast address is 10.0.0.255/24
---
- hosts: localhost
tasks:
- name: using '10.0.0.123/24', return network address
debug:
msg: "{{ '10.0.0.123/24' | ansible.utils.ipaddr('network') }}"
- name: using '10.0.0.123/24', return broadcast address
debug:
msg: "{{ '10.0.0.123/24' | ansible.utils.ipaddr('broadcast') }}"
...
Running this playbook should return the following.
TASK [using '10.80.168.5/24', return network address]
ok: [localhost] => {
"msg": "10.80.168.0"
}
TASK [using '10.80.168.5/24', return broadcast address]
ok: [localhost] => {
"msg": "10.80.168.255"
}
Did you find this article helpful?
If so, consider buying me a coffee over at