Brian Ferris
2009-Jun-13 06:56 UTC
[Puppet Users] Iterating over the cross-product of two arrays
I have a list of databases and a list of users. I want to to grant privileges on each database to each user, for a total of # databases x # users. I''d rather not write out the grant snippet for each unique combination, but rather define the databases as an array of values along with the users as an array of values, and then iterate over their cross-product. However, I''m finding this tricky to do in Puppet. The main way to perform iteration in Puppet seems to be the Array as $name trick: file { [''a'',''b'',''c'']: ... } is expanded to file {''a'':..}, file {''b'': ...}, file {''c'': ...} I''m not sure how I can make this trick worth with a cross-product of two arrays. Something like: define outer_loop($inner_values) { inner_loop { $inner_values: outer_value => $name } } define inner_loop($outer_value) { # perform op on ${outer_value} + ${name} } Doesn''t work, because the call to inner_loop in each iteration of the array results in duplicate definition instantiations. I know I''m not supposed to think of definitions as functions, but is there some way of getting the desired before I''m looking for? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. 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 -~----------~----~----~----~------~----~------~--~---
David Schmitt
2009-Jun-14 09:51 UTC
[Puppet Users] Re: Iterating over the cross-product of two arrays
Brian Ferris schrieb:> I have a list of databases and a list of users. I want to to grant > privileges on each database to each user, for a total of # databases x > # users. I''d rather not write out the grant snippet for each unique > combination, but rather define the databases as an array of values > along with the users as an array of values, and then iterate over > their cross-product. However, I''m finding this tricky to do in > Puppet. > > The main way to perform iteration in Puppet seems to be the Array as > $name trick: > > file { [''a'',''b'',''c'']: ... } is expanded to file {''a'':..}, file > {''b'': ...}, file {''c'': ...} > > I''m not sure how I can make this trick worth with a cross-product of > two arrays. Something like: > > define outer_loop($inner_values) { > inner_loop { $inner_values: > outer_value => $name > } > } > > define inner_loop($outer_value) { > # perform op on ${outer_value} + ${name} > } > > Doesn''t work, because the call to inner_loop in each iteration of the > array results in duplicate definition instantiations. I know I''m not > supposed to think of definitions as functions, but is there some way > of getting the desired before I''m looking for?This is one of the applications of my prefix_with function: http://github.com/DavidS/puppet-common/blob/de7690c4e76ddd7c1f01d81fe92c75771da47c51/plugins/puppet/parser/functions/prefix_with.rb > > define outer_loop($inner_values) { > inner_loop { prefix_with("${name}/", $inner_values): > outer_value => $name > } > } > > define inner_loop($outer_value) { > # perform op on ${outer_value} + ${name} > } > On the downside, you will have to use something like regsubst(0.24.8+) or gsub(common module) to get the original value back. On the upside, this creates unique and more descriptive names. Regards, DavidS -- dasz.at OG Tel: +43 (0)664 2602670 Web: http://dasz.at Klosterneuburg UID: ATU64260999 FB-Nr.: FN 309285 g FB-Gericht: LG Korneuburg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. 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 -~----------~----~----~----~------~----~------~--~---