Martijn Grendelman
2012-Mar-28 12:33 UTC
[Puppet Users] Hash iteration order in a template not consistent
Hi, I did some basic googling, but didn''t find an answer yet. I am sorry if this is a FAQ. In a manifest for creating an Apache config file, I define a hash like this: $aliases = { ''/foo/'' => ''/home/foo/www/'', ''/bar/'' => ''/home/bar/www/'', ''/baz/'' => ''/home/baz/www//'' } Then, in a template, I have: <% aliases.each_pair do |key, val| -%> Alias <%= key %> <%= val %> <% end -%> The result is mostly what I expect, but every once in a while, the order in which the Aliases are generated from the ''each_pair'' loop changes, resulting in a different file. The Ruby docs state that "hashes enumerate their values in the order that the corresponding keys were inserted.", but is that not true for Puppet hashes? I did stumble across this post: http://serverfault.com/questions/368784/puppet-and-templates-how-to-loop-sequently-and-not-randomly which suggests to do something like <% aliases.sort_by {|key, value| key}.each_pair do |key, val| -%> <% end -%> Will it work? Is that a proper solution? Thanks! Martijn Grendelman -- 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.
R.I.Pienaar
2012-Mar-28 12:39 UTC
Re: [Puppet Users] Hash iteration order in a template not consistent
----- Original Message -----> From: "Martijn Grendelman" <martijn@iphion.nl><snip>> http://serverfault.com/questions/368784/puppet-and-templates-how-to-loop-sequently-and-not-randomly > > which suggests to do something like > > <% aliases.sort_by {|key, value| key}.each_pair do |key, val| -%> > <% end -%> > > Will it work? Is that a proper solution?ruby hashes are not stored in predictable order so this will happen, the proposed solution should work. But as always the best is just to test it and see how it goes, it wont bite :) -- 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.
Sergey Zhuga
2012-Mar-28 12:40 UTC
Re: [Puppet Users] Hash iteration order in a template not consistent
Hi, I did it something like that: <% aliases.sort.each do |alias| -%> Alias <%= alias.first %> <%= alias.last %> <% end -%> Regards.> Hi, > > I did some basic googling, but didn''t find an answer yet. I am sorry if > this is a FAQ. > > In a manifest for creating an Apache config file, I define a hash like this: > > $aliases = { > ''/foo/'' => ''/home/foo/www/'', > ''/bar/'' => ''/home/bar/www/'', > ''/baz/'' => ''/home/baz/www//'' > } > > Then, in a template, I have: > > <% aliases.each_pair do |key, val| -%> > Alias<%= key %> <%= val %> > <% end -%> > > The result is mostly what I expect, but every once in a while, the order > in which the Aliases are generated from the ''each_pair'' loop changes, > resulting in a different file. > > The Ruby docs state that "hashes enumerate their values in the order that > the corresponding keys were inserted.", but is that not true for Puppet > hashes? > > I did stumble across this post: > > http://serverfault.com/questions/368784/puppet-and-templates-how-to-loop-sequently-and-not-randomly > > which suggests to do something like > > <% aliases.sort_by {|key, value| key}.each_pair do |key, val| -%> > <% end -%> > > Will it work? Is that a proper solution? > > Thanks! > Martijn Grendelman >-- 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.
Martijn Grendelman
2012-Mar-29 08:06 UTC
Re: [Puppet Users] Hash iteration order in a template not consistent
On 28-03-12 14:39, R.I.Pienaar wrote:> > > ----- Original Message ----- >> From: "Martijn Grendelman" <martijn@iphion.nl> > > <snip> > >> http://serverfault.com/questions/368784/puppet-and-templates-how-to-loop-sequently-and-not-randomly >> >> which suggests to do something like >> >> <% aliases.sort_by {|key, value| key}.each_pair do |key, val| -%> >> <% end -%> >> >> Will it work? Is that a proper solution? > > ruby hashes are not stored in predictable order so this will happen, the proposed > solution should work. > > But as always the best is just to test it and see how it goes, it wont bite :) ><% aliases.sort_by {|key, value| key}.each do |key, val| -%> seems to do the trick. ''each_pair'' doesn''t work here, because the sort_by returns an array. Again, I learned something :-) Thanks, Martijn. -- 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.
Matthias Saou
2012-Apr-03 10:14 UTC
Re: [Puppet Users] Hash iteration order in a template not consistent
On Thu, 29 Mar 2012 10:06:01 +0200 Martijn Grendelman <martijn@iphion.nl> wrote: [...]> > > > ruby hashes are not stored in predictable order so this will > > happen, the proposed solution should work. > > > > But as always the best is just to test it and see how it goes, it > > wont bite :) > > <% aliases.sort_by {|key, value| key}.each do |key, val| -%> > > seems to do the trick. ''each_pair'' doesn''t work here, because the > sort_by returns an array. Again, I learned something :-)...and what about those of us which want the hash entries to appear in the exact same order they are present in the puppet manifest? From what I''ve seen, it was working that way up to 2.6 included, and only gets "randomized" with puppet 2.7. I''ve been doing things like this for a while now : mm_cfg_settings => { ''ALLOW_SITE_ADMIN_COOKIES'' => "Yes", ''PUBLIC_ARCHIVE_URL'' => "''https://%(hostname)s/pipermail/%(listname)s''", ''MTA'' => "''Postfix''", ''POSTFIX_STYLE_VIRTUAL_DOMAINS'' => "''False''", ''DEFAULT_SUBJECT_PREFIX'' => "''''", ''DEFAULT_REPLY_GOES_TO_LIST'' => "1", }, <% mm_cfg_settings.each do |key,value| -%> <%= key %> = <%= value %> <% end -%> In this particular example, order isn''t critical other than for readability, but I have some others where items must be in the same order as they appear in the manifest''s hash or things will break. Is there a way to keep using hashes if the order from the manifest must be kept in a file generated from the template? Matthias -- 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.