How to install multiple files with one file share

I need to install several files in a directory that is not itself controlled by Puppet. source each file is under the files/ subcategory of my module.

I would like to install them all at once, because their property rights and permissions are the same. But what do I set for source ? I was hoping by simply indicating that the directory would work:

  file {[ "${rcdir}/foo", "${rcdir}/bar", ]: source => "puppet:///${module_name}/", group => 'wheel', owner => 'root', mode => '0644' } 

but unfortunately Puppet (using 3.7.5 here) is not smart enough to automatically add foo and bar respectively.

Is there a good way to do this, or do I need to list each file carefully? Thanks!

+6
source share
1 answer

There are many methods to achieve what you are doing here, with advantages and disadvantages for everyone.

The first and most explicit one that gives you the ability to configure each file independently, as well as view the full list of files that you manage, is to define each file independently. To reduce code duplication, you can use standard defaults (although this is not always convenient). It looks something like this:

 File { group => 'wheel', owner => 'root', mode => '0644', } file { "${rcdir}/foo": source => "puppet:///modules/${module_name}/foo", } file { "${rcdir}/bar": source => "puppet:///modules/${module_name}/bar", } 

It obviously gets very bulky pretty quickly though.

The second strategy is to use a specific type. This is a bit of a heavy tool that can be used for something like that, but it will be a trick. It will look something like this:

 define myclass::file_array ( $dest_base, $source_base, $group = 'wheel', $owner = 'root', $mode = '0644', ) { file { "${dest_base}/${name}": source => "${source_base}/${name}", group => $group, owner => $owner, mode => $mode, } } class myclass (){ $files_to_manage = ['foo', 'bar', 'baz'] myclass::file_array { $files_to_manage: source_base => "puppet:///modules/${module_name}", dest_base => $rcdir, } } 

This requires that you add a relatively arbitrary specific type and ultimately require you to add many other parameters if you want to pass all the properties available for the type of the main file, but that would be enough for your situation.

However, the easiest and cleanest way to do what you are trying is to allow the file resource to use its recursive functionality and put all the necessary files in its own directory in your module (provided that you have other files that are not related to this target directory). This requires that you allow Puppet to manage the existence of the directory, but it is hard to imagine that this is a problem for you (since any of these codes failed if the destination directory did not exist anyway). It will look something like this:

 file { $rcdir: ensure => directory, recurse => true, source => "puppet:///modules/${module_name}/rc_files", owner => 'root', group => 'wheel', mode => '0644', } // module directory 'files/rc_files' is where foo and bar would exist 

I'm sure the latter is your ideal solution, and you can use other aspects of the file resource ( https://docs.puppet.com/puppet/latest/reference/type.html#file-attribute-recurse ), such as cleaning up checking that no additional files get to the destination.

There are other methods out there, but hopefully one of them will do the trick for you.

+7
source

All Articles