Use other facts in a custom fact

1.7k Views Asked by At

I am having a tough time with the following custom fact; details below.

The custom fact needs to look for a certain json file in the following folder. This displays the information when used within a manifest. But, when I add it to the custom fact, it does not work.

"/opt/${::hostname}/${::custom_variable}_${::fqdn}.json"

However, if I hard code the values as shown below, it works fine.

"/opt/host1.domain.com/mycompany_host1.json"

Note that the custom variable is defined on the Puppet console against "classification."

1

There are 1 best solutions below

5
On

If you need to use facts within a custom fact, then you have to access them using Facter's .value method. Their values are accessible when the facts are referenced as symbol arguments to that method (e.g. Facter.value(:hostname)). To be able to use the Facter class, you have to require it in your Ruby file for the custom fact with:

require 'facter'

Then, you can use the variables in your above example in the normal way with string interpolation:

"/opt/#{Facter.value(:hostname)}/#{Facter.value(:custom_variable)}_#{Facter.value(:fqdn)}.json"

Note that the custom_variable fact needs to be assigned on a system during pluginsync before use in this custom fact. Also, you switched hostname and fqdn in your example above, so be sure those align correctly for you when you implement this.

https://docs.puppet.com/facter/3.6/custom_facts.html#using-other-facts