If I have something like this in my node file: daemontools_service::setup { ''carbon-cache'': service_name => ''carbon-cache'', .... extra_envs => { "GRAPHITE_STORAGE_DIR" => "/mnt/statsd-data/graphite-storage" "SOMETHING_ELSE" => "12345" "FOO" => "bar" }; ''another-service'': ...etc. } and for those "extra_envs" values I want to create files that look like this: carbon-cache/ env/ GRAPHITE_STORAGE_DIR SOMETHING_ELSE FOO with the *contents* of those files the *values* of the keys above, then I was hoping I could do something like this: -------------------------------------------------- class daemontools_service { define envdir_file ($name, $value){ file { "/var/lib/supervise/${service_name}/env/${key}" : mode => 644, content => "$value", } } define setup( $service_name, $extra_envs={}, ){ create_resources(envdir_file, $extra_envs) .... -------------------------------------------------- But I get the error at the create_resources() line. can''t convert String into Hash Is this the right approach? Or is there a more idiomatic way? Is it even possible to iterate through a hash like that in puppet? I see some indications on the web that this approach might work. Any suggestions would be appreciated. Thanks! -- 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 puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-May-10 14:55 UTC
[Puppet Users] Re: equivalent way to iterate through a hash
On Thursday, May 9, 2013 6:00:11 PM UTC-5, Kevin G. wrote:> > If I have something like this in my node file: > > daemontools_service::setup { > ''carbon-cache'': > service_name => ''carbon-cache'', > .... > extra_envs => { > "GRAPHITE_STORAGE_DIR" => "/mnt/statsd-data/graphite-storage" > "SOMETHING_ELSE" => "12345" > "FOO" => "bar" > }; > ''another-service'': > ...etc. > } > > and for those "extra_envs" values I want to create files that look like > this: > > carbon-cache/ > env/ > GRAPHITE_STORAGE_DIR > SOMETHING_ELSE > FOO > > with the *contents* of those files the *values* of the keys above, then I > was hoping I could do something like this: > > > -------------------------------------------------- > class daemontools_service { > > define envdir_file ($name, $value){ > file { "/var/lib/supervise/${service_name}/env/${key}" : > mode => 644, > content => "$value", > } > } > > define setup( > $service_name, > $extra_envs={}, > ){ > create_resources(envdir_file, $extra_envs) > .... > -------------------------------------------------- > > > But I get the error at the create_resources() line. > > can''t convert String into Hash > > Is this the right approach? Or is there a more idiomatic way? Is it even > possible to iterate through a hash like that in puppet? I see some > indications on the web that this approach might work. > > Any suggestions would be appreciated. Thanks! > >The create_resources() function expects its second argument to be a hash of hashes, with the keys of the outer hash being resource titles and the values being hashes of resource parameters for the corresponding resource. You could probably build something around create_resources() that achieves what you want, but it would likely require alterations to your node-level declarations. As an alternative, you could install PuppetLab''s useful ''stdlib'' module, whose ''keys'' function can help you out. For instance, you could switch to something like this: define supervise::service_setup($extra_envs={}) { $env_names = keys($extra_envs) # one supervise::envdir instance for each element of $env_names: supervise::envdir_file { $env_names: service_name => $name, all_envs => $extra_envs } } define supervise::envdir_file($service_name, $all_envs) { file { "/var/lib/supervise/${service_name}/env/${name}" : mode => 644, content => $all_envs[$name] } } Note that everywhere inside a defined type body, the special variable $name refers to the name / title of the defined-type instance (and thus it will be different for each instance of that type). Do not be confused when it is used inside the declarations of other resources -- it still refers to the name of the containing defined-type instance being *defined*, not the name / title of any resource *declared* as part of that type definition. Note also that I have put my definitions into a module, ''supervise''. That''s not essential, but I strongly recommend it. John -- 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 puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.