Regular expressions

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:


This kind of pattern is called a regular expression, or ...

Get Puppet 3 Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.