dropdown menu


In Puppet, facter is a standalone tool that holds the environment level variables. In can be considered similar to env variable. In Puppet, the key-value pair is known as “fact”.  We can list what Facter knows about the system by using command: facter

# facter
facterversion => 3.14.9
filesystems => ext2,ext3,ext4,xfs
fips_enabled => false
hypervisors => {
  virtualbox => {
    revision => "136177",
    version => "6.1.4"

is_virtual => true
kernel => Linux
kernelmajversion => 4.18
kernelrelease => 4.18.0-147.el8.x86_64
kernelversion => 4.18.0

Facter command can be used to list all the different environment variables and its associated values. These collection of facts comes with facter out-of-the-box and are referred to as core facts. One can add custom facts to the collection. Facter and facts are available throughout Puppet code as “global variable”, which means it can be used in the code at any point of time without any other reference.

file {'/tmp/userfile.txt': 
      ensure => file, 
      content => "the value for the 'OperatingSystem' fact is: $OperatingSystem \n", 

  ensure  => file,
  path    => '/etc/motd',
  mode    => '0644',
  content => "IP address is ${ipaddress}, hostname is ${fqdn}. It is running ${operatingsystem} ${operatingsystemrelease} and Puppet ${puppetversion}",


There are three types of fact that can be used and defined: Core Facts, Custom Facts, External Facts

Core Facts

Core facts are defined at the top level and accessible to all at any point in the code.

Just before an agent requests for a catalog from the master, the agent first compiles a complete list of information available in itself in the form of a key value pair. Facter data is treated as global variable. The facts are then available as top level variable and the Puppet master can use them to compile the Puppet catalog for the requesting agent. Facters are called in manifest as normal variable with $ prefix.

if ($OperatingSystem == "Linux") { 
   $message = "This machine OS is of the type $OperatingSystem \n" 
} else { 
   $message = "This machine is unknown \n" 

file { "/tmp/machineOperatingSystem.txt": 
   ensure => file, 
   content => "$message" 

[root@puppetagent1 /]# facter OperatingSystem 

[root@puppetagent1 /]# puppet apply /tmp/ostype.pp 
Notice: Compiled catalog for puppetagent1.codingbee.dyndns.org 
in environment production in 0.07 seconds 
Notice: /Stage[main]/Main/File[/tmp/machineOperatingSystem.txt]/ensure: 
defined content as '{md5}f59dc5797d5402b1122c28c6da54d073' 
Notice: Finished catalog run in 0.04 seconds  

[root@puppetagent1 /]# cat /tmp/machinetype.txt 
This machine OS is of the type Linux

Custom Facts

Using the “export FACTER” Syntax one can manually add the facts using the export FACTER_{fact’s name} syntax.

[root@puppetagent1 facter]# export FACTER_tallest_mountain="Everest" 
[root@puppetagent1 facter]# facter tallest_mountain Everest

External Fact

On the agent machine, we need to create a directory as mentioned below.

$ mkdir -p /etc/facter/facts.d
Create a Shell script in the directory with the following content.

$ ls -l /etc/facter/facts.d 
total 4 
-rwxrwxrwx. 1 root root 65 Sep 18 13:11 external-factstest.sh 
$ cat /etc/facter/facts.d/external-factstest.sh 
echo "hostgroup = dev" 
echo "environment = development"
Change the permission of the script file.

$ chmod u+x /etc/facter/facts.d/external-facts.sh
Once done, we can now see the variable present with the key/value pair.

$ facter hostgroup 
$ facter environment 

No comments:

Post a Comment