Puppet, changing multiple properties file with augeas

augeaspuppet

I'm using puppet and augeas tool quite a lot to configure property files. My latest requirement is to apply the same fixed set of changes to quite a long list of property files. So, I would like to do it in one run, and not to write an augeas for each property file.

Example:

  augeas { 'change_name_whatever':
    lens    => 'a_customized_lens',
    incl    => "$path/file1.properties", 
    changes => $change_set,
  }
  augeas { 'change_name_whatever':
    lens    => 'a_customized_lens',
    incl    => "$path/file2.properties", 
    changes => $change_set,
  }
etc...

I would like to use:

  augeas { 'change_name_whatever':
    lens    => 'a_customized_lens',
    incl    => "[list of files to change], 
    changes => $change_set,
  }

but it's not possible since augeas needs to pre-load the file.

Since I am using puppet 3.8, I can't use foreach type of looping. I saw that in puppet 4 you can declare a list of files, and then loop on them and do your thing. Which is cool… but doesn't work in puppet 3.

So, do I have any other solution then copy/pasting the same code many times?

cheers.

Best Answer

The best option to achieve that is to create a defined resource type:

define customized::filetype ($change_set) {
 augeas { "customized filetype ${title}":
    lens    => 'a_customized_lens',
    incl    => $name, 
    changes => $change_set,
  }
}

and then use it with an array in the title:

customized::filetype { [
  "$path/file1.properties",
  "$path/file2.properties"
  ]:
  change_set => $change_set,
}

You could even (and I'd recommend it) abstract the change set as key/value parameters in the defined resource type. For example:

define customized::filetype ($key, $value) {
 augeas { "customized filetype ${title}":
    lens    => 'a_customized_lens',
    incl    => $name, 
    changes => "set ${key} ${value}",
  }
}