Puppet Function: smartvar

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

Overview

smartvar()Any

Returns:

  • (Any)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/parser/functions/smartvar.rb', line 13

newfunction(:smartvar, :type => :rvalue) do |args|
  #URL to query
  foreman_url  = "http://foreman"
  foreman_user = "admin"
  foreman_pass = "changeme"

  var = args[0]
  raise Puppet::ParseError, "Must provide a variable name to search for" if var.nil?

  fqdn = lookupvar("fqdn")

  uri = URI.parse(foreman_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'

  path = "/hosts/#{CGI.escape(fqdn)}/lookup_keys/#{CGI.escape(var)}"
  req = Net::HTTP::Get.new(path)
  req.basic_auth(foreman_user, foreman_pass)
  req['Content-Type'] = 'application/json'
  req['Accept'] = 'application/json'

  begin
    Timeout::timeout(5) { PSON.parse(http.request(req).body)["value"] }
  rescue Exception => e
    raise Puppet::ParseError, "Failed to contact Foreman #{e}"
  end

end