Puppet condition

Conditional Statements

Conditional statements let your Puppet code behave differently in different situations. They are most helpful when combined with facts or with data retrieved from an external source.

Conditionals that alter logic:

*if statement

*unless statement

*case statement

If Statements

“If” statements take a Boolean condition and an arbitrary block of Puppet code and will only execute the block if the condition is true. They can optionally include elsif and else clauses.

Syntax:

if condition {

block of code

}

elsif condition {

block of code

}

else {

default option

}

Example:

if $facts['os']['name'] == 'Windows' {

include role::windows

}

elsif ($facts['os']['name'] == 'RedHat') and ($facts['os']['name'] == 'CentOS') { include role::redhat

}

elsif $facts['os']['name'] =~ /^(Debian|Ubuntu)$/ {

include role::debian

}

else {

include role::generic::os

}

Behavior

*The Puppet if statement behaves like if statements in any other language.

*If none of the conditions match and there is no else block, Puppet will do nothing.

Conditions

*Variables

*Expressions, including arbitrarily nested and/or expressions

*Functions that return values

“If” Statements

Regex capture variables

If you use a regular expression match operator as your condition, any captures from parentheses in the pattern will be available inside the associated code block as numbered variables ($1, $2, etc.), and the entire match will be available as $0:

Example:

if $trusted['certname'] =~ /^www(\d+)\./ {

notice("This is web server number $1.")

}

*Unless Statements

Unless is the reversed “if” statements. It takes a Boolean condition and an arbitrary block of Puppet code. It will only execute the block of code if the condition is false. There cannot be an elseif clause.

Syntax:

unless condition {

block of code

}

Example:

unless $facts['memory']['system']['totalbytes'] > 1073741824 { $maxclient = 500

}

Behavior

*The condition is evaluated first and, if it is false, the code block is executed.

*If the condition is true, Puppet will do nothing.

*The unless statement is also an expression that produces a value and can be used wherever a value is allowed.

Conditions

*Variables

*Expressions, including arbitrarily nested and/or expressions

*Functions that return values

Regex capture variables

Although “unless” statements receive regex capture variables like “if” statements, they usually aren’t used.

*Case Statements

Similar to the “if” statements, case statements choose one of several blocks of arbitrary Puppet code.

Syntax:

case condition {

'control expression': { block of code }

default: { block of code }

}

Example:

case $facts['os']['name'] {

'Windows': { include role::windows }

'RedHat', 'CentOS': { include role::redhat }

/^(Debian|Ubuntu)$/: { include role::debian }

default: { include role::generic::os }

}

Behavior

*Compares the control expression to each of the cases in the order they are defined.

*The default case is always evaluated last.

*The code block for the first matching case is executed.

*A maximum of one code block will be executed.

*If none of the cases match, Puppet will do nothing.

Conditions

*Variables

*Expressions, including arbitrarily nested and/or expressions

Recent Comments

No comments

Leave a Comment