CoursesPuppetIntermediate

Variables, facts & Facter

Node data and conditionals.

Intermediate12 min · lesson 6 of 12

Facts are data about a node, gathered by Facter at the start of every run — OS family and version, network interfaces, memory, CPU, virtualization, custom facts you add. They are available as variables in your manifests, so you write portable code that branches on the node’s reality: install the right package name per OS, size a setting from real memory, behave differently in a container versus bare metal.

terminal
$ facter os.family memory.system.total
os.family => Debian
memory.system.total => 7.76 GiB
# used in a manifest:
$pkg = $facts['os']['family'] ? { 'Debian' => 'apache2', default => 'httpd' }
package { $pkg: ensure => installed }

Variables and scope

Puppet variables are immutable within a scope — you assign once, you cannot reassign, which enforces declarative thinking (no accumulating state). They are typed (String, Integer, Array, Hash, and richer data types) and you should reference facts through the $facts hash for clarity. Combined with selectors, case statements, and conditionals, facts let one manifest correctly configure a diverse fleet.

manifest
case $facts['os']['family'] {
'Debian': { $svc = 'ssh' }
'RedHat': { $svc = 'sshd' }
default: { fail("Unsupported OS: ${facts['os']['family']}") }
}
service { $svc: ensure => running, enable => true }
Variables are immutable — that is a feature
You cannot reassign a Puppet variable in the same scope, and this catches people used to imperative languages. It is deliberate: it keeps the model declarative and prevents order-dependent state. Structure logic with selectors, conditionals, and separate variables rather than reassigning, and lean on it — immutability is what makes a manifest’s meaning independent of evaluation order.