Why will chef not read a file in a template?


#1

What is the deal with chef and trying to read a file? I mean…the file is
there. The below is in a template.

discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>

ls -al /var/chef/cache/
-rw-r–r-- 1 root root 19 Feb 22 06:05 unicast_hosts

And yes #{Chef::Config[:file_cache_path]} = /var/chef/cache/

[2014-02-22T06:15:12-05:00] INFO: Sending resource update report (run-id:
e5f200ea-5d0b-4652-b02a-3b836357ca5b)
[2014-02-22T06:15:12-05:00] ERROR:

Chef::Mixin::Template::TemplateError (No such file or directory -
#{Chef::Config[:file_cache_path]}/unicast_hosts) on line #327:

325:
326:
327: discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>
328:
329:


#2

Typically the logic should be in the recipe, not the template. I’m not sure
the templating system has access to resolve Chef::Config from what the
error is showing.

Alternatively, you should be able to at least form the full filename and
pass it to the template as a variable. Doing the logic like this inside the
template seems doubly wrong.

Ah, sorry, make that triply wrong… if you want variables inside a string
it needs to be double quoted, not single quoted.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Sat, Feb 22, 2014 at 3:19 AM, David Montgomery <davidmontgomery@gmail.com

wrote:

What is the deal with chef and trying to read a file? I mean…the file is
there. The below is in a template.

discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>

ls -al /var/chef/cache/
-rw-r–r-- 1 root root 19 Feb 22 06:05 unicast_hosts

And yes #{Chef::Config[:file_cache_path]} = /var/chef/cache/

[2014-02-22T06:15:12-05:00] INFO: Sending resource update report (run-id:
e5f200ea-5d0b-4652-b02a-3b836357ca5b)
[2014-02-22T06:15:12-05:00] ERROR:

Chef::Mixin::Template::TemplateError (No such file or directory -
#{Chef::Config[:file_cache_path]}/unicast_hosts) on line #327:

325:
326:
327: discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>
328:
329:


#3

Is it okay

that you use single quotes, the value won’t be substituted…

Regards,
Dennis

On 22/02/14 18:19, David Montgomery wrote:

What is the deal with chef and trying to read a file? I mean…the
file is there. The below is in a template.

discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>

ls -al /var/chef/cache/
-rw-r–r-- 1 root root 19 Feb 22 06:05 unicast_hosts

And yes #{Chef::Config[:file_cache_path]} = /var/chef/cache/

[2014-02-22T06:15:12-05:00] INFO: Sending resource update report
(run-id: e5f200ea-5d0b-4652-b02a-3b836357ca5b)
[2014-02-22T06:15:12-05:00] ERROR:

Chef::Mixin::Template::TemplateError (No such file or directory -
#{Chef::Config[:file_cache_path]}/unicast_hosts) on line #327:

325:
326:
327: discovery.zen.ping.unicast.hosts:
<%=File.read(’#{Chef::Config[:file_cache_path]}/unicast_hosts’)%>
328:
329:


#4

On Saturday, February 22, 2014 at 9:53 AM, Denis Barishev wrote:

Is it okay

that you use single quotes, the value won’t be substituted…

Regards,
Dennis

This is correct. Here’s an example in irb:

irb(main):016:0> ‘#{Time.now}’
=> "#{Time.now}"
irb(main):017:0> “#{Time.now}”
=> “2014-02-22 09:52:48 -0800”


Daniel DeLeo


#5

Also, if you are using Chef 11 then you can include it as a partial.

On Saturday, February 22, 2014, Daniel DeLeo dan@kallistec.com wrote:

On Saturday, February 22, 2014 at 9:53 AM, Denis Barishev wrote:

Is it okay

that you use single quotes, the value won’t be substituted…

Regards,
Dennis

This is correct. Here’s an example in irb:

irb(main):016:0> ‘#{Time.now}’
=> "#{Time.now}"
irb(main):017:0> “#{Time.now}”
=> “2014-02-22 09:52:48 -0800”


Daniel DeLeo