Puppet Function: h2n

Defined in:
lib/puppet/parser/functions/h2n.rb
Function type:
Ruby 3.x API

Overview

h2n()Stri

Takes a single hostname and returns the associated IP address if it can determine it.

If it cannot be determined, simply returns the passed hostname.

Returns:

  • (Stri)

    Stri



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
# File 'lib/puppet/parser/functions/h2n.rb', line 2

newfunction(:h2n, :type => :rvalue, :doc => <<-EOM) do |args|

  require 'resolv'
  Takes a single `hostname` and returns the associated IP address if it
  can determine it.

  If it cannot be determined, simply returns the passed hostname.

  @return [String]
  EOM

  to_find = args.first.to_s
  retval = to_find

  if not to_find.include?('.') and not lookupvar('::domain').empty? then
    to_find = "#{to_find}.#{lookupvar('::domain')}"
  end

  begin
    Timeout::timeout(2) do
      retval = Resolv::DNS.new().getaddress(to_find).to_s
    end
  rescue Timeout::Error, Resolv::ResolvError
    # ignore
  end
  return retval
end