我有一个带有自定义资源write_config
的食谱my_service
。此资源是从另一份食谱node_a
来调用的。
-
my_service/resources/write_config.rb
:property :template_source, String, name_property: true property :calling_cookbook, String, required: true action :create do template "/tmp/test.txt" do source template_source cookbook calling_cookbook owner 'root' mode '0644' action :create end end
-
节点食谱食谱
node_a/default.rb
:my_service_write_config 'config.erb' do calling_cookbook 'node_a' end
这很好,但是我想摆脱calling_cookbook
属性。
有没有办法自动获取calling_cookbook
名称?
解决方案
(谢谢Coderanger!(基本资源template
似乎在调用(node_a
(食谱(?(的上下文中进行评估。
-
my_service/resources/write_config.rb
:property :template_source, String, name_property: true action :create do template "/tmp/test.txt" do source template_source owner 'root' mode '0644' action :create end end
-
在节点食谱食谱中使用它
node_a/default.rb
成为单线:... include_recipe 'my_service' my_service_write_config 'config.erb' ...
整洁!
这是自动跟踪为new_resource.cookbook_name
的。您可能需要to_s
,因为有时它以某种方式最终会出现一个符号,否则应该是您需要的。
也专门针对模板,如果您删除cookbook
行。