2

我有一本my_service带有自定义资源的食谱write_config。该资源是从另一本食谱中调用的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'
    ...
    

整洁的!

4

1 回答 1

2

这被自动跟踪为new_resource.cookbook_name。您可能需要to_s它,因为它有时会以某种方式结束,但否则应该是您需要的。

cookbook如果您只是删除该行,这也专门针对模板,这已经是默认行为。

于 2017-04-20T18:30:07.900 回答