I have not seen any official best practice regarding how to redefine attributes in wrapper cookbooks, I think you need to choose a style and as long as all your cookbooks match your organization, you should be good. What follows my opinions.
Attribute File
First, we used the attribute file separation style similar to that described in this format attributes/customize.rb . However, we found it much easier to just save all the attributes in attributes/default.rb , especially since our attributes remain under 50 lines of code.
Override Priority
We always use the lowest attribute priority when redefining attributes in our cookie wrapper book. We adhere to default whenever possible, because the order of priority dictates your cookbook. default wins over wrapped cookbooks by default .
Attribute Access Style
Most cookbooks you see there use the string access format.
default['foo']['bar'] = 42
In fact, there is a power rule , which specifically prevents the use of access to characters. Foodcritic also recommends using a consistent style to access attributes . If you're deciding on a style, keep in mind that the community has pretty much defined the string access format.
However, there is another way. The attributes in the chef are not Hash , they are actually Mash . Mash supports access to attributes as methods, therefore:
default.foo.bar = 42
We prefer this syntax because it is more compact.
However, be careful, as Tejay Cardon points out , Chef did this with method_missing so that you might run into problems if the attribute name matches the name of the current (or future) method of the node object. We are the foreword to all of our cookbooks with our name, so clashes are unlikely, but when we redefine the community cookbooks, we may run into problems. Fortunately, testing should catch any such omissions.
Warning note
Overriding attributes in a heap wrapper book will not work in unexpected ways, especially when using string interpolation. This is very well explained in this blog post . The main thing is if the cookbook defines attributes as:
default['version'] = '1.0' default['url'] = "http://example.com/#{node['version']}.zip"
And in your wrapper cookbook, you override version :
default['version'] = '2.0'
default['url'] will still be http://example.com/1.0.zip , not http://example.com/2.0.zip , as you would expect. This is because the url attribute is interpolated until your redefinition occurs. The blog post goes deep and offers a potential solution.
Wrap up
At the end of the day, there are several ways to do something at the Chef, and the community is still ripening. There are some recommendations for writing reusable cookbooks, etc., but the practice is still evolving.
The best you can do is try different styles and find out which one is best for you and your team. After you decide on a style, I would suggest writing a style guide (I used this Angular style guide as inspiration ). You can also apply style guides using custom food rules . We have had success with this approach.