A collection of task oriented solutions in Puppet


Using the Facts hash


You want to use the facts hash in your manifests.


class facts_hash {

  # display a simple value 
  $virt = $facts['virtual']
  notify { "I'm using a value !${virt}! ": }

  # fetch a subhash
  $partitions = $facts['partitions']
  notify { "Displaying a sub hash ${partitions}": }

  # access a nested value
  $major_release = $::facts['os']['release']['major']
  notify { "My major release version is ${major_release}": }



It can be difficult to determine where a variable gets its value from in your manifests. Is it a fact? Could it be a local variable? Using the $facts hash makes it easier to see where values come from.

The $facts hash became available in puppet 3.5 and was enabled by default from puppet 4.0 so there is no longer a reason not to use it, even in manifests you publish to the internet.

It is worth noting that the $facts array is, by default, immutable and any attempts to write to it will cause an error.

class invalid_assign {

  $facts = 'Not allowed'

# Error: Could not retrieve catalog from remote server: 
# Error 400 on SERVER: 
# Evaluation Error: 
# Error while evaluating a '=' expression,
# Attempt to assign to a reserved variable name: 'facts' at /etc/puppetlabs/code/environments/production/modules/facts_hash/manifests/init.pp:4:10 on node testy

See also