如何在配方编辑时读取食谱文件? [英] How to read cookbook file at recipe compilation time?

查看:52
本文介绍了如何在配方编辑时读取食谱文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种事情在厨师食谱中很常见:

This kind of thing is commonly seen in Chef recipes:

%w{foo bar baz}.each do |x|
  file "#{x}" do
    content "whatever"
  end
end

但是我想从菜谱保存的文件中读取要循环播放的项目,例如:

But I want to read the items to loop over from a file which is kept with the cookbook, for example:

File.open('files/default/blah.txt').each do |x|
  file "#{x}" do
    content "whatever"
  end
end

如果我给出 blah.txt 的完整路径,则该方法有效,厨师客户端恰巧将其缓存,但它不可移植。如果我像在示例中希望那样将当前目录作为食谱的根目录,则无法正常工作。

This works if I give the full path to blah.txt where chef-client happens to cache it, but it's not portable. It doesn't work if I write it like in the example, "hoping" that the current directory is the root of the cookbook. Is there a way to obtain the cookbook root directory as the recipes are compiled?

推荐答案

另一种解决方案,对于那些不喜欢的人,是否可以通过获取食谱来获取食谱的根目录?在收敛之前需要文件内容,但是不需要任何client.rb修改的文件是使用cookbook_file将资源读取到file_cache_path中,然后延迟加载它。这是一个示例,其中我读取了要嵌入到xml模板中的常规脚本。

Another solution, for those who don't need the file content until converge time, but one that does not require any client.rb modifications, is to use cookbook_file to read the resource into your file_cache_path and then lazy load it. Here is an example where I read in a groovy script to be embedded into an xml template.

script_file = "#{Chef::Config['file_cache_path']}/bootstrap-machine.groovy"
cookbook_file script_file do
    source 'bootstrap-machine.groovy'
end

config_xml = "#{Chef::Config['file_cache_path']}/bootstrap-machine-job.xml"

template config_xml do
    source 'bootstrap-machine-job.xml.erb'
    variables(lazy {{
        :groovy_script => File.open(script_file).read
    }})
end

这篇关于如何在配方编辑时读取食谱文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆