在厨师自定义资源中获取呼叫食谱



我有一个带有自定义资源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行。

,这已经是默认行为。

最新更新