1

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

我想要实现的是以下内容:

食谱 1

设置默认属性列表,例如 default[: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'
   }

现在我想要的是 来自“cookbook 1”的template.conf与cookbook2 和那些食谱的属性更新/合并。

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

4

2 回答 2

2

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

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

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

于 2014-08-13T14:01:12.270 回答
0

不知道这是否是最美丽的方式,但可以使用

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 配方1 时,它将再次添加条目,这会导致每次厨师运行重新启动

于 2014-08-15T10:39:53.113 回答