Chef Attributes


#1

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,

  1. 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)

  1. 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.


#2

For 1., I use the environment to define some attributes: for exemple, I
force some value for the users and sudo cookbooks like this:

override_attributes “authorization” => {
“sudo” => {
“groups” => [“sudo”,“admin”],
“passwordless” => false
}
}, “users” => {
“supergroup” => “admin”,
“active_groups” => [“group1”, “group2”]
}

Just make sure to have only one line for override_attribute, it’s one call.

for your point 2., I don’t have usage that looks like this: I always build
"composite" attribute as variables local to the cookbooks. But I can see
how this can be usefull.

On Wed, Dec 14, 2011 at 12:23 PM, bjbq4d@gmail.com wrote:

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,

  1. 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)

  1. 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.