简体   繁体   中英

Merge attributes from different Chef cookbooks or recipes

Is there a possibility to merge attributes from several cookbooks or recipes?

What I want to achieve is the following:

cookbook 1

sets a default list of attributes like default[:bamboo][:agent][:attributes] = { 'system.attr-1' => 'test1' }

in template.conf , I have

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

cookbook 2

inherent "cookbook 1" and have 2 recipes

recipe1

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

recipe2

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

Now what I want is that the template.conf from "cookbook 1" is updated/merged with the attributes of cookbook2 and those recipes.

Is this possible? If not, what are the other options?

In cookbook 2, you want to take advantage of Ruby's Hash#merge :

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

And then ensure cookbook 2 depends on cookbook 1 (so the attributes are loaded first).

don't know if this is the most beatifull way to go but got it to work with

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

EDIT:: okay this does create a few problems because cookbook1 is goint to delete the old entries and when it finally arrives at cookbook2 recipe 1 it will add the entries again this causing on each chef-run a restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM