rubychef-infrachef-attributes

Chef Attributes Structure


I am just trying to understand if there is a difference on how we define the attribute structure in the cookbook's attribute files.

Following are two examples:

// style-A
default['my_cookbook']['name'] = 'something'
default['my_cookbook']['version'] = '0.1.0'

// style-B
node.default.my_cookbook = {
  :name => 'something',
  :version => '0.1.0'
}

The reason I am asking this question is, in the previous versions of chef I kind of remember style-A is highly recommended and all the community cookbooks follow this style. I also recall seeing some issues with the style-B when I used to try to override the attributes from role or environment I would come up with a weird behavior that replaces the whole hash structure if I tried to override a single attribute in it.

I am not seeing this error now and I am on the latest 11 version of chef but was just wondering if this is something I should worry about. I am trying to be consistent with all my cookbooks with a single style and I need some advice on how I structure it. Does it matter at this point?


Solution

  • When using the default['key']['subkey'] style, default is a Mash and the []= method of Mash will convert Hash or an array of hashes to a Mash.

    The node.default.property = style will assign a Hash without calling the []= method.

    Mash inherits from Hash providing additional functionality, as well as overriding the default value of the initializer to {} which makes any undefined values a hash, which is then converted to a mash because of the behavior of mash.