Ask Your Question

iterating over a deep nested hash

asked 2017-01-11 21:46:32 -0600

CBR gravatar image

updated 2017-01-12 12:30:22 -0600

I have the following hiera data. I'm currently using 3 nested each functions to get the values of the data. Is there a simplest way to iterate over all values ?

        key3: value3
        key4: value4
        key1: value1
        key2: value2

Update: I have incorrectly stated that I used combination of each and dig. I used nested each function.

edit retag flag offensive close merge delete



There is not enough information to help you. Can you specify what you want the result of the operation to be?

Henrik Lindberg gravatar imageHenrik Lindberg ( 2017-01-12 05:46:10 -0600 )edit

@Henrik, I need each key in a variable so that they can be used to create a resource type. For example, creating a directory /opt/dxgav01/acdx01/a.ini (where a.ini will have key3 = value3 ; key4 = value 4).

CBR gravatar imageCBR ( 2017-01-12 06:08:27 -0600 )edit

If you can't do the appropriate transformation of the structure in puppet, you can always make a custom ruby function to transform the input structure into the appropriately shaped hash/data structure to iterate over.

DarylW gravatar imageDarylW ( 2017-01-12 12:10:58 -0600 )edit

If on 4.x you can also write a function in the Puppet Language (much easier). Will consider what you posted and come back with a reply.

Henrik Lindberg gravatar imageHenrik Lindberg ( 2017-01-12 12:47:53 -0600 )edit

I still haven't made the switch from puppet3 yet, so I'm not used to the power that is now available in the puppet DSL, thanks for the great example @Henrik!

DarylW gravatar imageDarylW ( 2017-01-13 12:53:27 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-01-13 09:46:43 -0600

Henrik Lindberg gravatar image

Here is an example function in the puppet language that should work on a recent 4.x version of Puppet (or that is easily translated into Ruby. it transforms a nested hash to an Array that it then notices. You can use the result and continue the transformation to the wanted string form.

function example(Hash $h, Array $keys = []) {
  $h.reduce([]) |$memo, $e| {
    [$k, $v] = $e 
    if $v =~ Hash { $memo + example($v, $keys + $k) }
    else { $memo << [($keys + $k), $v] }
$data = {
  'apache::hosts' => {
   'dxgav01' => {
     'acdx' => {
       'abcd01' => {
         'key1' => 'value 3',
         'key2' => 'value 4',
       'ncdv01' => {
         'key1' => 'value 1',
         'key2' => 'value 2',
# compute 
$result  = example($data)
# here just noticing the result, transform into wanted strings
# using join of keys etc.
notice $result
edit flag offensive delete link more



much simpler than what I have, I knew it would have to be a reduce function. Just have to practise using it. Thank you.

CBR gravatar imageCBR ( 2017-01-13 12:07:02 -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: 2017-01-11 21:46:32 -0600

Seen: 360 times

Last updated: Jan 13 '17