Puppet Function: extlib::netmask_to_cidr

Defined in:
lib/puppet/functions/extlib/netmask_to_cidr.rb
Function type:
Ruby 4.x API

Summary

Converts an octet netmask address of the form 255.255.255.0 into its CIDR variant. Thus making it directly usable with the values from facter.

Overview

extlib::netmask_to_cidr(Stdlib::IP::Address::Nosubnet $netmask)Integer[0, 128]

Examples:

calling the function

extlib::netmask_to_cidr('255.0.0.0')

Parameters:

  • netmask (Stdlib::IP::Address::Nosubnet)

    IPv6 or IPv4 netmask in octet notation

Returns:

  • (Integer[0, 128])

    CIDR / prefix length



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/functions/extlib/netmask_to_cidr.rb', line 6

Puppet::Functions.create_function(:'extlib::netmask_to_cidr') do
  # @param netmask IPv6 or IPv4 netmask in octet notation
  # @return CIDR / prefix length
  # @example calling the function
  #   extlib::netmask_to_cidr('255.0.0.0')
  dispatch :netmask_to_cidr do
    param 'Stdlib::IP::Address::Nosubnet', :netmask
    return_type 'Integer[0, 128]'
  end

  def netmask_to_cidr(netmask)
    dummy_addr = IPAddr.new(netmask).ipv6? ? '::' : '0.0.0.0'
    IPAddr.new("#{dummy_addr}/#{netmask}").prefix
  end
end