Ask Your Question

Hiera hash into a template

asked 2015-11-25 09:26:57 -0600

gilbo gravatar image

updated 2015-11-25 10:01:37 -0600


I have the following:

 a_cluster :
 - a : bob
   p : 51
 - a : mike
   p : 52
 ...<any number of more a / p pairs>...

the question is i need to extrapolate these values into a template to produce seperated pairs. it needs to appear in the template like this:

A String of Stuff : bob:51,mike:52,..and so on for however many occurances there are of the a/p pairs in the array.

Would i need to do a bit of embedded ruby in the template?

I was doing something like this in the erb template:

<%- [@a_cluster].flatten.compact.each do |cluster| -%>
A string of stuff <%= cluster['a'] %>:<%= cluster['p'] -%>

but that just produces in the template:

A string of stuff bob:51

A string of stuff mike:52

but i want all pairs on the same line, sperated by the ':' character.... cannot seem to fathom it.
also as an aside in my travels i think i'm seeing some best practise notes which say "do this sort of stuff in the manifests, not in the templates" so should i be doing this in the manifests?

Thanks for reading Gilbo.

edit retag flag offensive close merge delete


I'm a little unclear on what you mean by 'all pairs on the same line, separated by the : character'... do you mean [ A string of stuff bob:51 mike:52 ] ?

DarylW gravatar imageDarylW ( 2016-04-01 09:37:08 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2016-04-01 10:10:51 -0600

DarylW gravatar image

Sometimes it is useful to play with your ideas directly in ruby before putting them into your template. The way I would do it is as follows.. Translate the yaml into a native ruby representation... I'm not a true rubyist but I'll show you what I know..

#Fire up IRB
bash$ irb

#Enter your data as a HEREDOC -

irb(main):001:0> data = <<-EOT
irb(main):002:0>a_cluster :
irb(main):003:0> - a : bob
irb(main):004:0>   p : 51
irb(main):005:0> - a : mike
irb(main):006:0>   p : 52

#irb with then show you what the value of data looks like
=> " a_cluster :\n- a : bob\n   p : 51\n - a : mike\n   p : 52\n"

#include the yaml library
irb(main):008:0> require 'yaml'
=> true

#parse the yaml into a ruby object
irb(main):009:0> h = YAML.load(data)
=> {"a_cluster" =>[{"a"=>"bob", "p"=>"51"}, {"a"=>"mike", "p"=>"52"}]}

#play with your data
#pull out just the array to iterate over (similar to what @a_cluster gives you above)
=> [{"a"=>"bob", "p"=>"51"}, {"a"=>"mike", "p"=>"52"}]

#print something similar to what you did above
irb(main):011:0>h["a_cluster"].each do | cluster |
irb(main):012:1>puts "A string of stuff #{cluster["a"]}:#{cluster["p"]}"
A string of stuff bob:51
A string of stuff mike:52
=> [{"a"=>"bob", "p"=>"51"}, {"a"=>"mike", "p"=>"52"}]

Since I'm not sure exactly how you want to represent your data, I can only show you how ruby sees what you are currently doing. If you apply flatten and compact to your array, it doesn't look any different then it initially does. flatten returns a 1d array from a series of nested arrays compact removes nil values from your array

One thing to keep in mind is that when generating files in puppet, it is useful to ensure that your data is sorted if order doesn't matter, that way a change in the input files won't produce a different output file. In your case, I would probably put a 'sort' instead of flatten and compact.

If you reply with how you would like your output to look, I can help you further shape it's output.

edit flag offensive delete link more

answered 2016-04-04 03:16:13 -0600

gilbo gravatar image

Thanks - that's really useful for future reference.

I did however sort my issue. I used this:{ |cluster| "#{cluster['a']}:#{cluster['b']}"}.join(',')

This produces:


in the resulting config.

Thanks for looking anyway - that tip you've mentioned above is really helpful.

edit flag offensive delete link more


Yea, I didn't see it mentioned in your original comment that you wanted them comma separated on the same line, or else I would have suggested something like you mentioned. Glad you found a solution!

DarylW gravatar imageDarylW ( 2016-04-04 14:33:42 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-11-25 08:44:07 -0600

Seen: 938 times

Last updated: Apr 04 '16