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

3,991 views
Skip to first unread message

edroz...@gmail.com

unread,
Aug 13, 2014, 11:29:24 AM8/13/14
to ansibl...@googlegroups.com
Hello folks,

Following up on 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

======

Example custom Jinja2 filter:

# Adapted from:
# http://code.activestate.com/recipes/576483-convert-subnetmask-from-cidr-notation-to-dotdecima/

# Convert a dotted (xxx.xxx.xxx.xxx) netmask to CIDR bits (xx) notation
def netmask_to_cidr(netmask):
    return sum([bin(int(x)).count('1') for x in netmask.split('.')])

class FilterModule(object):
    ''' utility filters for operating on ip netmasks '''

    def filters(self):
        return {
            'netmask_to_cidr' : netmask_to_cidr
        }

Michael DeHaan

unread,
Aug 14, 2014, 8:02:54 AM8/14/14
to edroz...@gmail.com, ansibl...@googlegroups.com
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.






--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julien Tognazzi

unread,
Oct 8, 2014, 8:04:00 AM10/8/14
to ansibl...@googlegroups.com, edroz...@gmail.com
Hi,

Will this filter be added in next release ?

what is the status on this ?

regards,

Julien.

Michael DeHaan

unread,
Oct 8, 2014, 8:49:08 AM10/8/14
to Julien Tognazzi, ansibl...@googlegroups.com, edroz...@gmail.com
AFAIK, there have been no submissions for any CIDR-related filters at this time.

Still open to them!




Kefas Teguh Putra

unread,
Feb 17, 2016, 9:19:28 PM2/17/16
to Ansible Development, julien....@gmail.com, edroz...@gmail.com
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.

Kefas Teguh Putra

unread,
Feb 18, 2016, 1:33:17 AM2/18/16
to Ansible Development, julien....@gmail.com, edroz...@gmail.com
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:
Reply all
Reply to author
Forward
0 new messages