Variables store values so that those values can be accessed in code later.
After you've assigned a variable a value, you cannot reassign it. Variables depend on order of evaluation: you must assign a variable a value before it can be resolved.
Assigning variables
Prefix variable names with a dollar sign ($
). Assign values to variables with the equal sign (=
) assignment operator.
$content = "some content\n"
Assign variables using their short name within their own scope. You cannot assign values in one scope from another scope. For example, you can assign a value to the apache::params
class's $vhostdir
variable only from within the apache::params
class.
You can assign multiple variables at the same time from an array or hash.
[$a, $b, $c] = [1,2,3] # $a = 1, $b = 2, $c = 3
[$a, [$b, $c]] = [1,[2,3]] # $a = 1, $b = 2, $c = 3
[$a, $b] = [1, [2]] # $a = 1, $b = [2]
[$a, [$b]] = [1, [2]] # $a = 1, $b = 2
$a
variable and a value of 20 to the $b
variable.[$a, $b] = {a => 10, b => 20} # $a = 10, $b = 20
[$a, $c] = {a => 5, b => 10, c => 15, d => 22} # $a = 5, $c = 15
# scope-example.pp
# Run with puppet apply --certname www1.example.com scope-example.pp
$myvar = "Top scope value"
node 'www1.example.com' {
$myvar = "Node scope value"
notice( "from www1: $myvar" )
include myclass
}
node 'db1.example.com' {
notice( "from db1: $myvar" )
include myclass
}
class myclass {
$myvar = "Local scope value"
notice( "from myclass: $myvar" )
}
Resolution
You can use the name of a variable in any place where a value of the variable's data type would be accepted, including expressions, functions, and resource attributes. Puppet replaces the name of the variable with its value. By default, unassigned variables have a value of undef
. See the section about unassigned variables and strict mode for more details.
content
parameter value resolves to whatever value has been assigned to the $content
variable. The $address_array
variable resolves to an array of the values assigned to the $address1
, $address2
, and $address3
variables:file {'/tmp/testing':
ensure => file,
content => $content,
}
$address_array = [$address1, $address2, $address3]
Interpolation
Puppet can resolve variables that are included in double-quoted strings; this is called interpolation. Inside a double-quoted string, surround the name of the variable (the portion after the $
) with curly braces, such as ${var_name}
. This syntax is optional, but it helps to avoid ambiguity and allows variables to be placed directly next to non-whitespace characters. These optional curly braces are permitted only inside strings.
$homedir
.$rule = "Allow * from $ipaddress"
file { "${homedir}/.vim":
ensure => directory,
...
}
Scope
The area of code where a given variable is visible is dictated by its scope. Variables in a given scope are available only within that scope and its child scopes, and any local scope can locally override the variables it receives from its parents. See the section on scope for complete details.
$vhost
variable shows that the variable is found and assigned a value in the apache::params
class: $vhostdir = $apache::params::vhostdir
Variables can be assigned outside of any class, type, or node definition. These top scope variables have an empty string as their first namespace segment, so that the qualified name of a top scope variable begins with a double colon, such as $::osfamily
.
Unassigned variables and strict mode
By default, you can access variables that have never had values assigned to them. If you do, their value is undef
. This can be a problem, because an unassigned variable is often an accident or a typo. To make unassigned variable usage return an error, so that you can find and fix the problem, enable strict mode by setting strict_variables = true
strict_variables in the puppet.conf
file on your master and on any nodes that run puppet apply
. For details about this setting, see the configuration page.
Naming variables
Some variable names are reserved; for detailed information, see the reserved name page.
Variable names
Variable names are case-sensitive and must begin with a dollar sign ($
). Most variable names must start with a lowercase letter or an underscore. The exception is regex capture variables, which are named with only numbers.
-
Uppercase and lowercase letters
-
Numbers
-
Underscores (
_
). If the first character is an underscore, access that variable only from its own local scope.
Qualified variable names are prefixed with the name of their scope and the double colon (::
) namespace separator. For example, the $vhostdir
variable from the apache::params
class would be $apache::params::vhostdir
.
Optionally, the name of the very first namespace can be empty, representing the top namespace. The main reason to namespace this way is to indicate to anyone reading your code that you're accessing a top-scope variable, such as $::is_virtual
.
\A\$[a-z0-9_][a-zA-Z0-9_]*\Z
Qualified variable names match
the following regular expression:
\A\$([a-z][a-z0-9_]*)?(::[a-z][a-z0-9_]*)*::[a-z0-9_][a-zA-Z0-9_]*\Z