Ask Your Question

create an array with x entries

asked 2015-10-07 01:36:09 -0500

tp gravatar image

Hey, I would like maintain different instances of a service on one system. Count of service instances depends on fact processorcount.

  • processorcount == 1 => 1 service instance
  • processorcount == 2 => 2 service instances ...

As far as I know, there is no loop over an integer, follow snippet is impossible (because $i is immutable).

while ($i <=$processorcount){ #do something $i++ }

So I would like create an array with $processorcount entries, and then use this array for a loop.

Is it possible to create an array with x entries in a .pp file? And if yes, how? :)

Best regards, thomas.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-07 07:31:13 -0500

llowder gravatar image

For the looping, you can use each


$my_array.each |$index| { ... }

Getting the dynamically sized array is a bit trickier, and I don't know of a good way to do that.

However, the each function can work with an integer, so you would need to make sure that $processorcount is an integer.

To be sure, you can do something like:

$int_processorcount = $processorcount + 0
$int_processorcount.each |$index| {

This is assuming that you are using Puppet 4.x or a late version from the 3.x series with future parser enabled.

edit flag offensive delete link more


Thank you for your help. I overlook "or something that is of enumerable type (integer, Integer range, or String)" in each documentation. I found in the meantime range() $my_array = range("1", "$pr_myclass::instances") $my_array.each |Integer $value| { #... }

tp gravatar imagetp ( 2015-10-08 03:21:26 -0500 )edit

I'm seeing this error Syntax error at '.'; expected '}', how do I fix it?

yzhang gravatar imageyzhang ( 2016-03-09 13:21:50 -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-10-07 01:36:09 -0500

Seen: 96 times

Last updated: Oct 07 '15