On Friday, January 4, 2013 9:11:28 AM UTC-6, Andy Taylor wrote: > > Hi, > > I'm trying to build a module for haproxy which fetches all the > configuration data from Hiera to populate the haproxy config file. I've run > into a number of issues though when I try to use hashes. Ideally, I want to > use something like this: > > haproxy_listeners : > "cluster1" : > ip : '192.168.0.2' > port : '80' > servers : > "server1" : > ip : '192.168.0.3' > port : '8080' > > So a hash of clusters with each cluster containing a nested hash of > servers. Is this possible with Hiera/ERB? It's easy enough to iterate over > the first hash, but I can't work out how to extract the contents of the > nested hash. Or I might just be approaching this in entirely the wrong > way... Any help would be much appreciated. > > The data can be expressed in YAML, loaded into a Puppet variable via Hiera, and processed in an ERB template to construct your file. In other words, yes, it is possible.
May I ask, however, what the advantage is to encoding the data in YAML and then transcoding it into the haproxy configuration language? I mean, if you also use bits and pieces of that object elsewhere in your manifests then perhaps it makes sense, but if all you want to do with it is generate the config file then I don't see the point. Why not just serve up the config as a static or mostly-static file? Leaving aside the question of merits of your design, - Note that your example data are a quadruply-nested hash, not just a doubly-nested one as you say. You have a hash of clusters, each of which is a hash itself and contains a third hash, each of whose values is * another* hash. - The same Ruby mechanisms that allow your template to iterate and otherwise inspect the outer hash also work for the inner ones. - Perhaps, though, you are tripping over the fact that the values in your outer hash are of uniform type and meaning, whereas the values in the next-level hash are of varying type and meaning. You probably don't actually want to iterate the second-level hash; instead, you want to retrieve specific values from it by key. So, you might do something of this general form (not intended to resemble an actual haproxy configuration file): haproxy.conf.erb: ============ <% @clusters.each_pair | cluster, properties | { -%> <cluster name="<%= cluster %>" ip="<%= properties['ip'] %>" port="<%= properties['port'] %>" > <% properties['servers'].each_pair | server, server_props | { -%> <server name="<%= server %>" ip="<%= server_props['ip'] %>" port="<%= server_props['port'] %>" /> <% } -%> </cluster> <% } -%> Once corrected for the inevitable typos, that should produce output similar to <cluster name="cluster1" ip="192.168.0.2" port="80" > <server name="server1" ip="192.168.0.3" port="8080" /> </cluster> 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/-/uZSpX_tBuM8J. 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.