Merging Chef attribute arrays

If you want to add individual hash elements to an array you can do it using the insertion operator << instead of the assignment operator =

In cookbook A

# Create the default attribute as an array
default[:test]=[{:baz => 'A', :qux => 'B'}]

In cookbook B

# Using array insertion on an existing array
default[:test] << {:baz => 'C', :qux => 'D'}



If the runlist order is not guaranteed to be A,B then you need to guard against trying to insert into an array which doesn't yet exist.

In cookbook A

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

In cookbook B

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



If you want to merge 2 arrays just use +=

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

# In cookbook B
default[:test] += [ {:baz => 'C', :qux => 'D'}, {:baz => 'E', :qux => 'F'} ]