# Array attributes merge

**URL:** <https://discourse.chef.io/t/array-attributes-merge/4584>\
**Category:** Chef Infra (archive)\
**Created:** [October 21, 2013, 12:22pm UTC](https://discourse.chef.io/t/array-attributes-merge/4584 "2013-10-21T12:22:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aL1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/al1/32/228_2.png) [@aL1](https://discourse.chef.io/u/aL1)\
**Post date:** [October 21, 2013, 12:22pm UTC](https://discourse.chef.io/t/array-attributes-merge/4584/1 "2013-10-21T12:22:14Z")

</div>

Hi guys!

According to this:  
[http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)

Array attributes are merged, i have checked out that this is the behaviour  
when using default attributes on attribute files and roles.

But when i try to define a default attribute on one recipe:

node.default[‘foo’] = [‘bar’]

The one that i have already on defaults.rb attribute file:

node.default[‘foo’] = [‘littlebar’]

Is overrided by the one i defined on the recipe, resulting node[‘foo’] =  
[‘bar’]

Defining on a role:

default\_attributes (:foo =\> [‘bar’])

Works as expected, and results on node[‘foo’] = [‘littlebar’,‘bar’]

So what exactly node.default[‘x’] = value do on recipes? Looks like a  
direct override… O\_o

–  
Si necesitas una máquina para hacer algo y no la compras al final te darás  
cuenta de que has pagado lo mismo y no tienes la máquina.–Henry Ford

Alberto

---

<div class="post-metadata">

**Author:** ![Michael\_Glenney](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@Michael\_Glenney](https://discourse.chef.io/u/Michael_Glenney)\
**Post date:** [October 21, 2013, 3:04pm UTC](https://discourse.chef.io/t/array-attributes-merge/4584/2 "2013-10-21T15:04:55Z")

</div>

I don't have a complete answer but I know that:

1. The doc you linked to specifically calls out roles and environments. Might be a clue
2. When writing a provider for a cookbook attributes are mutable. So maybe this will work for you within a recipe:

node.default['foo'] \<\< 'bar'

Sent from my iPhone

> On Oct 21, 2013, at 5:22 AM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com) wrote:
> 
> Hi guys!
> 
> According to this: [http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)
> 
> Array attributes are merged, i have checked out that this is the behaviour when using default attributes on attribute files and roles.
> 
> But when i try to define a default attribute on one recipe:
> 
> node.default['foo'] = ['bar']
> 
> The one that i have already on defaults.rb attribute file:
> 
> node.default['foo'] = ['littlebar']
> 
> Is overrided by the one i defined on the recipe, resulting node['foo'] = ['bar']
> 
> Defining on a role:
> 
> default\_attributes (:foo =\> ['bar'])
> 
> Works as expected, and results on node['foo'] = ['littlebar','bar']
> 
> So what exactly node.default['x'] = value do on recipes? Looks like a direct override... O\_o
> 
> --  
> Si necesitas una máquina para hacer algo y no la compras al final te darás cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford  
> Alberto

---

<div class="post-metadata">

**Author:** ![Luke\_Bradbury](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/luke_bradbury/32/458_2.png) [@Luke\_Bradbury](https://discourse.chef.io/u/Luke_Bradbury)\
**Post date:** [October 21, 2013, 3:22pm UTC](https://discourse.chef.io/t/array-attributes-merge/4584/3 "2013-10-21T15:22:33Z")

</div>

My experience with this issue was similar, however I found that \<\< doesn't  
play nice with the autovivification.  
Consequently I've been using this pattern as a workaround:

node.default['foo'] = node['foo'].to\_a.push('bar')

Regards,  
Luke

On 21 October 2013 16:04, Michael Glenney [mike.glenney@gmail.com](mailto:mike.glenney@gmail.com) wrote:

> I don't have a complete answer but I know that:
> 
> 1. The doc you linked to specifically calls out roles and environments.  
> Might be a clue
> 2. When writing a provider for a cookbook attributes are mutable. So  
> maybe this will work for you within a recipe:
> 
> node.default['foo'] \<\< 'bar'
> 
> Sent from my iPhone
> 
> On Oct 21, 2013, at 5:22 AM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com) wrote:
> 
> Hi guys!
> 
> According to this:  
> [http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)
> 
> Array attributes are merged, i have checked out that this is the behaviour  
> when using default attributes on attribute files and roles.
> 
> But when i try to define a default attribute on one recipe:
> 
> node.default['foo'] = ['bar']
> 
> The one that i have already on defaults.rb attribute file:
> 
> node.default['foo'] = ['littlebar']
> 
> Is overrided by the one i defined on the recipe, resulting node['foo'] =  
> ['bar']
> 
> Defining on a role:
> 
> default\_attributes (:foo =\> ['bar'])
> 
> Works as expected, and results on node['foo'] = ['littlebar','bar']
> 
> So what exactly node.default['x'] = value do on recipes? Looks like a  
> direct override... O\_o
> 
> --  
> Si necesitas una máquina para hacer algo y no la compras al final te darás  
> cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford
> 
> Alberto

---

<div class="post-metadata">

**Author:** ![aL1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/al1/32/228_2.png) [@aL1](https://discourse.chef.io/u/aL1)\
**Post date:** [October 22, 2013, 6:27am UTC](https://discourse.chef.io/t/array-attributes-merge/4584/4 "2013-10-22T06:27:25Z")

</div>

Even += works in order to append values he he.

May be that recipes attr definitions just override attr files ones.

Mike: I have checked out that arrays merge works well on defaults attr  
files + defaults on role. So environment and roles examples on the link,  
are just examples, i think.

Can a opscode dev, explain what is the exactly behaviour with arrays on  
attributes? Looks like it depends where you define the attr override works  
as expected o\_O  
El 21/10/2013 16:22, "Luke Bradbury" [luke.dolman@gmail.com](mailto:luke.dolman@gmail.com) escribió:

> My experience with this issue was similar, however I found that \<\< doesn't  
> play nice with the autovivification.  
> Consequently I've been using this pattern as a workaround:
> 
> node.default['foo'] = node['foo'].to\_a.push('bar')
> 
> Regards,  
> Luke
> 
> On 21 October 2013 16:04, Michael Glenney [mike.glenney@gmail.com](mailto:mike.glenney@gmail.com) wrote:
> 
> > I don't have a complete answer but I know that:
> > 
> > 1. The doc you linked to specifically calls out roles and environments.  
> > Might be a clue
> > 2. When writing a provider for a cookbook attributes are mutable. So  
> > maybe this will work for you within a recipe:
> > 
> > node.default['foo'] \<\< 'bar'
> > 
> > Sent from my iPhone
> > 
> > On Oct 21, 2013, at 5:22 AM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com)  
> > wrote:
> > 
> > Hi guys!
> > 
> > According to this:  
> > [http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)
> > 
> > Array attributes are merged, i have checked out that this is the  
> > behaviour when using default attributes on attribute files and roles.
> > 
> > But when i try to define a default attribute on one recipe:
> > 
> > node.default['foo'] = ['bar']
> > 
> > The one that i have already on defaults.rb attribute file:
> > 
> > node.default['foo'] = ['littlebar']
> > 
> > Is overrided by the one i defined on the recipe, resulting node['foo'] =  
> > ['bar']
> > 
> > Defining on a role:
> > 
> > default\_attributes (:foo =\> ['bar'])
> > 
> > Works as expected, and results on node['foo'] = ['littlebar','bar']
> > 
> > So what exactly node.default['x'] = value do on recipes? Looks like a  
> > direct override... O\_o
> > 
> > --  
> > Si necesitas una máquina para hacer algo y no la compras al final te  
> > darás cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford
> > 
> > Alberto

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [October 22, 2013, 6:32am UTC](https://discourse.chef.io/t/array-attributes-merge/4584/5 "2013-10-22T06:32:01Z")

</div>

[Arrays and Chef Attributes – Noah Kantrowitz](https://coderanger.net/2013/06/arrays-and-chef/) goes over this in great detail 🙂

--Noah

On Oct 21, 2013, at 11:27 PM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com) wrote:

> Even += works in order to append values he he.
> 
> May be that recipes attr definitions just override attr files ones.
> 
> Mike: I have checked out that arrays merge works well on defaults attr files + defaults on role. So environment and roles examples on the link, are just examples, i think.
> 
> Can a opscode dev, explain what is the exactly behaviour with arrays on attributes? Looks like it depends where you define the attr override works as expected o\_O
> 
> El 21/10/2013 16:22, "Luke Bradbury" [luke.dolman@gmail.com](mailto:luke.dolman@gmail.com) escribió:  
> My experience with this issue was similar, however I found that \<\< doesn't play nice with the autovivification.  
> Consequently I've been using this pattern as a workaround:
> 
> node.default['foo'] = node['foo'].to\_a.push('bar')
> 
> Regards,  
> Luke
> 
> On 21 October 2013 16:04, Michael Glenney [mike.glenney@gmail.com](mailto:mike.glenney@gmail.com) wrote:  
> I don't have a complete answer but I know that:
> 
> 1. The doc you linked to specifically calls out roles and environments. Might be a clue
> 2. When writing a provider for a cookbook attributes are mutable. So maybe this will work for you within a recipe:
> 
> node.default['foo'] \<\< 'bar'
> 
> Sent from my iPhone
> 
> On Oct 21, 2013, at 5:22 AM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com) wrote:
> 
> > Hi guys!
> > 
> > According to this: [http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)
> > 
> > Array attributes are merged, i have checked out that this is the behaviour when using default attributes on attribute files and roles.
> > 
> > But when i try to define a default attribute on one recipe:
> > 
> > node.default['foo'] = ['bar']
> > 
> > The one that i have already on defaults.rb attribute file:
> > 
> > node.default['foo'] = ['littlebar']
> > 
> > Is overrided by the one i defined on the recipe, resulting node['foo'] = ['bar']
> > 
> > Defining on a role:
> > 
> > default\_attributes (:foo =\> ['bar'])
> > 
> > Works as expected, and results on node['foo'] = ['littlebar','bar']
> > 
> > So what exactly node.default['x'] = value do on recipes? Looks like a direct override... O\_o
> > 
> > --  
> > Si necesitas una máquina para hacer algo y no la compras al final te darás cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford  
> > Alberto

---

<div class="post-metadata">

**Author:** ![aL1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/al1/32/228_2.png) [@aL1](https://discourse.chef.io/u/aL1)\
**Post date:** [October 22, 2013, 7:18am UTC](https://discourse.chef.io/t/array-attributes-merge/4584/6 "2013-10-22T07:18:29Z")

</div>

Hi Noah!

I have read it ofc. But the thing is that array attributes dont are  
set-unioned when you define the attr on a recipe. That is the thing.

When you use roles + defaults.rb file it works as you say on your blog. But  
when using defaults.rb file and recipe attrs, it doesnt work!

--  
Si necesitas una máquina para hacer algo y no la compras al final te darás  
cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford

Alberto

On Tue, Oct 22, 2013 at 7:32 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)wrote:

> [Arrays and Chef Attributes – Noah Kantrowitz](https://coderanger.net/2013/06/arrays-and-chef/) goes over this in great  
> detail 🙂
> 
> --Noah
> 
> On Oct 21, 2013, at 11:27 PM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com)  
> wrote:
> 
> > Even += works in order to append values he he.
> > 
> > May be that recipes attr definitions just override attr files ones.
> > 
> > Mike: I have checked out that arrays merge works well on defaults attr  
> > files + defaults on role. So environment and roles examples on the link,  
> > are just examples, i think.
> > 
> > Can a opscode dev, explain what is the exactly behaviour with arrays on  
> > attributes? Looks like it depends where you define the attr override works  
> > as expected o\_O
> > 
> > El 21/10/2013 16:22, "Luke Bradbury" [luke.dolman@gmail.com](mailto:luke.dolman@gmail.com) escribió:  
> > My experience with this issue was similar, however I found that \<\<  
> > doesn't play nice with the autovivification.  
> > Consequently I've been using this pattern as a workaround:
> > 
> > node.default['foo'] = node['foo'].to\_a.push('bar')
> > 
> > Regards,  
> > Luke
> > 
> > On 21 October 2013 16:04, Michael Glenney [mike.glenney@gmail.com](mailto:mike.glenney@gmail.com)  
> > wrote:  
> > I don't have a complete answer but I know that:
> > 
> > 1. The doc you linked to specifically calls out roles and environments.  
> > Might be a clue
> > 2. When writing a provider for a cookbook attributes are mutable. So  
> > maybe this will work for you within a recipe:
> > 
> > node.default['foo'] \<\< 'bar'
> > 
> > Sent from my iPhone
> > 
> > On Oct 21, 2013, at 5:22 AM, "aL." [ocholetrasaleatorias@gmail.com](mailto:ocholetrasaleatorias@gmail.com)  
> > wrote:
> > 
> > > Hi guys!
> > > 
> > > According to this:  
> > > [http://docs.opscode.com/essentials\_node\_object\_deep\_merge.html#addition](http://docs.opscode.com/essentials_node_object_deep_merge.html#addition)
> > > 
> > > Array attributes are merged, i have checked out that this is the  
> > > behaviour when using default attributes on attribute files and roles.
> > > 
> > > But when i try to define a default attribute on one recipe:
> > > 
> > > node.default['foo'] = ['bar']
> > > 
> > > The one that i have already on defaults.rb attribute file:
> > > 
> > > node.default['foo'] = ['littlebar']
> > > 
> > > Is overrided by the one i defined on the recipe, resulting node['foo']  
> > > = ['bar']
> > > 
> > > Defining on a role:
> > > 
> > > default\_attributes (:foo =\> ['bar'])
> > > 
> > > Works as expected, and results on node['foo'] = ['littlebar','bar']
> > > 
> > > So what exactly node.default['x'] = value do on recipes? Looks like a  
> > > direct override... O\_o
> > > 
> > > --  
> > > Si necesitas una máquina para hacer algo y no la compras al final te  
> > > darás cuenta de que has pagado lo mismo y no tienes la máquina.--Henry Ford  
> > > Alberto
