Bringing the owner / group recursively to the chef

I have a cube of code in Chef that recursively creates some directories

 # Deploy config files from files
  unless instance[:directories].nil?
    instance[:directories].each do |dir|
      unless File.directory?("#{dir[:source_dir]}")
        remote_directory "#{dir[:path]}" do
          source "#{dir[:source_dir]}"
          owner "#{config[:owner]}"
          group "#{config[:group]}"
          recursive true
          notifies :run, "execute[change permissions]", :immediately
          notifies :restart, "service[#{instance[:name]}]"
        end
      end
    end
  end

According to the specification, the remote_directory resource applies the owner / group rights only to the node sheet in the specified path, and not to the created intermediate nodes.

I plan to notify the execute command when a path is ever created and permissions are applied recursively. How to pass an argument (in this case # {dir [: path]}) to execute the command, as shown below.

  execute "change permissions" do
    command "chown -R #{config[:owner]}:#{config[:group]} #{path}"
    user "root"
    action :nothing
  end
+4
source share
1 answer

No. Enable the runtime resource with the remote directory resource:

unless instance[:directories].nil?
    instance[:directories].each do |dir|
      unless File.directory?("#{dir[:source_dir]}")
        path = dir[:path]

        remote_directory path do
          source dir[:source_dir]
          owner config[:owner]
          group config[:group]
          recursive true
          notifies :run, "execute[change-permission-#{path}]", :immediately
          notifies :restart, "service[#{instance[:name]}]"
        end

        execute "change-permission-#{path}" do
          command "chown -R #{config[:owner]}:#{config[:group]} #{path}"
          user "root"
          action :nothing
        end
      end
    end
  end
+5
source

All Articles