Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active April 26, 2018 18:32
Show Gist options
  • Save natemccurdy/293a383758281544a39c958863c4e46a to your computer and use it in GitHub Desktop.
Save natemccurdy/293a383758281544a39c958863c4e46a to your computer and use it in GitHub Desktop.
A better alternative to "recuse => true" to set permissions
define recurse_file_permissions (
String[1] $target_dir = $title,
Optional[String[1]] $file_mode = undef,
Optional[String[1]] $dir_mode = undef,
Optional[String[1]] $owner = undef,
Optional[String[1]] $group = undef,
) {
if $facts['os']['family'] == 'windows' {
fail("${module_name} does not support Windows")
}
unless $file_mode or $dir_mode or $owner or $group {
fail('At least one of file_mode, dir_mode, owner, or group is required')
}
if $dir_mode {
exec { "Set perms of ${target_dir} directories to ${dir_mode}":
command => "find ${target_dir}/ -type d ! -perm ${dir_mode} -exec chmod -c ${dir_mode} {} \\;",
onlyif => "find ${target_dir}/ -type d ! -perm ${dir_mode} | grep '.*'",
path => $facts['path'],
logoutput => true,
loglevel => 'info',
}
}
if $file_mode {
exec { "Set perms of ${target_dir} contents to ${file_mode}":
command => "find ${target_dir}/ -type f ! -perm ${file_mode} -exec chmod -c ${file_mode} {} \\;",
onlyif => "find ${target_dir}/ -type f ! -perm ${file_mode} | grep '.*'",
path => $facts['path'],
logoutput => true,
loglevel => 'info',
}
}
if $owner and $group {
exec { "Set owner and group of ${target_dir} contents to ${owner}:${group}":
command => "find ${target_dir}/ \\( ! -user ${owner} -or ! -group ${group} \\) -exec chown ${owner}:${group} -c {} \\;",
onlyif => "find ${target_dir}/ \\( ! -user ${owner} -or ! -group ${group} \\) | grep '.*'",
path => $facts['path'],
logoutput => true,
loglevel => 'info',
}
} elsif $owner {
exec { "Set owner of ${target_dir} contents to ${owner}":
command => "find ${target_dir}/ \\( ! -user ${owner} \\) -exec chown ${owner} -c {} \\;",
onlyif => "find ${target_dir}/ \\( ! -user ${owner} \\) | grep '.*'",
path => $facts['path'],
logoutput => true,
loglevel => 'info',
}
} elsif $group {
exec { "Set group of ${target_dir} contents to ${group}":
command => "find ${target_dir}/ \\( ! -group ${group} \\) -exec chgrp ${group} -c {} \\;",
onlyif => "find ${target_dir}/ \\( ! -group ${group} \\) | grep '.*'",
path => $facts['path'],
logoutput => true,
loglevel => 'info',
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment