Chapter 8. Attributes

Attributes represent information about your node. In addition to the information that can be automatically generated by ohai, you can set attributes in Chef recipes or in separate attribute files.

Attribute files are located in the attributes folder of a cookbook. Similar to recipes, the default attribute file is called:

<cookbook>
└── attributes
    └── default.rb

Figure 8-1 shows the format of an attribute when it is specified in a cookbook attribute file.

Setting attributes in attribute files
Figure 8-1. Setting attributes in attribute files

Attributes can also be set directly in recipes. Figure 8-2 shows the format of an attribute when it is set in a recipe. You must precede the attribute name with node. when you set an attribute directly in a recipe.

Setting attributes in recipes
Figure 8-2. Setting attributes in recipes

Because attributes can be defined in multiple places, all attribute values are composed together during a Chef run according to the priority levels as shown in Figure 8-3. Attributes defined by ohai have the highest priority, followed by attributes defined in a recipe, then attributes defined in an attribute file. In other words, recipe attributes have a higher priority than those defined in attribute file, and will override them by default. Attributes defined by ohai trump everything else.

Figure 8-3. Attribute priority ...

Get Learning Chef now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.