Using regular expressions in if statements
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…
This is one example of using a regular expression in a conditional statement:
Add the following to your manifest:
if $::lsbdistdescription =~ /LTS/ { notify { 'Looks like you are using a Long Term Support version of Ubuntu.': } } else { notify {'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 specifying the text to be matched. If the match succeeds, the if
expression will be true and so the code between the first set of curly braces will be executed.
If you wanted instead to do something if the text does not match, use !~
rather than =~
:
if $::lsbdistdescription !~ /LTS/ {
There's more…
Regular expressions are very powerful...