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

后端 未结 5 2344
情歌与酒
情歌与酒 2021-02-18 15:45

the resource_directory has only 2 actions available: create and delete

I need to update the owner and group of a directory recursi

5条回答
  •  故里飘歌
    2021-02-18 16:31

    https://stackoverflow.com/a/28283020/11822923

    Answer met my need but there is an issue with grep command, even if the user is apache2 or apach the grep exit code will be 0, but I needed the user to exactly be apache (Apache web server user on CentOS 7.7).

    Here is my recipe:

    node["apache"]["sites"].each do |sitename, data|
      document_root = "/content/sites/#{sitename}"
    
      directory document_root do
        action :create
        mode "0755"
        recursive true
        owner "apache"
        group "apache"
      end
    
      execute "chown_to_apache_user" do
        command "chown -R apache:apache /content"
        user "root"
        action :run
        not_if '[ $(stat -c %U /content/) = "apache" ]'
      end
    
      template "/etc/httpd/conf.d/#{sitename}.conf" do
        source "vhost.erb"
        mode "0644"
        variables(
          :document_root => document_root,
          :port => data["port"],
          :domain => data["domain"]
        )
        notifies :restart, "service[httpd]"
      end
    
    end
    

    For comparison:

    P.S.: to check group as well:

    node["apache"]["sites"].each do |sitename, data|
      document_root = "/content/sites/#{sitename}"
    
      directory document_root do
        action :create
        mode "0755"
        recursive true
        owner "apache"
        group "apache"
      end
    
      execute "chown_to_apache_user" do
        command "chown -R apache:apache /content"
        user "root"
        action :run
        not_if '[ $(stat -c %U /content/) = "apache" ] && [ $(stat -c %G /content/) = "apache" ]'
      end
    
      template "/etc/httpd/conf.d/#{sitename}.conf" do
        source "vhost.erb"
        mode "0644"
        variables(
          :document_root => document_root,
          :port => data["port"],
          :domain => data["domain"]
        )
        notifies :restart, "service[httpd]"
      end
    
    end
    

提交回复
热议问题