3

Is it possible to configure attributes that are accessible across recipes? If yes, how? I've configured attributes for each recipe, but there is significant repetition I'd like to eliminate.

Ari
  • 4,121
  • 8
  • 40
  • 56

1 Answers1

5

The way Chef works is that at the start of a convergence Chef iterates through all cookbooks that have recipes that will be run on the node (either directly on the run list or referenced via include_recipe). For each cookbook Chef will then read in all of the attribute files stored in the attributes folder and add the attributes to the node hash.

The important thing to note here is that the attributes bear no relation to the recipes you are executing, and all recipes will see the same view of the attributes that were loaded.

Your description is a bit vague, but from what I can gather you have a cookbook with a few recipes, and for each recipe you are creating a corresponding attribute file. Within each of these attribute files you're re-defining the same attribute (e.g. default['myapp']['foo'] = "blah").

This is not-necessary due to the process I described at the start. At convergence time Chef will read through all of your attribute files regardless of what recipes you are running. This means that the second (or third, or fourth, etc) attribute file to be loaded will simply overwrite the node['myapp']['foo'] attribute that was set by the previously loaded attribute file.

In this situation, the common pattern is to have a default.rb attribute file which defines all of the common attributes that will be used by all of your recipes. If needed, you can then move your recipe/component-specific attributes to their own attribute file.

One thing to be aware of in this situation is that Chef loads attribute files in alphabetical order. If your recipe-specific attribute file (e.g. client.rb) references one of the attributes defined in default.rb, then you will need to use the include_attribute directive in order to get Chef to load default.rb before your client.rb, otherwise all of the attributes from default.rb will still be nil.

Jared Russell
  • 10,804
  • 6
  • 30
  • 31