I recently discovered that I many of the cookbooks I had written would not work
correctly with roles/environments. The main reasons for this were that,
- In the ‘attributes’ file I would do a check to see if certain attributes
were defined. I wrote this because chef is new at where I work, and there was
no reasonable defaults for these variables so we took a fail-fast approach.
In order to fix this I had to move this code to a recipe. (Role/Environment
attributes aren’t available in/during the attributes file)
- In the ‘attributes’ file I often had attributes that were built from other
attributes. For example,
default[“lib_path”] = "/usr/lib"
default[“app_lib_path”] = “#{node[“lib_path”]}/app”
This fails for the same reason as above and is fixed in the same manner.
I was curious if anyone else had fallen into this trap or any opinions on how
this was handled/written.
I like the idea of keeping this kind of code in the ‘attributes’ file as it can
become obnoxious if there are many recipes and I have to either duplicate all
this code in each recipe or build an ‘attribute’ recipe and have each recipe
include it.