2

I need to create a user account and a lot of files for that user with Puppet.

Works fine with the most straightforward approach, but on every file resource I have to specify owner => 'theuser', which seems horribly redundant (if I don't they end up root-owned).

Is there a way to have a scoped default for the owner? Or something like sudo where puppet runs under that user for a certain set of resources?

2
  • are all the files in the same location?
    – Mike
    Mar 7, 2013 at 6:18
  • @Mike: a common root folder could be arranged.
    – Thilo
    Mar 7, 2013 at 6:19

2 Answers 2

4

So if everything is in a single location you can use the recurse option for file

file { "/path/to/files/":
    ensure  => directory,
    recurse => true,
    owner   => "user",
    group   => "group",
    mode    => "700",
    source  => "puppet:///modules/moduleName/path/in/files/",
  }

Then just have all your files in like

modules/ModuleName/path/in/files/

Puppet will put all the files in that module path into /path/to/files on the system owned by that user/group.

4
  • Will it do anything to files that are already in that directory (but not in the module)?
    – Thilo
    Mar 7, 2013 at 6:23
  • 1
    if any filename matches it will overwrite them.. you can add a purge => true, to it and it will delete any file on the system that isn't in puppet for that directory
    – Mike
    Mar 7, 2013 at 6:24
  • Sweet. That cuts down not just on the owner attribute, but on the multiple file resources altogether. Can this also be made to work with files that are not static, but use content => Template( somehow (some files in that module are templates)?
    – Thilo
    Mar 7, 2013 at 6:31
  • if it can work with templates.. i don't know of a way
    – Mike
    Mar 7, 2013 at 6:39
1

To cut down on e.g. the owner attribute you can add some defaults.

File {
  owner => 'user',
  group => 'somegroup',
  mode  => '0444'
}

file {
  '/some/where/file1': source => 'some-source1';
  '/some/where/file2': source => 'some-source2';
  '/some/where/file3': source => 'some-source3';
}

Documentation on resource defaults: https://puppet.com/docs/puppet/latest/lang_defaults.html

2
  • Do you have a link to the documentation for this default definition feature?
    – Thilo
    Mar 7, 2013 at 9:08
  • 1
    @Thilo I added a link to the official documentation about it in the answer.
    – pkhamre
    Mar 7, 2013 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .