Writing facts into host_parameters

Problem:

Is it possible to write Puppet facts into host_parameters?
Facter is collecting some information on client side which I need in host_parameters.
So every host has for example the fact category with a value. I want to write this information into a host_parameter in general and regularly.

Foreman and Proxy versions: 1.20

In Foreman 1.22 https://projects.theforeman.org/issues/4127 was merged. I think you can use that with ERB templating to look up a fact in the host parameters.

@ekohl Can you give me a hint, how to start this?

Also It would make sense to have a specific fact in the comments field of a hosts (since the fact is some kind of description for what the host is used). Is it the same approach?

You can use https://projects.theforeman.org/projects/foreman/wiki/TemplateWriting as a basis. So <%= @host.facts['fqdn'] %> works to add the FQDN fact as a global parameter.

I don’t know if this requires 1.22 but I’ve confirmed this works on nightly which is the only version I have at hand to test with.

1 Like

In the meantime I’m running on 1.22 :+1:
To be honest, the wiki article does not help a beginner. I still missing a little bit Foreman logic.

Let me ask some general questions:

  • We are talking about Provisioning Templates?
  • We have to add additional configs into those templates?
  • The templates are those, that are being used by the operating systems?
    • if so, this only works for new instances which are built after my config changes?

It does assume you know ERB (see https://www.stuartellis.name/articles/erb/ if not). Template variables does document how it generally can be used (as I wrote above) but TemplateWriting documents the actual data structures you can use in ERB. The actual content is rendered with live data and not pre-calculated/cached. As long as the parameter is present on a host (and for global that’s every single host), it’s used.

Our documentation can certainly use some improvement.

Thanks again! The templating language itself seems to be more or less self-explaining.

What I really miss is an example. You wrote, it can be used in global parameters.
It sounds like this:


But the host does not show the value:

You should look at the rendered output. There’s the YAML button on host details or via the API.

Now I got it! Thanks a lot!

@ekohl
Still one question. The host API does not show the value in my case. It’s showing the ERB template. Which endpoint do you mean? Or is it maybe a versioning issue since you use the nightly?

I’ve tested /api/hosts/ID

image

The parameters there are meant for modification. /api/hosts/ID/enc is meant for consumption. That does make me realize another limitation in the current Ansible inventory.