Ask Your Question

How to add a new element to the array without reassigning?

asked 2015-09-18 11:47:02 -0500

gin gravatar image

updated 2015-09-19 18:13:08 -0500

ramindk gravatar image


just a simple question, how do I add a new element to the array in Puppet 3.7.2 WITHOUT reassigning the variable?

Suppose I have $array = ['1'] in a small dynamic loop, thus I cannot (or don't want) use new variables to store my new array. I want to add new values to the already existing array $array.

EDIT: Thanks for the reply syphrix. I did try this.

$bla = [1]   
concat($bla, [2])  
notify{"The value is: ${bla}": }

And the result is:

Notice: The value is: [1]
Notice: /Stage[main]/Scripts/Notify[The value is: [1]]/message: defined 'message' as 'The value is: [1]'

Am I missing something obvious?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-09-19 21:35:16 -0500

GregLarkin gravatar image

updated 2015-09-19 21:35:37 -0500

Hi there,

Unfortunately, you cannot reassign variables in Puppet, no matter what. The reason your code is not working is that the concat function returns an rvalue, meaning its return value needs to be assigned to some other variable or it needs to be part of an expression evaluation.

So you can do this:

$bla = [1]
$blargh = concat($bla, [2])
notify{"The value is: ${blargh}": }

But not this:

$bla = [1]   
$bla = concat($bla, [2])  
notify{"The value is: ${bla}": }

Also see:

edit flag offensive delete link more


Thanks, Greg. I'll mark this answer as it expands more on the subject.

gin gravatar imagegin ( 2015-09-21 07:23:44 -0500 )edit

answered 2015-09-18 16:44:10 -0500

syphrix gravatar image

You can accomplish this with the concat function from the puppetlabs-stdlib


Appends the contents of multiple arrays onto the first array given. For example:

  • concat(['1','2','3'],'4') returns ['1','2','3','4'].
  • concat(['1','2','3'],'4',['5','6','7']) returns ['1','2','3','4','5','6','7']. Type: rvalue.
edit flag offensive delete link more


Thanks! Quite a shame this restriction exists. Just wanted to be sure if that's the case.

gin gravatar imagegin ( 2015-09-21 07:21:24 -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

1 follower


Asked: 2015-09-18 11:47:02 -0500

Seen: 2,597 times

Last updated: Sep 19 '15