Hi All,
I am trying to get the hostgroup from a foreman instance (1.5.2, puppet
3.6, facter 2.1.0) for a given node as a fact.
I have the fact script defined as follows (pardon for the horrible ruby -
not a programmer :-)):
#!/usr/bin/env ruby
require "net/http"
require "net/https"
require "json"
require "facter"
require 'puppet'
First, get the hostname ID from foreman
path = URI.escape("/api/hosts/#{ Facter.value(:fqdn) }/")
uri = URI.parse("https://puppet.domain.com")
req = Net::HTTP::Get.new(path)
req.basic_auth(USERNAME, PASSWORD)
req['Content-Type'] = 'application/json'
req['Accept'] = 'application/json'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
obj = JSON.parse(http.request(req).body)
hostgroupid = obj["host"]["hostgroup_id"]
Then, get the actual host name
path = URI.escape("/api/hostgroups/#{hostgroupid}")
req = Net::HTTP::Get.new(path)
req.basic_auth(USERNAME, PASSWORD)
req['Content-Type'] = 'application/json'
req['Accept'] = 'application/json'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
obj = JSON.parse(http.request(req).body)
Facter.add(:role) do
setcode do obj["hostgroup"]["name"] end
end
Now, if I run this script directly, it returns the hostgroup name just
fine, but if I run it via facter -p or puppet directly, it fails:
root@centosdev vagrant]# facter -p role -d
value for lsbdistid is still nil
value for lsbdistid is still nil
Found no suitable resolves of 1 for ec2_metadata
value for ec2_metadata is still nil
[root@centosdev vagrant]# puppet agent --test
Fact file /var/lib/puppet/facts.d/role.rb was parsed but returned an empty
data set
This is driving me crazy Any ideas what could be causing this?
Thanks a lot!
Karolis