Puppet Function: foreman::smartvar
- Defined in:
-
lib/puppet/functions/foreman/smartvar.rb
- Function type:
- Ruby 4.x API
Overview
foreman::smartvar(String[1] $var, Optional[Stdlib::HTTPUrl] $foreman_url, Optional[String] $foreman_user, Optional[String] $foreman_pass) ⇒ Any
11
12
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
|
# File 'lib/puppet/functions/foreman/smartvar.rb', line 11
Puppet::Functions.create_function(:'foreman::smartvar') do
dispatch :smartvar do
required_param 'String[1]', :var
optional_param 'Stdlib::HTTPUrl', :foreman_url
optional_param 'String', :foreman_user
optional_param 'String', :foreman_pass
end
def smartvar(var, foreman_url = "http://foreman", foreman_user = "admin", foreman_pass = "changeme")
scope = closure_scope
fqdn = scope['facts']['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) { JSON.parse(http.request(req).body)["value"] }
rescue Exception => e
raise Puppet::ParseError, "Failed to contact Foreman #{e}"
end
end
end
|