4

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.

SecondThought
  • 409
  • 1
  • 4
  • 11
  • http://serverfault.com/questions/522671/how-to-use-current-cookbook-template-dir-to-copy-all-templates-recursively-in-a/722533#722533 – Zaur Sep 15 '15 at 22:34

1 Answers1

3

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/")

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
Jacklynn
  • 155
  • 1
  • 7
EdwardTeach
  • 622
  • 8
  • 20
  • 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