I'm trying to find a way to apply template resource dynamically on all files which are in a folder inside the cookbook's template folder. something like:
Dir.foreach("../templates/default/shared/") do | file | # This is the wrong line...
template "/home/admin/shared/#{file}" do
source "shared/#{file}"
…
end
end
how can I do it? I'm trying to avoid having a separate list of all the files in this directory as a variable... thanks.
Your code will be executed in the context of a node, so your Ruby code (
Dir.foreach
) will need to examine the node's local cache. In my Chef installation, the local cache path is required for my chef-client configuration template, so I have this attribute:node[:chef][:cache_path]
.So:
EDIT: In modern Chef configurations, templates won't exist on the server until they're needed. You'll need to add this to your client.rb: