if
statements
An "if" statement
takes a Boolean condition and an arbitrary block of Puppet
code, and executes the code block only if the condition is true. Optionally, an if
statement can include elsif
and else
clauses.
Behavior
Puppet's if
statements behave much
like those in any other language. The if
condition
is evaluated first and, if it is true, the if
code
block is executed. If it is false, each elsif
condition (if present) is tested in order, and if all conditions fail, the else
code block (if present) is executed. If none of the
conditions in the statement match and there is no else
block, Puppet does nothing and
moves on. If statements executes a maximum of one code block.
In addition to executing the code in a block, an if
statement also produces a value, so the if
statement
can be used wherever a value is allowed.The value of an if
expression is the value of the last expression in the executed
block, or undef
if no block was executed.
Syntax
An if
statement consists of:
The
if
keyword.A condition (any expression resolving to a Boolean value).
A pair of curly braces containing any Puppet code.
Optionally: any number of
elsif
clauses, which are processed in order.Optionally: the
else
keyword and a pair of curly braces containing Puppet code.
elsif
clause consists of: The
elsif
keyword.A condition.
A pair of curly braces containing any Puppet code.
if $facts['is_virtual'] { # Our NTP module is not supported on virtual machines: warning('Tried to include class ntp on virtual machine; this node might be misclassified.') } elsif $facts['os']['name'] == 'Darwin' { warning('This NTP module does not yet work on our Mac laptops.') } else { # Normal node, include the class. include ntp }
Conditions
The condition of an if
statement can be any expression that resolves
to a Boolean value. This includes:
-
Expressions, including arbitrarily nested
and
andor
expressions -
Functions that return values
Expressions that resolve to non-Boolean values are automatically converted to Booleans. For more information, see the Booleans documentation.
Regex capture variables
If you use the regular expression match operator in a condition, any captures from
parentheses in the pattern are available inside the associated code block as
numbered variables (for example, $1
, $2
), and the entire match is available as $0
. This example captures any digits from a hostname
such as www01
and www02
, and stores them in the $1
variable:
if $trusted['certname'] =~ /^www(\d+)\./ { notice("Welcome to web server number $1.") }
Regex capture variables are different from other variables in a couple of ways:
-
The values of the numbered variables do not persist outside the code block associated with the pattern that set them.
-
In nested conditionals, each conditional has its own set of values for the set of numbered variables. At the end of an interior statement, the numbered variables are reset to their previous values for the remainder of the outside statement. This causes conditional statements to act like local scopes, but only with regard to the numbered variables.