0

How to iterate over an array in Puppet

// Assuming fact my_env => [ shared1, shared2, shared3 ]

define myResource {
 file { "/var/tmp/$name":
    ensure => directory,
    mode   => 0600,
  } 
  user { $name:
    ensure => present,
  }
 }
 myResource { $my_env: }

I tried this but $name got the whole array.

Am i doing something wrong?

Community
  • 1
  • 1
SAM
  • 61
  • 1
  • 1
  • 6

2 Answers2

1

If you are using Facter 1.x, all values that are returned from fact code are implicitly converted to string. In the case of arrays, all elements are conatenated.

As a workaround, you can make your fact do a proper concatenation, e.g. joining the values with commas.

setcode do
  result = []
  # ... code to store an array in #{result}
  result * ','
end

In your manifest, you can turn this back into a proper array using the split function.

$my_env_arr = split($::my_env, ',')
my_resource { $my_env_var: }

Array facts Just Work with Facter 2.x.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
0

@felix-frank

On the second line in your second code example, did you mean to use

my_resource { $my_env_var: }

or

my_resource { $my_env_arr: }

I ask because the latter makes much more sense to me, and the variable name would match what is shown in the first line of that example.

Community
  • 1
  • 1
Brad Knowles
  • 131
  • 1
  • 4