We've seen a couple of different ways of testing string values already. You can compare strings for equality:
if $role == 'webserver' { ... }
You can also test whether one string is a substring of another:
if 'dunk' in 'doughnuts' { ... }
But what if you want to test for patterns of characters? Say, app
followed by any characters, followed by staging
. Puppet has a special pattern-matching language you can use for this:
if $::hostname =~ /app.*staging/ { ... }
This expression will be true if $::hostname
is any of the following, and many more:
app_staging
app-1-staging
application_staging
appstaging
my_app_staging_server
Note the slash characters surrounding the pattern:
/app.*staging/
This kind of pattern is called a regular expression, or regex for short, and Puppet uses the slash character (/
) to mark the start and end of regular expressions.