Learning Kit

Puppet Language Basics

Variables

Time: 5 minutes
Materials needed: none

As you write Puppet code, you may find yourself repeating the same values again and again. To save time and minimize errors, you can assign those repeating values to variables.

1
$variable_name = variable_content

Variables begin with a $ (dollar sign) and the name of the variable, followed by an = (equal sign), and finally the variable’s value. Variable names are case sensitive and must start with a lowercase letter or _ (underscore).

1
2
3
class { 'motd':
  content => “this is the message of the day for the ${facts[‘environment’]} infrastructure”,
}

Or

1
2
3
4
$message = ‘’Welcome to the $facts[‘environment’] infrastructure“
class { 'motd':
  content => $message,
}

Note that a variable’s value is wrapped in "" (double quotes), as opposed to the ” (single quotes) you’ve seen so far in resource and class declarations.

Any code inside '' (single quotes) will be read by your system literally. Wrapping variable values in “” (double quotes) will tell your system to interpolate that code instead. Puppet agents will see the “” (double quotes) and understand that the string is a variable, and to replace that code with the variable’s value automatically.

Puppet sites use proprietary and third-party cookies. By using our sites, you agree to our cookie policy.