Ask Your Question

string slicing puppet

asked 2018-07-17 16:50:17 -0600

Dan1 gravatar image


In the yaml file I have this variable:

Deploy_path: 'C:\Program Files\Apache Software Foundation\Tomcat8116'

Now I created a new variable in the pp file:

$base_deployment = hiera('Deploy_path')

I would like to create another variable which contains only the last part of the base_deployment, I mean - I would like my variable contain the value Tomcat8116.

How can I slice base_deployment variable into a new variable which contains only the Tomcat8116 part?


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2018-07-18 09:34:08 -0600

DarylW gravatar image

updated 2018-07-23 07:07:04 -0600

Using a combination of split() and the general array notation, you should be able to break that string into an array, and access the last member.

$base_deployment = lookup('Deploy_path')
$base_deployment_array = split($base_deployment, '\') 
$basename = $base_deployment_array[-1]

or if you want to be clever

$basename = split( lookup('Deploy_path') , '\' )[-1]

I haven't run this through puppet, so you may need to do "\\" or '\\' instead of '\' due to special character quoting.


edit flag offensive delete link more



Cannot have a space between the expression and the access operator `[]`

Henrik Lindberg gravatar imageHenrik Lindberg ( 2018-07-23 05:24:11 -0600 )edit

`$basename = hiera('Deploy_path').split('/')[-1]` Reads more nicely from left to right ;-) Also - hiera() functions are deprecated - switch to using `lookup`.

Henrik Lindberg gravatar imageHenrik Lindberg ( 2018-07-23 05:27:10 -0600 )edit

Thanks for the reminder Henrik, We are still using puppet3 here, so i'm not used to all the nice new things puppet has to offer ( we just recently upgraded from 2.7 due to openssl upgrades that finally broke it on our systems). Updated my answer to reflect your comments!

DarylW gravatar imageDarylW ( 2018-07-23 07:08:26 -0600 )edit

Thanks a lot, it solved my problem :)

Dan1 gravatar imageDan1 ( 2018-07-25 12:02:09 -0600 )edit

answered 2018-07-23 08:04:31 -0600

fvoges gravatar image

You can also use the dirname() function from puppetlabs/stdlib

edit flag offensive delete link more


Ah, I think you mean basename ( ), but yea, I forgot that one was added to stdlib, nice pointer!

DarylW gravatar imageDarylW ( 2018-07-23 08:11:52 -0600 )edit

Yeh, I meant basename :)

fvoges gravatar imagefvoges ( 2018-07-23 09:19:38 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-07-17 16:50:17 -0600

Seen: 230 times

Last updated: Jul 23