【问题标题】:Merge attributes from different Chef cookbooks or recipes合并来自不同厨师食谱或食谱的属性
【发布时间】:2017-02-07 05:21:40
【问题描述】:

是否可以合并多个食谱或食谱的属性?

我想要达到的目标如下:

食谱 1

设置默认属性列表 喜欢 默认[:bamboo][:agent][:attributes] = { 'system.attr-1' => 'test1' }

template.conf中,我有

<% if @options -%>
<%   @options.sort.map do | option, value | -%>
<%= option %>=  <%= value %>
<%   end -%>
<% end -%>

食谱 2

固有的“食谱 1” 并有 2 个食谱

食谱1

   node.default[:bamboo][:agent][:attributes]                = {
       'system.attr-2'                           => 'test2'
   }

食谱2

   node.default[:bamboo][:agent][:attributes]                = {
       'system.attr-3'                           => 'test3'
   }

现在我想要的是 template.conf "cookbook 1" 更新/合并了 cookbook2 和那些食谱的属性。

这可能吗?如果没有,还有哪些其他选择?

【问题讨论】:

    标签: ruby chef-infra chef-recipe cookbook berkshelf


    【解决方案1】:

    在食谱 2 中,您想利用 Ruby 的 Hash#merge

    node.default[:bamboo][:agent][:attributes] = node[:bamboo][:agent][:attributes].merge(
      'system.attr-3' => 'test3'
    )
    

    然后确保食谱 2 依赖于食谱 1(因此首先加载属性)。

    【讨论】:

    • 但这意味着我需要为每个配方添加一段代码来创建 template.conf 正确吗?
    【解决方案2】:

    不知道这是否是最漂亮的方式,但可以使用它

    node.default[:bamboo][:agent][:attributes] = node[:bamboo][:agent][:attributes].merge(
        'system.attr-3' => 'test3'
    )
    
    template 'bamboo-capabilities.properties' do
    path "#{node[:bamboo][:agent][:data_dir]}/bin/bamboocapabilities.properties"
    source 'bamboo-capabilities.properties.erb'
    cookbook 'bamboo'
      owner  node[:bamboo][:agent][:user]
      group  node[:bamboo][:agent][:group]
      mode 0644
      variables(
          :options => node[:bamboo][:agent][:attributes]
      )
      notifies :restart, 'service[bamboo-agent]', :delayed
    end
    

    编辑:: 好的,这确实会产生一些问题 因为cookbook1 将要删除旧条目,当它最终到达时 在 cookbook2 recipe 1 它将再次添加条目 这导致每个厨师运行重新启动

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      相关资源
      最近更新 更多