Ansible - Convert subnet mask into prefix using ansible.utils.ipaddr
by
Jeremy Canfield |
Updated: November 06 2023
| 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('address/prefix') can be used to take an IP address/subnet mask and convert it into IP address/prefix.
---
- hosts: localhost
tasks:
- debug:
msg: "{{ '10.11.12.13/255.255.255.0' | ansible.utils.ipaddr ('address/prefix') }}"
...
Running this playbook should return the following, converting subnet mask 255.255.255.0 into prefix /24.
ok: [localhost] => {
"msg": "10.11.12.13/24"
}
Did you find this article helpful?
If so, consider buying me a coffee over at