Basic string manipulation in puppet

5,554 views
Skip to first unread message

Aaron Nicoli

unread,
Feb 16, 2012, 5:50:27 PM2/16/12
to Puppet Users
Hi all,

Been searching the net for a few hours now.
I have a basic operation I wish to perform but, cannot find examples
close enough to what I'm after to implement it myself.

This is what I want to do:

$version = '10.2.0'

I want to manipulate the string to make a new var ($cutversion), equal
just 102.

For example in perl (since I know it):

$version = '10.2.0';
if ($version =~ /^(\d+)\.(\d+).*$/) {
$cutversion = $1 . $2;
}

Any help?

Thanks,
Aaron.

Stefan Schulte

unread,
Feb 16, 2012, 6:24:01 PM2/16/12
to Puppet Users

There are not that many string manipulating functions in puppet itself
but you can always build your own [1] or have a look at the puppet-std
[2]

However your problem can be solved with the build-in function regsubst
which can be found in the function reference [3]

This should work (while untested)

$cutversion = regsubst($version, '^(\d+)\.(\d+).*$', '\1\2')

Does this help?

-Stefan


[1] http://docs.puppetlabs.com/guides/custom_functions.html
[2] https://github.com/puppetlabs/puppetlabs-stdlib
[3] http://docs.puppetlabs.com/references/2.7.9/function.html#regsubst

-Stefan

Aaron Nicoli

unread,
Feb 16, 2012, 7:36:00 PM2/16/12
to Puppet Users
Stefan,

Your the man, thanks for the help, exactly the kind of thing I am
after!

Cheers dude,

On Feb 17, 10:24 am, Stefan Schulte <stefan.schu...@taunusstein.net>
wrote:
Reply all
Reply to author
Forward
0 new messages