Puppet: merging lists of hashes

puppet

If I have a puppet class that receives a hash variable, and I want to provide defaults for some of the keys in the hash variable, I can simply use a hash merge ($hash1 + $hash2) to generate a new class with the desired defaults. I.e, given:

defaults:
  field1: default1
  field2: default2
  field3: defaul3

myconfig:
  field1: val1
  field3: val3

Then $defaults + $myconfig gives me:

finalconfig:
  field1: val1
  field2: default2
  field3: val3

But what do I do if I want to accomplish the same thing with a list of hashes? That is, if my input is:

myconfig:
  - field1: custom1
    field2: custom2
  - field1: custom1
    field3: custom3

And I have defaults that look like:

defaults:
  field1: default1
  field2: default2
  field3: default3

I want to end up with:

finalconfig:
  - field1: custom1
    field2: custom2
    field3: default3
  - field1: custom1
    field2: default2
    field3: custom3

Is there a way to do this within a puppet manifest?

Best Answer

This turned out to be easier than I thought. If I have:

$defaults = {
  field1 => default1,
  field2 => default2,
  field3 => default3
}

I can apply those defaults to a list of hashes like this

$finalconfig = $config.map |cfg| {
  $defaults + $cfg
}