Enumerating Puppet Arrays

86 views
Skip to first unread message

Joaquin Menchaca

unread,
Apr 2, 2014, 6:03:42 PM4/2/14
to puppet...@googlegroups.com
Is there a way to do something like this:

class make_it_so {
  $scripts = ["a.rb", "b.rb"]

  cron { $scripts:
    command     => "/path/$scripts",
    user        => 'deploy',
    hour        => '*/4',
    minute      => '0',
  }

}

The attribute usage of the array will just take the entire array concatenated together. :'(

I would like to have each resource declaration have a correspond to a matching element of the array

I would like the attribute with $scripts to match to the iteration of the resource declaration $scripts.

Christopher Wood

unread,
Apr 2, 2014, 7:07:04 PM4/2/14
to puppet...@googlegroups.com
Check out defined types. For a faked together example:

define make_it_so::make_one_thing_so {
cron { $title:
command => "/path/$title",
user => 'deploy',
hour => '*/4',
minute => '0',
}
}

http://docs.puppetlabs.com/learning/definedtypes.html


Then add a data structure which you will presumably retrieve from hiera:

$scripts = {
'a.rb' => {},
'b.rb' => {},
}

http://docs.puppetlabs.com/hiera/1/


Then use create_resources to make as many of these as you need, without putting arrays or hashes in your puppet code ($scripts here will be automatically looked up via hiera):

class make_it_so (
$scripts = {}
) {
create_resources('make_it_so::make_one_thing_so', $scripts)
}

http://docs.puppetlabs.com/references/latest/function.html#createresources
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [1]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/puppet-users/6db6cb93-e9c4-4a42-91e7-36aa7583b5e1%40googlegroups.com.
> For more options, visit [3]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:puppet-users...@googlegroups.com
> 2. https://groups.google.com/d/msgid/puppet-users/6db6cb93-e9c4-4a42-91e7-36aa7583b5e1%40googlegroups.com?utm_medium=email&utm_source=footer
> 3. https://groups.google.com/d/optout

Ellison Marks

unread,
Apr 2, 2014, 7:21:55 PM4/2/14
to puppet...@googlegroups.com, christop...@pobox.com
Potentially easier, you could use prefix($scripts, '/path/'), which comes from puppetlabs-stdlib to put the path on all members of the array. Then just pass the now prefixed array to the defined type described above. This avoids storing the script names externally, which may or may not be desirable. If the scripts you want in cron might change now and then, then the external lookup is the better way.

jcbollinger

unread,
Apr 3, 2014, 9:05:10 AM4/3/14
to puppet...@googlegroups.com


On Wednesday, April 2, 2014 6:21:55 PM UTC-5, Ellison Marks wrote:
Potentially easier, you could use prefix($scripts, '/path/'), which comes from puppetlabs-stdlib to put the path on all members of the array. Then just pass the now prefixed array to the defined type described above. This avoids storing the script names externally, which may or may not be desirable. If the scripts you want in cron might change now and then, then the external lookup is the better way.


+1

You beat me to it.

Also, if you're willing to turn on the experimental "future" parser in recent Puppet then you can use its array-iteration mechanisms.  (Though iteration isn't quite the right characterization -- in reality it's more like SIMD.)

http://docs.puppetlabs.com/puppet/latest/reference/experiments_future.html
http://docs.puppetlabs.com/puppet/latest/reference/experiments_lambdas.html


John

Reply all
Reply to author
Forward
0 new messages