Ask Your Question
0

Processing over hash with array - puppet 3.8

asked 2017-01-26 05:42:28 -0500

SimonH gravatar image

updated 2017-01-26 05:54:17 -0500

We're using puppet 3.8 (unfortunately can't move to puppet 4 yet)

I've got a hash in hiera that looks something like this:

hash_data:
  item1:
    field1:   'some data'
    array_data:
      - data1
      - data2
  item2:
    field1:   'other data'
    array_data:
      - data3
      - data4

I've put together a module with code something like:

class processor {
    $data = hiera_hash('hash_data', {})

    create_resources(processor::hash_entry, $data)
}

define processor::hash_entry ($field1, $array_data) {
#    .. do_something ..

# process array items
    processor::process_array { ${array_data} : 
        datavar = 'somevalue'
    }
}

define processor::process_array($element, $datavar) {
    # do something
}

this works fine as long at the array_data fields in the hash all contain unique fields. However, if I need to put non-unique data something like:

hash_data:
  item1:
    field1:   'some data'
    array_data:
      - data1
      - data2
  item2:
    field1:   'other data'
    array_data:
      - data3
      - data2                  ( **non-unique value **)

then we hit a duplicate resource. Can anyone suggest how I could process that hash if I wanted to be able to handle the items in array_data as unique items?

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-01-26 08:32:24 -0500

DarylW gravatar image

updated 2017-01-26 08:46:52 -0500

Other than future_parser, best thing I could recommend is making a custom ruby function that turns your hash structure into something eaiser to process( array of comma separated strings) that you can pass into a define, and then split and use positions or a regex to pull out the value you need.

You could always do something like prefix (from [stdlib])(https://forge.puppet.com/puppetlabs/stdlib) the values with the outer 'unique' name.

class processor {
    $data = hiera_hash('hash_data', {})

    create_resources(processor::hash_entry, $data)
}

define processor::hash_entry ($field1, $array_data) {
#    .. do_something ..

# make array items unique with outer name
 $uniq_array_data = prefix($array_data, "${name}-")

# process array items
    processor::process_array { ${uniq_array_data} : 
        datavar = 'somevalue'
    }
}

define processor::process_array($element, $datavar) {
    $input_array = split($name, '-') # or use a regex match for greater control
    $_element = $input_array[1] #to pick the non-name part
    # do something
}

I really would recommend writing putting some big ugly # HERE THERE BE DRAGONS comments around this, document it, and possibly provide the code you would us in puppet4 (or with future parser enabled) in a comment to show what you are ACTUALLY trying to do, as opposed to the hoops you need to hop through without proper looping/iteratoin

edit flag offensive delete link more

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

Stats

Asked: 2017-01-26 05:42:28 -0500

Seen: 42 times

Last updated: Jan 26