Ask Your Question
0

Copying multiple files within separate resources.

asked 2014-10-24 04:23:20 -0500

djc72uk gravatar image

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 all with the same ownership but is some instances different permissions. Is this the correct approach?

Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-10-25 23:16:25 -0500

cbarbour gravatar image

updated 2014-10-26 19:38:48 -0500

There are a couple of answers to your question.

You can reduce repetition in your module using one of these approaches:

  1. Set a resource default so that every file in the module is owned by root:root and set mode 0444 unless specified otherwise.
  2. Write a defined type that automates the creation of the files.
  3. Use create_resources() to iterate through a data structure containing resources, with default parameters for unset values.

A resource default is the easiest of these approaches to use and understand. Here's your example, using a default:

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

Note that file is capitalized in the default case.

There are some benefits and drawbacks of this approach...

  • Good: Defaults tend to highlight exceptions.
  • Bad: Defaults cause resources to take on non-default behaviors.
  • Bad: Defaults are inherited through scoped class.
  • Bad: Defaults can affect stuff you don't expect them to affect (see scope.)

The official style guide has more information.

https://docs.puppetlabs.com/guides/style_guide.html#resource-defaults

For my part, I've seen at least two cases where careless defaults caused major site-wide outages. In both cases, the defaults were scoped site wide. Use with caution.

Bigger picture answer:

Your approach of dropping a bunch of files onto the managed system is simple and easy to understand, which is a positive. This approach is fine for a small, simple site. For a larger site, I'd advise you to split this module into a bunch of separate modules, (possibly Forge modules) and use Hiera to separate your code from your site specific data.

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: 2014-10-24 04:23:20 -0500

Seen: 1,634 times

Last updated: Oct 26 '14