Puppet is two things:
Announcement
You can find all my latest posts on medium.- It is a command line tool for querying node specific information
- It is a place where a node specific key-value hash is stored.
Facter is a standalone tool that house’s environment-level variables. In that sense, It is a bit like the bash equivalent of the “env” command, in fact there is an overlap between info stored in facts and info stored in the “env” command.
Each key-value in facter is referred to as “facts”. You can view all the facts and their values using the facter command on it’s own:
$ facter
Note, if the above comamand doesn’t give the fact value that you’re looking for, then also try:
$ facter -p
This will list all the various environment variables, and their respective values. These collection of facts comes with facter straight out of the box and are referred to as core facts. However you can add your own “custom facts” to this collection, which we will cover later.
If you just to view one variable’s value, then you can just do:
facter {variable-name}
For example:
[root@puppetmaster ~]# facter virtual virtualbox
Also some facter data is in a nested structure, and to retrieve a nested fact, then you need to give the fully qualified name. For example here’s an example of a nested fact:
$ facter -p os { architecture => "x86_64", family => "RedHat", hardware => "x86_64", name => "CentOS", release => { full => "6.8", major => "6", minor => "8" }, selinux => { enabled => false } }
Now if you want to retrieve the ‘full’ info:
$ facter -p os.release.full 6.8
However your puppet manifests you would refer to this fact like this:
facts['os']['release']['full']
However the key thing that makes facter important to puppet, is that facters facts are available throughout your puppet code as “global variables”. Here is an example:
[root@puppetmaster modules]# tree user_account user_account └── manifests └── init.pp 1 directory, 1 file [root@puppetmaster modules]# cat user_account/manifests/init.pp class user_account { user { 'homer': ensure => 'present', uid => '121', shell => '/bin/bash', home => '/home/homer', } file {'/tmp/testfile.txt': ensure => file, content => "the value for the 'osfamily' fact is: $osfamily \n", } } [root@puppetmaster modules]# puppet agent --test Notice: /Stage[main]/Activemq::Service/Service[activemq]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Activemq::Service/Service[activemq]: Unscheduling refresh on Service[activemq] Notice: Finished catalog run in 4.09 seconds [root@puppetmaster modules]# cat /tmp/testfile.txt the value for the 'osfamily' fact is: RedHat [root@puppetmaster modules]# facter osfamily RedHat [root@puppetmaster modules]#
Notice here, that we didn’t define $osfamily, we just used it like it was a normal variable. However there are other ways to refer to a fact in your puppet code:
$fact_name
$::fact_name
– this is better practice$facts['fact_name'] - this is best practice, put only works from puppet 3.7 onwards
If
In puppet, each of these environment variables are referred to as “facts”. There are 3 types of facts:
– core facts
– custom facts
– external facts
All core facts are defined in the top level scope and are accessible anywhere in your code.
You can create your own facter on the bash command line like this:
export FACTER_{facter_name}="string" # e.g.: [root@puppetmaster /]# export FACTER_alphabet="abcdefg" [root@puppetmaster /]# facter alphabet abcdefg [root@puppetmaster /]#
There is another place that you can access environment, these are puppet specific config settings, which are accessble through:
puppet config print http://serverfault.com/questions/725595/extract-nested-hash-facts-from-puppets-facter-command-line-tool-how?newreg=03145631d44641e688bb6ef527e91600
http://terrarum.net/blog/puppet-infrastructure.html
https://docs.puppetlabs.com/facter/latest/fact_overview.html#
https://docs.puppetlabs.com/puppet/latest/reference/lang_facts_and_builtin_vars.html
https://docs.puppetlabs.com/facter/latest/core_facts.html
https://docs.puppetlabs.com/facter/latest/custom_facts.html