3

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

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
souser
  • 5,868
  • 5
  • 35
  • 50

1 Answers1

3

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

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • The hostname and fqn worked as expected. +1 for that. However, the custom fact did not work. Can you elaborate on how I can ensure that it gets assigned during pluginsync ? I have configured it in the Puppet console in the "Classification" tab. – souser May 11 '17 at 02:49
  • @user So those variables become top-scope Puppet variables which are not accessible during pluginsync unfortunately because the variables are not available as part of the REST API endpoints from the classifier: https://docs.puppet.com/pe/latest/nc_index.html. Could you elaborate on the logic you are using to set those variables so I can incorporate that into the Ruby code? – Matthew Schuchard May 11 '17 at 12:01
  • that is it really. All I need to do is put the contents of that file into a variable. Since I cannot do that directly via puppet manifest, I am writing this custom fact. It just happens that the path that the file happens to be in, contains some variables. – souser May 11 '17 at 15:41
  • @user Ok, well if you decide to share the logic for the custom_variable then I can help you finish this off. – Matthew Schuchard May 11 '17 at 15:54
  • not sure what else is missing. All it is, is a variable with a value. – souser May 11 '17 at 17:25