Created
August 9, 2012 21:40
-
-
Save jenrzzz/3308273 to your computer and use it in GitHub Desktop.
puppet host lookup
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 'resolv' | |
| module Puppet::Parser::Functions | |
| newfunction(:get_ip_addr, :type => :rvalue) do |args| | |
| # Super sexy regex to match valid IPs | |
| ip_addr_re = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/ | |
| hostname = args[0].strip | |
| if hostname =~ ip_addr_re then return hostname end | |
| begin | |
| Resolv::DNS.open { |dns| return dns.getaddress hostname } | |
| rescue Resolv::ResolvError | |
| return '' | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment