# Node attribute merge issue

**URL:** <https://discourse.chef.io/t/node-attribute-merge-issue/5085>\
**Category:** Chef Infra (archive)\
**Created:** [February 5, 2014, 11:54am UTC](https://discourse.chef.io/t/node-attribute-merge-issue/5085 "2014-02-05T11:54:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@Tom](https://discourse.chef.io/u/Tom)\
**Post date:** [February 5, 2014, 11:54am UTC](https://discourse.chef.io/t/node-attribute-merge-issue/5085/1 "2014-02-05T11:54:28Z")

</div>

Hi,

I am using chef-solo and I am trying to use data bags as a way of  
controlling some external environment data.

My workflow dictates that all cookbooks are stored in one git repository  
and I tag production releases.  
To remove sensitive and changeable env data from this repository, I have  
moved certain attributes into data bags and at run time I am loading those  
data bags from a directory in the solo config file.

I am trying to merge the data bag attributes into node attributes in a  
recipe doing the following:

* * *

envdata = data\_bag\_item(“databag”, “env-json-file”)

## envdata.each do |k,v| node.override[‘k’] = v end

## where my data bag is like this:

## { “id”: “env-json-file”, “cookbook\_name”: { “memcache”: { “servers”: [“server1:11211”, “server2:11211”] } } }

I would expect node[‘cookbook\_name’][‘memcache’][‘servers’] to have become  
the above array, but I am still finding the default attribute from the  
cookbook to be the value used.

Have I missed something fundamental in how attributes are merged,  
especially if they are several layers deep in a Hash?

Thanks,

Tom

---

<div class="post-metadata">

**Author:** ![George\_Miranda1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/george_miranda1/32/156_2.png) [@George\_Miranda1](https://discourse.chef.io/u/George_Miranda1)\
**Post date:** [February 5, 2014, 12:26pm UTC](https://discourse.chef.io/t/node-attribute-merge-issue/5085/2 "2014-02-05T12:26:42Z")

</div>

Hi Tom,

Your understanding of how attributes are merged should indeed work. In  
your pasted code, it looks like you're not interpolating the value of 'k',  
which would be why it's not working.

Two things:

1. Recommended best practice when setting node attributes is to stick to  
using defaults and let merge order do the rest. You could set default  
attributes once you're using the value of k and that should also work. It  
leaves you with plenty of remaining options if you later find yourself  
needing to then override these values elsewhere.

2. chef-solo now support environments. So you could use the built-in  
mechanisms for this rather than reinventing them by setting your  
environment data in a location outside of this repo and pointing to it by  
setting 'environment\_path' in your solo config file.

HTH,  
-g

On Wed, Feb 5, 2014 at 12:54 PM, Tom [tom.ashley@gmail.com](mailto:tom.ashley@gmail.com) wrote:

> Hi,
> 
> I am using chef-solo and I am trying to use data bags as a way of  
> controlling some external environment data.
> 
> My workflow dictates that all cookbooks are stored in one git repository  
> and I tag production releases.  
> To remove sensitive and changeable env data from this repository, I have  
> moved certain attributes into data bags and at run time I am loading those  
> data bags from a directory in the solo config file.
> 
> I am trying to merge the data bag attributes into node attributes in a  
> recipe doing the following:
> 
> * * *
> 
> envdata = data\_bag\_item("databag", "env-json-file")
> 
> ## envdata.each do |k,v| node.override['k'] = v end
> 
> ## where my data bag is like this:
> 
> ## { "id": "env-json-file", "cookbook\_name": { "memcache": { "servers": ["server1:11211", "server2:11211"] } } }
> 
> I would expect node['cookbook\_name']['memcache']['servers'] to have become  
> the above array, but I am still finding the default attribute from the  
> cookbook to be the value used.
> 
> Have I missed something fundamental in how attributes are merged,  
> especially if they are several layers deep in a Hash?
> 
> Thanks,
> 
> Tom

--

_George Miranda_ -- Consulting

512.481.2876 - [gmiranda@getchef.com](mailto:gmiranda@getchef.com) -  
Linkedin[http://www.linkedin.com/in/gmiranda23/](http://www.linkedin.com/in/gmiranda23/)  
Twitter [https://twitter.com/gmiranda23](https://twitter.com/gmiranda23)

CHEF

[GETCHEF.COM](http://GETCHEF.COM) [http://www.getchef.com/](http://www.getchef.com/)

TM

_[getchef.com](http://getchef.com) [http://www.getchef.com/](http://www.getchef.com/)_ Blog[http://www.opscode.com/blog/](http://www.opscode.com/blog/)  
Facebook [https://www.facebook.com/getchefdotcom](https://www.facebook.com/getchefdotcom)  
Twitter[https://twitter.com/getchefdotcom](https://twitter.com/getchefdotcom)  
Youtube [https://www.youtube.com/getchef](https://www.youtube.com/getchef)

_Meet me at #ChefConf 2014_ [http://chefconf.com/](http://chefconf.com/)
