Getting information about the environment
Often in a Puppet manifest, you need to know some local information about the machine you're on. Facter is the tool that accompanies Puppet to provide a standard way of getting information (facts) from the environment about things such as these:
- Operating system
- Memory size
- Architecture
- Processor count
To see a complete list of the facts available on your system, run:
$ sudo facter architecture => amd64 augeasversion => 0.10.0 domain => compute-1.internal ec2_ami_id => ami-137bcf7a ec2_ami_launch_index => 0
Note
While it can be handy to get this information from the command line, the real power of Facter lies in being able to access these facts in your Puppet manifests.
Some modules define their own facts; to see any facts that have been defined locally, add the -p (pluginsync)
option to facter as follows:
$ sudo facter -p
How to do it…
Here's an example of using Facter facts in a manifest:
- Reference a Facter fact in your manifest like any other variable. Facts are global variables in Puppet, so they should be prefixed with a double colon (
::
), as in the following code snippet:notify { "This is $::operatingsystem version $::operatingsystemrelease, on $::architecture architecture, kernel version $::kernelversion": }
- When Puppet runs, it will fill in the appropriate values for the current node:
[root@hiera-test ~]# puppet agent -t ... Info: Applying configuration version '1411275985'Notice: This is RedHat version 6.5, on x86_64 architecture, kernel version 2.6.32 ... Notice: Finished catalog run in 0.40 seconds
How it works…
Facter provides a standard way for manifests to get information about the nodes to which they are applied. When you refer to a fact in a manifest, Puppet will query Facter to get the current value and insert it into the manifest. Facter facts are top scope variables.
Tip
Always refer to facts with leading double colons to ensure that you are using the fact and not a local variable:
$::hostname
NOT $hostname
There's more…
You can also use facts in ERB templates. For example, you might want to insert the node's hostname into a file, or change a configuration setting for an application based on the memory size of the node. When you use fact names in templates, remember that they don't need a dollar sign because this is Ruby, not Puppet:
$KLogPath <%= case @kernelversion when '2.6.31' then '/var/run/rsyslog/kmsg' else '/proc/kmsg' end %>
When referring to facts, use the @
syntax. Variables that are defined at the same scope as the function call to template can also be referenced with the @
syntax. Out of scope variables should use the scope
function. For example, to reference the mysql::port
variable we defined earlier in the mysql
modules, use the following:
MySQL Port = <%= scope['::mysql::port'] %>
Applying this template results in the following file:
[root@hiera-test ~]# puppet agent -t ... Info: Caching catalog for hiera-test.example.com Notice: /Stage[main]/Erb/File[/tmp/template-test]/ensure: defined content as '{md5}96edacaf9747093f73084252c7ca7e67' Notice: Finished catalog run in 0.41 seconds [root@hiera-test ~]# cat /tmp/template-test MySQL Port = 3306
See also
- The Creating custom facts recipe in Chapter 9, External Tools and the Puppet Ecosystem