Puppet Facter & Facts
In Puppet, we can put several values as an environment variable with the help of a facter. Facter is a tool of puppet, which is used to keep the environment level variable. It is exactly like an environment variable of Bash or Linux. Sometimes the information that is stored in facts and the machine's environment variable may overlap.
The key-value pair of the puppet is called as fact. Each resource has its own facts, and we can construct custom facts in Puppet.
This command lists all the different environment variables and their related values.
If you want to see only one variable, use the following command:
The main feature of facter and facts in puppet is that these are available throughout puppet code as "global variable", which means we can use it in any part of our code as per our requirement.
In puppet, three types of fact can be used:
At the top level, the core facts are described and available to everyone at any point in the code. List of some basic core facts are:
To see the full list of facts and values use the following command:
We can also add the custom facts to the node in the following ways:
In Ruby, $LOAD_PATH is similar to the Bash $PATH variable, but it is not an environment variable; instead, it is a pre-defined variable.
In $LOAD_PATH "$" variable is an array to search and load the values.
Now, create a directory facter, add a .pp file and append the content to it:
Add the following content to the custom_facts.rb file:
Facter scans through the entire folder listed in $LOAD_PATH, and search for a directory called facter. Once it gets that particular directory, it will put them anywhere in the directory structure. If this folder is found, then it searches for any Ruby file in that facter directory and loads all the specified facts in the memory.
FACTERLIB is similar to $LOAD_PATH. There is only one difference in both, i.e., FACTERLIB is an OS-level environment parameter, and $LOAD_PATH is a Ruby special variable.
The environment variable may not be configured by default. Let's check:
To test the FACTERLIB, use the following steps:
Create a directory named test_facts.
To see the directory structure execute the following command:
Copy the following contents to the .rb files:
Now, use the export statement:
Now, test the new facter:
If you want to apply some new facts created at the provisioning stage, then you can use external facts. External facts are the best way to apply metadata to a virtual machine (VM) at its provisioning time. For example, OpenStack, vSphere, AWS, etc.
Puppet can use all of the metadata and its generated information to decide what items should be included in the catalog that will be applied.
Next TopicPuppet File Server