1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
# Add inet6 prefix if the argument is an IPv6 address.
#
# This is useful for services relying on python-memcached which require
# the inet6:[<ip_address]:<port> format.
#
# Returns the argument untouched otherwise.
Puppet::Functions.create_function(:inet6_prefix) do
def inet6_prefix(*args)
require 'ipaddr'
result = []
args = args[0] if args[0].kind_of?(Array)
args = [args] unless args.kind_of?(Array)
args.each do |ip|
begin
unless ip.match(/^inet6:.+/)
ip_parts = ip.split(/\s|\[|\]/).reject { |c| c.empty? }
if IPAddr.new(ip_parts[0]).ipv6?
Puppet.debug("#{ip} is changed to inet6:[#{ip}]")
ip = "inet6:[#{ip_parts.shift}]#{ip_parts.join}"
end
end
rescue ArgumentError, NoMethodError => e
# ignore it
end
result << ip
end
return result[0] if args.size == 1
result
end
end
|