Search code examples
filepuppetmodeowner

Does each file need to be copied within a separate resource


I'm still getting to grips with puppet (feels like I'm drinking from a hose pipe at times) so I've attempted to keep my configuration and environment simple initially. I've started by having puppet deploy files to my clients. However, I get the feeling that the way I'm deploying the files isn't the most efficient way of doing so. For every file, I'm specifying like this:

file { "/etc/ntp.conf":
                owner => 'root',
                group => 'root',
                mode  => '0444',
                source => 'puppet://basxtststinfl01/files/etc/ntp.conf',
        }
        file { "/etc/snmp/snmpd.conf":
                owner => 'root',
                group => 'root',
                mode  => '0644',
                source => 'puppet://basxtststinfl01/files/etc/snmpd.conf',
        }

I have up 15 files I'd like to deploy. Is this the correct approach?

Thanks.


Solution

  • File in modules is a good keyword.

    Generally, to solve the problem of repetitive resource, you can wrap them in a defined type.

    define deployed_file($ensure = 'present',
                         $owner = 'root',
                         $group = 'root',
                         $mode  = '644',
                         $recurse = '') {
        if $recurse != '' { File { recurse => $recurse } }
        file {
            $name:
                ensure => $ensure,
                owner  => $owner,
                group  => $group,
                source => "puppet://basxtststinfl01/files${name}",
        }
    }
    

    Your resources from above can then be written as:

    deployed_file {
        '/etc/ntp.conf':
            mode => '444';
        '/etc/snmp/snmpd.conf':
    }
    

    You can add more parameters to make the URL customizable.

    Note that I added the recurse parameter for posterity. file has lots of attributes, and if you need for the deployed_file to support them, you should add them in this manner, so that they get passed to the wrapped file if specified, but ignored otherwise.