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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to