Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (29.3k points)

Is it possible to do a string substitution/transformation in Puppet using a regular expression?

If $hostname is "web1", I want $hostname_without_number to be "web". The following isn't valid Puppet syntax, but I think I need something like this:

$hostname_without_number = $hostname.gsub(/\d+$/, '')

1 Answer

0 votes
by (50.2k points)

There's a regular expression substitution function built-in. It probably calls the same underlying gsub function.

$hostname_without_number = regsubst($hostname, '\d+$', '')

Or if you prefer to actually call out to Ruby, you can use an inline ERB template:

$hostname_without_number = inline_template('<%= hostname.gsub(/\d+$/, "") %>')

Reference: http://docs.puppetlabs.com/references/2.7.3/function.html

Browse Categories

...