Skip to content

Instantly share code, notes, and snippets.

@lathiat
Created April 15, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lathiat/10790228 to your computer and use it in GitHub Desktop.
Save lathiat/10790228 to your computer and use it in GitHub Desktop.
puppet interfaces ipaddr prefixlen
require 'ipaddr'
require 'puppet/util/ipcidr'
interfaces = Facter.value('interfaces')
# If we do not have any interfaces, then there are no requested attributes
unless interfaces == :undefined
interfaces = interfaces.split(',')
interfaces.each do |iface|
ipaddress = Facter.value("ipaddress_#{iface}")
netmask = Facter.value("netmask_#{iface}")
ipaddress6 = Facter.value("ipaddress6_#{iface}")
netmask6 = 64
unless ipaddress.nil?
ip = Puppet::Util::IPCidr.new(ipaddress)
ipm = ip.mask(netmask)
Facter.add("prefixlen_#{iface}") do
setcode do
ipm.prefixlen
end
end
Facter.add("netcidr_#{iface}") do
setcode do
ipm.cidr
end
end
Facter.add("ipcidr_#{iface}") do
setcode do
"#{ipaddress}/#{ipm.prefixlen}"
end
end
end
unless ipaddress6.nil?
ip6 = Puppet::Util::IPCidr.new("#{ipaddress6}/64")
Facter.add("netcidr6_#{iface}") do
setcode do
ip6.cidr
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment