Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}"

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.

share|improve this question… – Zaur Sep 15 '15 at 22:34
up vote 1 down vote accepted

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].



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:

no_lazy_load true
share|improve this answer
thanks for your answer. I thought about it, but didn't know how much can I rely on this cache or how do I get to it. So, what should I do to have this attribute too? it's not there by default if I got you right. – SecondThought Dec 25 '12 at 11:47
Check your cookbooks and attributes. The cache was there for me by default in the "chef" cookbook, because it was required to configure the chef configuration template. – EdwardTeach Dec 25 '12 at 20:32
In the current version of Chef, it looks like the templates aren't locally downloaded until you explicitly require them... so the template dir on the server will be empty when you start. Chicken/Egg – Evan Mar 27 '13 at 20:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.