Using regular expressions in if statements

"Some people, when confronted with a problem think; 'I know, I'll use regular expressions.' Now they have two problems."—Jamie Zawinski

Another kind of expression you can test in if statements and other conditionals is the regular expression. A regular expression is a powerful way of comparing strings using pattern matching.

How to do it…

Add the following to your manifest:

if $lsbdistdescription =~ /LTS/ {
    notice("Looks like you are using a Long Term Support version of Ubuntu.")
} else {
    notice("You might want to upgrade to a Long Term Support version of Ubuntu...")
}

How it works…

Puppet treats the text supplied between the forward slashes as a regular expression that specifies what the text to be matched ...

Get Puppet 2.7 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.