Finding parameters for a host

Hi all, relative newbie to Foreman here, trying to pull some data from the
Foreman API for use in another application.

I'll walk you through my thinking, so if I've made any mistakes, perhaps
someone might point them out to me. I'm trying to pull out parameter data
for a class on a host, which is set at hostgroup level in this case (but
might not always be).

So in Puppet, a host (or group of hosts) can have a class applied to it. As
part of including that class on the host, parameters must be populated with
values. The class can have default values to be used, or they can be
overridden.

For example, if you click parameters at
https://foreman/hostgroups/123-<group-name>/edit
you'll see where values are passed for particular classes.

If the value has been set at the hostgroup level, there doesn't seem to be
a way to fetch it via the host. I tried these two calls:
http://foreman/api/hosts/<host-id> - shows the classes, but not parameters
http://foreman/api/hosts/<host-id>/smart_class_parameters - shows default
values for parameters, but not where they've been over-ridden by the group

I can't even get it from the hostgroup calls:
http://foreman/api/hostgroups/<hostgroup-id>/smart_class_parameters/

The only way I've found so far is to fetch the class object:
http://foreman/api/puppetclasses/<class-id>

This shows each parameter with an ID, so we can get the parameter object
and value:
http://foreman/api/smart_class_parameters/<parameter-id>

This contains the values set for overriding parameters based on rules like
'"match": "hostgroup=<group-name>".'

It doesn't seem possible to be able to fetch which parameters have been set
for a host, without parsing the rules to see if they match the host.

Is my understanding above all correct? Is there a shortcut that I'm missing
to finding out what values a host is using for a class?

It's already in the ENC data that Foreman sends to Puppet (assuming the
host is in the hostgroup in question). When the ENC YAML is compiled,
Foreman will parse the parameters from least specific to most specific,
overriding ones with the same name as it goes. So if your hostgroup has
"class foo -> parameter bar = quux" you should see that on your host
directly (check the YAML button on the host page). But if the host has
"class foo -> parameter bar = qaaz" then you'll see that instead. The
exact hierarchy is listed in the manual, see:

https://theforeman.org/manuals/1.15/#4.2.3Parameters

You don't actually say where you're trying to consume this data
though. I assume you're wanting it in Puppet, which should work out of
the box - anything in the ENC is passed to Puppet and should be used
directly. If you want to access it in templates, you can use "<%=
host_enc %>" (via About - Foreman
TemplateWriting#Functions-and-macros), and from some other place, there
is an API call to get the host's ENC data (GET /api/hosts/:id/enc)

If want to access data for other hosts / hostgroups in Puppet
manifests, there are ways - but for brevity I'll not go into to that
yet. If that's what you're actually looking for, shout out :slight_smile:

Greg

··· On Wed, 2017-07-05 at 08:45 -0700, Brian McArdle wrote: > Is my understanding above all correct? Is there a shortcut that I'm > missing to finding out what values a host is using for a class?

Thanks for the reply Greg,

I am looking to consume the data through the API, and your docs link and
the term 'ENC data' (which is new to me) helped me find the answer.

The docs on parameters mention at the end of section 4.2.3:
"To see how Foreman is passing the parameters to Puppet, go to a Host and
click the YAML button. You will be shown the exact YAML data sent to the
Puppet master - the parameters will be in the “parameters” hash."

The short version of my question was, how do I get that data from the API?
The answer is via /api/hosts/<id>/enc

Many thanks for solving an issue that has been plaguing me for most of the
week!
Brian

··· On Friday, July 7, 2017 at 10:21:45 AM UTC+1, Greg Sutcliffe wrote: > > On Wed, 2017-07-05 at 08:45 -0700, Brian McArdle wrote: > > Is my understanding above all correct? Is there a shortcut that I'm > > missing to finding out what values a host is using for a class? > > It's already in the ENC data that Foreman sends to Puppet (assuming the > host is in the hostgroup in question). When the ENC YAML is compiled, > Foreman will parse the parameters from least specific to most specific, > overriding ones with the same name as it goes. So if your hostgroup has > "class foo -> parameter bar = quux" you should see that on your host > directly (check the YAML button on the host page). But if the host has > "class foo -> parameter bar = qaaz" then you'll see that instead. The > exact hierarchy is listed in the manual, see: > > https://theforeman.org/manuals/1.15/#4.2.3Parameters > > You don't actually say *where* you're trying to consume this data > though. I assume you're wanting it in Puppet, which should work out of > the box - anything in the ENC is passed to Puppet and should be used > directly. If you want to access it in templates, you can use "<%= > host_enc %>" (via http://projects.theforeman.org/projects/foreman/wiki/ > TemplateWriting#Functions-and-macros > ), > and from some other place, there > is an API call to get the host's ENC data (GET /api/hosts/:id/enc) > > If want to access data for other hosts / hostgroups in Puppet > manifests, there are ways - but for brevity I'll not go into to that > yet. If that's what you're actually looking for, shout out :) > > Greg >