# Best way to get at CIDR netmask data? (i.e. "/24" rather than 255.255.255.0)

**URL:** <https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932>\
**Category:** Ansible Developer\
**Created:** [August 13, 2014, 3:29pm UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932 "2014-08-13T15:29:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [August 13, 2014, 3:29pm UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/1 "2014-08-13T15:29:24Z")

</div>

Hello folks,

Following up on [https://github.com/ansible/ansible/issues/8584](https://github.com/ansible/ansible/issues/8584), wanted to open a discussion on the best way to be able to get at CIDR formatted netmask info.

If a host fact returns a netmask 255.255.255.0, I’d like to find a way to convert that to “24” - some config files such as Postgres pg\_hba.conf require CIDR format.

I was able to create a custom Jinja2 filter (referenced in the issue above) and pasted below - would it make sense to incorporate such a filter into the standard Ansible codebase? Is there a better approach?

Regards,  
–Ed

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 14, 2014, 12:02pm UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/2 "2014-08-14T12:02:53Z")

</div>

The above looks pretty good to me.

python-netaddr should be available most anywhere, and I wouldn’t object to it being a dependency, and I think we’d want to use that if possible.

---

<div class="post-metadata">

**Author:** ![Julien\_Tognazzi](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Julien\_Tognazzi](https://forum.ansible.com/u/Julien_Tognazzi)\
**Post date:** [October 8, 2014, 12:04pm UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/3 "2014-10-08T12:04:00Z")

</div>

Hi,

Will this filter be added in next release ?

what is the status on this ?

regards,

Julien.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 8, 2014, 12:49pm UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/4 "2014-10-08T12:49:06Z")

</div>

AFAIK, there have been no submissions for any CIDR-related filters at this time.

Still open to them!

---

<div class="post-metadata">

**Author:** ![Kefas\_Teguh\_Putra](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@Kefas\_Teguh\_Putra](https://forum.ansible.com/u/Kefas_Teguh_Putra)\
**Post date:** [February 18, 2016, 2:19am UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/5 "2016-02-18T02:19:27Z")

</div>

Hi all,

my first post here ever.

I’ve been looking all over for this feature… Is this implemented yet in ansible v2? It would be great to have it in ansible. In my setting, this format is required by Postgresql’s pg\_hba.conf and Pacemaker’s script to configure virtual ips in Debian HA.

Pada Rabu, 08 Oktober 2014 19.49.08 UTC+7, michael menulis:

---

<div class="post-metadata">

**Author:** ![Kefas\_Teguh\_Putra](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@Kefas\_Teguh\_Putra](https://forum.ansible.com/u/Kefas_Teguh_Putra)\
**Post date:** [February 18, 2016, 6:33am UTC](https://forum.ansible.com/t/best-way-to-get-at-cidr-netmask-data-i-e-24-rather-than-255-255-255-0/15932/6 "2016-02-18T06:33:17Z")

</div>

Never mind! I finally have a satisfying answer.

It’s a work-around just recently explained (23 days ago) by jtognazzi:  
**{{ ‘X/255.255.255.0’ | ipaddr(‘prefix’) }}**  
gives:  
**24**  
Where X is a dummy ip.

Thanks anyway! ansible is an awesome project!

Pada Rabu, 08 Oktober 2014 19.49.08 UTC+7, michael menulis:
