Using JSON in chef cookbook attributes

One of the little niggles with chef that annoys me is the fact you can't use a JSON file for cookbook attributes, yet role and node attributes are in JSON, instead you're stuck with having to transpose between ruby and JSON.

This little trick works around the issue by reading in a JSON file and merging in with the default attribute object accessible from default.rb. Obviously you could expand on this to control the attribute precedence if you like.


In your cookbook, create attributes/default.rb like this:

require 'json'
file = + '/../attributes.json')
default.merge! JSON.parse(file)

Then in your cookbook root, create an attributes.json file. I know what you're thinking, why not just put this in the attributes folder, well for some reason when you knife the cookbook up, it removes non .rb files from that folder!

   "your": "attributes",
   "in": "json"