Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Let's say I have two cookbooks, Foo and Bar. In cookbook Foo, attributes/default.rb contains the following:

default[:test] = [{:baz => 'A', :qux => 'B'}]

In cookbook B, I'd like to expand that array with another object (i.e. merge the two arrays): {:baz => 'C', :qux => 'D'}, ideally from within a recipe.

I tried to put the following Bar's recipe, or the attributes file:

default[:test] = [{:baz => 'C', :qux => 'D'}]

Expecting the two to get merged and result in:

node[:test] == [{:baz => 'A', :qux => 'B'}, {:baz => 'C', :qux => 'D'}]

But that is not what happens. Instead, only one of the two objects is contained in the array during a Chef-Solo run. I also tried with default.override and default.set with the same results. How can I merge the two arrays?

Thanks.

share|improve this question
add comment

1 Answer

You can try using Chef's DeepMerge method from inside the second recipe like:

a = Chef::Mixin::DeepMerge.merge(node[:test], [ (new array here) ])

a will contain the deep-merged hashes!

share|improve this answer
    
That doesn't seem to work when I want to set node[:test] to the deeply merged array. Here's the exact code I'm running: Cookbook1/attributes/default.rb: default[:test] = [{:baz => 'A', :qux => 'B'}] Cookbook2/recipes/default.rb: a = Chef::Mixin::DeepMerge.merge(node[:test], [{:baz => 'C', :qux => 'D'}]) followed by node.override[:test] = a This however results in node[:test] being empty. –  Joseph S. Mar 5 '13 at 8:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.