Node attribute merge issue


#1

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


#2

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 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 -
Linkedinhttp://www.linkedin.com/in/gmiranda23/
Twitter https://twitter.com/gmiranda23

CHEF

GETCHEF.COM http://www.getchef.com/

TM

getchef.com http://www.getchef.com/ Bloghttp://www.opscode.com/blog/
Facebook https://www.facebook.com/getchefdotcom
Twitterhttps://twitter.com/getchefdotcom
Youtube https://www.youtube.com/getchef

Meet me at #ChefConf 2014 http://chefconf.com/