How to recursively change the owner and group on a directory with Chef?

zabumba picture zabumba · May 28, 2014 · Viewed 25.1k times · Source

the resource_directory has only 2 actions available: create and delete

I need to update the owner and group of a directory recursively.

How do I do that?

using a simple resource_execute?

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :run
end

Answer

Bill Warner picture Bill Warner · Feb 2, 2015

You can set the default action to nothing then have resources that may screw things up notify the perm fixer:

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :nothing
end

resource "that may screw up perms" do
  stuff "one two three"
  notifies :run, execute "chown-data-www"
end

with more options you could have the action :run but not if the parent folder is already the correct perms. You could alter this to include a deeper/problem file/directory, or with a find command similar to this

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :run
  not_if '[ $(stat -c %U /var/www/myfolder) = "www-data" ]'
end

Edit: fix to reflect comment below