Last active
December 17, 2015 15:15
-
-
Save faxm0dem/35e8e639aa946f6e50b4 to your computer and use it in GitHub Desktop.
puppet function to lookup address
This file contains 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 'resolv' | |
module Puppet::Parser::Functions | |
newfunction(:getaddress, :type => :rvalue, :doc => <<-EOS | |
Takes string and tries to resolve IP | |
EOS | |
) do |arguments| | |
if (arguments.size != 1) then | |
raise(Puppet::ParseError, "getaddress(): Wrong number of arguments " + | |
"given (#{arguments.size} for 1") | |
end | |
if (! arguments[0].kind_of?(String)) then | |
raise(Puppet::ParseError, "getaddress(): Wrong kind for argument " + | |
"given (#{arguments[0].class} instead of String") | |
return | |
end | |
r = Resolv::DNS.new() | |
begin | |
tries ||= 3 | |
ip = r.getaddress(arguments[0]) | |
rescue Resolv::ResolvError => e | |
if (tries -=1 ) > 0 | |
retry | |
else | |
Puppet.warning("getaddress(): Caught Resolv::ResolvError: #{e}") | |
end | |
end | |
return ip.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment