for i in array do something - iteration irritation

87 views
Skip to first unread message

earthgecko

unread,
Nov 7, 2012, 4:15:15 PM11/7/12
to puppet...@googlegroups.com
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? 

earthgecko

unread,
Nov 7, 2012, 4:23:35 PM11/7/12
to puppet...@googlegroups.com
Apologies

app::thing { 'foo':
  config_templates = ['foo.conf', 'foo.more.conf']
}

jcbollinger

unread,
Nov 7, 2012, 5:46:05 PM11/7/12
to puppet...@googlegroups.com


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

earthgecko

unread,
Nov 7, 2012, 5:56:32 PM11/7/12
to puppet...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages