skip to Main Content

I would like to create/ensure a directory exists and recursively copy many files to it using native Puppet methods if possible. The file modes are same for all files but differ from the directory.

I am using Puppet6 version 6.10.0 on CentOS 6.10

This code will create/ensure my directory exists and copy all the files to it but sets the access rights and ownership exactly the same.

file { "/opt/dir1":
  ensure  => "directory",
  owner   => "user1",
  group   => "root",
  mode    => "0700",
  recurse => true,
  source  => "puppet:///modules/mymodule/dir1",
}

What I get:

ls -la /opt/dir1"
drwx------ 2 user1 root 4096 Sep 23 20:31 .
drwxr-xr-x 7 user1 root 4096 Oct  6 15:20 ..
-rwx------ 1 user1 root   72 Oct  5 17:15 file1

What I want:

ls -la /opt/dir1"
drwx------ 2 user1 root 4096 Sep 23 20:31 .
drwxr-xr-x 7 user1 root 4096 Oct  6 15:20 ..
-rw-r--r-- 1 user1 root   72 Oct  5 17:15 file1

2

Answers


  1. The Puppet file resource can’t set different modes for the apex directory and its files when using recurse. https://puppet.com/docs/puppet/5.5/types/file.html#file-attribute-mode

    Would you be able to use an archive resource instead? With an archive resource, you can specify a tar file as the source, and the permissions will be set following those in the tar file.

    Login or Signup to reply.
  2. If you are able to manage the permissions and mode in the source you can use this parameter source_permissions => use. Note, depending on your version you may get a deprecated warning:

    Warning: The `source_permissions` parameter is deprecated. Explicitly set `owner`, `group`, and `mode`.
    
    file { "/opt/dir1":
      ensure            => "directory",
      owner             => "user1",
      group             => "root",
      source_permissions => "use",
      recurse           => true,
      source            => "puppet:///modules/mymodule/dir1",
    }
    

    This would allow you do manage the mode in the source but still override the owner and group. you could also drop the owner and group params above and manage them in the source as well. However I’m not sure how this works if you have windows clients and a linux puppet master, or a missmatch in users/groups on the master vs agent

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search