Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

So:

Dir.foreach("#{node[:chef][:cache_path]}/cookbooks/the_cookbook/templates/default/shared/")
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

 
discard

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.