Parameters and Variables in Puppet

A parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine.

Puppet modules are sets of instructions to configure systems. They usually divide into functions and data. The function defines the action to take, and take data as input. Data should never be hard coded into the functions, instead we want to provide them through variables, which take the data from oneor more sources through parameters.

So parameters are basically key-value pairs like this:

$key         = 'value'
$foo         = 'bar'
$reqpackages = 'ntp'
$pkg_ensure  = 'latest'
$ntp_server  = 'example.example.net'

The first set in the pair is the variable, the second set the default data, which ideally should be overridden through Hiera or External Node Classifiers (ENC). A very good example for an ENC is Foreman

Puppet modules provided here at the ConfDroid are always following a similar structure, and parameters are always contained in params.pp. They are then inherited to the various classes. One exception are Defines, which cannot inherit parameters. The reason for the centralized parameter stash is simplification. When troubleshooting a module in case something does not work as expected, you do not want to have to search every single class for a potential hidden parameter.

There are mainly two cases where variables are effectively used:

  • Puppet classes:
class ntp (

) inherits ntp::params.pp {

  package { $reqpackages:
    ensure => $pkg_ensure,
  }
}
  • ERB templates
# this is an ERB template
ntp server = <%= @ntp_server %>

In order for ENCs like Foreman to recognize parameters, they must be located in the class signature:

class ntp::params (
# this is the class signature,  within the () Parameters contained here are recognized by Foreman. They must be separated by comma. Parameters in the class signature cannot be concatenated., with the exception of  facts
$key         = 'value',
$foo         = 'bar',
$reqpackages = 'ntp',
$pkg_ensure  = 'latest',
$ntp_server  = 'example.example.net',
$domainname  = $::domain # this is a fact

) {

# This is not the class signature. Parameters here are recognized by the puppet module, but not by Foreman. Paramaters located here  must not  be separated by comma, and they can be concatenated:

$another_key  = 'another value'
$bar          = 'foo'
$ntp_fqdn     = "${ntp_server}.${::domainname}"

Leave a Reply