Ask Your Question

convert netmask to cidr

asked 2013-10-09 13:45:51 -0600

lorcutt gravatar image

HI -

I'm trying to configure puppet to setup keepalived. As part of this I need to get the netmask of an ip in CIDR notation. I can use the fact $::netmask_eth0 to get a netmask like 255.255.255, but the keepalived configuration file requires /24 notation.

I found the ruby gen NetAddr::CIDR looks like it could do the job, but I don't appear to be able to invoke that from inside a puppet inline_template even though "gem list netaddr" shows it installed.

Any guidance or ideas?


edit retag flag offensive close merge delete


Please post your sample code so we can have a look and troubleshoot it.

GregLarkin gravatar imageGregLarkin ( 2013-10-09 14:41:05 -0600 )edit

A similar question was asked here without the puppet context: I've tried the following so far without getting what I'm looking for ...(more)

lorcutt gravatar imagelorcutt ( 2013-10-09 14:52:13 -0600 )edit

Did those various tests give you errors or the wrong answers altogether?

GregLarkin gravatar imageGregLarkin ( 2013-10-09 15:10:10 -0600 )edit

Some were errors and some returned an empty string. None gave me the /24 I was looking for.

lorcutt gravatar imagelorcutt ( 2013-10-09 15:24:25 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2013-10-09 15:18:19 -0600

lorcutt gravatar image

updated 2013-10-09 15:45:39 -0600

Seems like it didn't like the .count("1") on the end of this line:

#$vipmask = inlinetemplate("<% erbout.concat(IPAddr::new('$::netmasketh0').toi.tos(2).count("1")) %>")

When I pulled the count out into the stdlib count function I got the right answer:

$vipmaskbin = inlinetemplate("<% _erbout.concat(IPAddr::new('$::netmasketh0').toi.tos(2)) %>") $vipmask = count($vipmask_bin,"1")

edit flag offensive delete link more


Converted this into a custom function: # cidr_mask.rb # Converts netmask to cidr format ( -> 24) require 'rubygems' require 'ipaddr' module Puppet::Parser::Functions newfunction(:cidr_mask, :type => :rvalue ...(more)

lorcutt gravatar imagelorcutt ( 2013-10-10 12:13:58 -0600 )edit

answered 2013-10-09 15:13:41 -0600

Stefan gravatar image

You should really avoid the inline_template function. There are two alternatives:

  1. define a custom fact. This way every agent would deliver an netmaskcidr_<interface> fact. The problem here is that every agent has to have your gem installed which is probably a bad idea.
  2. define a custom function. This way your puppet master can convert a netmask into your desired format with a custom function. A function always runs on the master so you only have to have your gem installed there.

If you want to do option 2) you can define a custom function by dropping the following sample ... (more)

edit flag offensive delete link more


Still have the problem with the function not finding the netaddr gem. Error: Could not retrieve catalog from remote server: Error 400 on SERVER: no such file to load -- netaddr ...(more)

lorcutt gravatar imagelorcutt ( 2013-10-09 15:23:38 -0600 )edit

Are you running Puppet Enterprise? Puppet Enterprise ships its own ruby so you would not be able to use any of the the system's rubygems. If that is case ...(more)

Stefan gravatar imageStefan ( 2013-10-09 15:31:42 -0600 )edit

This is opensource puppet 3.2.2

lorcutt gravatar imagelorcutt ( 2013-10-09 16:09:49 -0600 )edit

answered 2015-10-12 14:37:21 -0600

vikas027 gravatar image

Another option is to use the forge module netstdlib.

I used it as below to convert my $netmask variable which had in it.

$netmask_cidr = netmask_to_masklen("$netmask")

Hope this helps someone.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-10-09 13:45:51 -0600

Seen: 2,366 times

Last updated: Oct 12 '15