Ask Your Question

Split string into multiple variables

asked 2013-10-08 22:10:17 -0500

STOIE gravatar image

updated 2013-10-08 22:14:59 -0500

Hi there,

I currently use the following in my manifests:

$items = 'item1,item2,item3'

$curitem = split($items, ',')
# Insert loop here...

But, now I want to add some more functionality... so each item can have an associated value: Something like:

$items = 'item1:one,item2:two,item3:three'

$curitem = split($items, ',')
# Insert loop here...
$itemname, $itemvalue = split($curitem, ':')

Does anyone know if this is possible in a puppet manifest? And if so, how it can be done...?

Guys / Gals, I would really really appreciate you're help / input on this one :D


edit retag flag offensive close merge delete


You can take a look on inline_template() function which is use for executing ruby code in between the puppet manifests. Another way of executing Ruby code is creating .rb files ...(more)

rahul khengare gravatar imagerahul khengare ( 2013-10-09 04:23:01 -0500 )edit

Hi there, I was looking into the inline_template function, but, if you declare a var in the ruby template, can you pass it back to the puppet code? As for ...(more)

STOIE gravatar imageSTOIE ( 2013-10-09 04:32:08 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2013-10-10 17:43:47 -0500

STOIE gravatar image

Okay so, ended up getting this working... Here's how...

I have a custom fact that is outputting the following: (it's called $disks)


Which is "disk mount":"size in GB",...

This is what my Nagios manifest looks like:

$disksarray = split($disks, ',')
define nag_disks {
  $curdiskinfo = split($name, ':')
  $curdisk = $curdiskinfo[0]
  $cursize = $curdiskinfo[1]
  # actual work...
nag_disks { $disksarray:; }

And it's working perfectly. Has fixed all those Nagios errors, that just aren't problems...

edit flag offensive delete link more

answered 2013-10-09 03:42:29 -0500

updated 2013-10-09 05:48:01 -0500

It looks like hashes could do this job better then strings.

F.e. your code might look this way:

$items = { item1 => 'one', item2 => 'two', item3 => 'three' }

Update You didn't ask about converting string to hash, but I assume that it might be useful. Cause hash-es are extremely useful and flexible. F.e. it allows to create multiple resources of the same type using create_resources function.

So here is how to convert string (in your format) to hash.

Three functions are used:

  • regsubst - to replace ':' with ','. (built-in in puppet)
  • split - to convert string to array. (built-in in puppet)
  • hash - to ...

edit flag offensive delete link more


Hi there, Unfortunately, the string is actually being passed in from a facter var... and as far as I can tell facter can only return strings?!?

STOIE gravatar imageSTOIE ( 2013-10-09 04:30:40 -0500 )edit

I updated my answer, adding example on how to convert string to hash, and explanation on why it might be useful.

Aleksey Timohin gravatar imageAleksey Timohin ( 2013-10-09 05:48:51 -0500 )edit

Awesome, thanks for the comprehensive answer. I'm already using the create_resources functions with hiera hashes, so will see if I can adopt it for this operation. Note. the actual ...(more)

STOIE gravatar imageSTOIE ( 2013-10-09 06:37:14 -0500 )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


Asked: 2013-10-08 22:10:17 -0500

Seen: 20,298 times

Last updated: Oct 10 '13