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? Thanks edit retag close merge delete ## Comments Please post your sample code so we can have a look and troubleshoot it. ( 2013-10-09 14:41:05 -0500 )edit A similar question was asked here without the puppet context: http://stackoverflow.com/questions/1825928/netmask-to-cidr-in-ruby I've tried the following so far without getting what I'm looking for ...(more) ( 2013-10-09 14:52:13 -0500 )edit Did those various tests give you errors or the wrong answers altogether? ( 2013-10-09 15:10:10 -0500 )edit Some were errors and some returned an empty string. None gave me the /24 I was looking for. ( 2013-10-09 15:24:25 -0500 )edit ## 3 Answers Sort by » oldest newest most voted 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") more ## Comments Converted this into a custom function: # cidr_mask.rb # Converts netmask to cidr format (255.255.255.0 -> 24) require 'rubygems' require 'ipaddr' module Puppet::Parser::Functions newfunction(:cidr_mask, :type => :rvalue ...(more) ( 2013-10-10 12:13:58 -0500 )edit 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 ## Comments 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) ( 2013-10-09 15:23:38 -0500 )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) ( 2013-10-09 15:31:42 -0500 )edit This is opensource puppet 3.2.2 ( 2013-10-09 16:09:49 -0500 )edit Another option is to use the forge module netstdlib. I used it as below to convert my $netmask variable which had 255.255.255.0 in it.

$netmask_cidr = netmask_to_masklen("$netmask")


Hope this helps someone.

more