Getting hostgroup as a fact in facter

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 :frowning: Any ideas what could be causing this?

Thanks a lot!
Karolis

> 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.

Just curious, what's the fact used for? Inside a puppet manifest?

The Foreman host group is already exposed as a global parameter named
$hostgroup, you should be able to just use this in your manifest. You
can see the global parameters set by clicking the 'YAML' link on the
host.

If you'd like it named differently, you can add a parameter to your host
or host group named 'role' for example, and set the value to:
<%= @host.hostgroup %>

With safe mode rendering disabled in settings.

Would that do what you need?

··· On Mon, Sep 01, 2014 at 02:29:56PM +0100, Karolis Pabijanskas wrote:

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 :frowning: Any ideas what could be causing this?

Thanks a lot!
Karolis


You received this message because you are subscribed to the Google Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.


Stephen Benjamin


Red Hat GmbH | http://de.redhat.com/ | Sitz: Grasbrunn
Handelsregister: Amtsgericht München, HRB 153243
Geschäftsführer: Charles Cachera, Michael Cunningham,
Michael O’Neill, Charles Peters

Thanks. It is used as a fact in hiera. I do use hiera over foremans
built-in version of it, as I find the foreman one a bit too limiting at
times for this particular set-up. Hostgroup inheritance is not on par yet
with what you can achieve with simple puppet inheritance and hiera in my
opinion.

Thus foreman, in its purpose, is only used as a reporting tool and ENC on
the puppet side of things. I essentially just assign a hostgroup to a node
which only has 1 class in it, and all else is done with hiera and puppet in
the background. Gives a nice overview of what does what to business guys,
and allows me to stay with hiera, which I just find a lot more comfortable
and sane at times.

··· On 1 September 2014 16:56, Stephen Benjamin wrote:

On Mon, Sep 01, 2014 at 02:29:56PM +0100, Karolis Pabijanskas wrote:

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.

Just curious, what’s the fact used for? Inside a puppet manifest?

The Foreman host group is already exposed as a global parameter named
$hostgroup, you should be able to just use this in your manifest. You
can see the global parameters set by clicking the ‘YAML’ link on the
host.

If you’d like it named differently, you can add a parameter to your host
or host group named ‘role’ for example, and set the value to:
<%= @host.hostgroup %>

With safe mode rendering disabled in settings.

Would that do what you need?

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 :frowning: Any ideas what could be causing this?

Thanks a lot!
Karolis


You received this message because you are subscribed to the Google
Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.


Stephen Benjamin


Red Hat GmbH | http://de.redhat.com/ | Sitz: Grasbrunn
Handelsregister: Amtsgericht München, HRB 153243
Geschäftsführer: Charles Cachera, Michael Cunningham,
Michael O’Neill, Charles Peters