Created
April 15, 2014 23:50
puppet interfaces ipaddr prefixlen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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