您可以通过以下方式控制模板来自的食谱cookbook
模板资源的属性。对于自定义资源,cookbook 属性默认为使用资源的调用说明书,而不是声明资源的位置。您可以轻松地覆盖它,因为您知道您正在编写的食谱的名称。所以我们可以这样:
食谱/mytemplate/resources/foo.rb
provides :foo
property :template_source, String, default: "foo.erb"
property :template_cookbook, String, default: "mytemplate"
action :run do
template "/tmp/foo.xyz" do
source new_resource.template_source
cookbook new_resource.template_cookbook
end
end
食谱/mytemplate/templates/foo.erb:
THIS IS THE DEFAULT TEMPLATE
食谱/测试/metadata.rb:
name "test"
version "0.0.1"
depends "mytemplate"
食谱/测试/食谱/default.rb:
foo "whatever"
然后,调用者可以使用自己的模板名称和位置来覆盖它,并将模板“依赖注入”到资源中:
食谱/测试/食谱/default.rb:
foo "whatever" do
template_cookbook cookbook_name
template_source "bar.erb"
end
食谱/测试/模板/bar.erb:
THIS IS THE OVERRIDDEN TEMPLATE
The cookbook_file
资源有一个cookbook
应该以相同的方式工作的属性。
您可以在定义属性的 sous-chef tomcat 食谱中“野外”使用此模式here https://github.com/sous-chefs/tomcat/blob/35195822e051a856f342fd7f80e1ee77bdfabf15/resources/service_upstart.rb#L35-L36然后在它渲染的模板资源中使用here https://github.com/sous-chefs/tomcat/blob/35195822e051a856f342fd7f80e1ee77bdfabf15/resources/service_upstart.rb#L90 and here https://github.com/sous-chefs/tomcat/blob/35195822e051a856f342fd7f80e1ee77bdfabf15/resources/service_upstart.rb#L100