earthgecko
2012-Nov-07 21:15 UTC
[Puppet Users] for i in array do something - iteration irritation
Hi I am sure that there is some way to iterate an array and do something for each in. It must be a wheel that has been invented (even if in a ruby function)? I have needed to do something like this a few times now and I reckon someone clever has already done it :) Using a normal bash type for loop as an example: app::thing { ''foo'': $config_templates = [''foo.conf'', ''foo.more.conf''] } define app::thing( $config_templates = '''', ) { if $config_templates != '''' { for i_config in $config_templates do file { "/opt/app/thing/${name}/conf/${i_config}": content => template("app/${name}/conf_templates/${i_config}.erb"), } done } } Is there something in stdlib or forge to iterate an array and do something? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/4UT6Pg50gEcJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
earthgecko
2012-Nov-07 21:23 UTC
[Puppet Users] Re: for i in array do something - iteration irritation
Apologies app::thing { ''foo'': config_templates = [''foo.conf'', ''foo.more.conf''] } -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/tsXtAlEp-8sJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
jcbollinger
2012-Nov-07 22:46 UTC
[Puppet Users] Re: for i in array do something - iteration irritation
On Wednesday, November 7, 2012 3:15:15 PM UTC-6, earthgecko wrote:> > Hi > > I am sure that there is some way to iterate an array and do something for > each in. It must be a wheel that has been invented (even if in a ruby > function)? > > I have needed to do something like this a few times now and I reckon > someone clever has already done it :) > > Using a normal bash type for loop as an example: > > app::thing { ''foo'': > $config_templates = [''foo.conf'', ''foo.more.conf''] > } > > define app::thing( > $config_templates = '''', > ) { > if $config_templates != '''' { > for i_config in $config_templates > do > file { "/opt/app/thing/${name}/conf/${i_config}": > content => template("app/${name}/conf_templates/${i_config}.erb"), > } > done > } > } > > Is there something in stdlib or forge to iterate an array and do something? >Puppet DSL cannot express iteration *per se*, but it can compactly express a set of resources that differ only in title. In conjunction with defined types, that is usually sufficient for this kind of problem: define app::thing::conffile ($thingname) { file { "/opt/app/thing/${thingname}/conf/${name}": content => template("app/${thingname}/conf_templates/${name}.erb"), } } define app::thing ($config_templates = ''NONE'') { if $config_templates != ''NONE'' { # creates multiple conf files if $config_templates # is an array: app::thing::conffile( $config_templates: thingname => $name } } } If you need more flexibility than that then you may find the built-in create_resources() function useful to you. For the ultimate in flexibility, you can always write your manifest in Puppet''s Ruby DSL, which more or less makes it a dynamic manifest-generating script instead of an actual manifest. In any case, it lets you use Ruby directly. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/DAgGsFIZLqgJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
earthgecko
2012-Nov-07 22:56 UTC
[Puppet Users] Re: for i in array do something - iteration irritation
Hi John Thanks, that will solve my immediate requirement. Trying to keep it as puppet DSL and simple as possible. However, I will check out the create_resources() function further. Thanks for the pointers. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/odb1kUCzNUUJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.