Chef Attributes

Apurva RajmaneApurva Rajmane
3 min read

In chef, attributes are key-value pairs that represents specific details about a node.

Attributes are used by Chef Infra Client to understand:

  • The Current state of the node.

  • What the state should be the node was at the end of the previous Chef Infra Client run.

  • What the state of the node should be at the end of the current Chef Infra Client run.

Attributes are defined by:

  • The node as saved on the Chef Infra Server

  • Attributes passed using JSON on the command line

  • Cookbooks (in attribute files and/or recipes)

  • Policyfiles

Types of Attributes

Attribute TypeDescription
defaultA default attribute is automatically reset at the start of every chef-client run and has the lowest attribute precedence. Use default attributes as often as possible in cookbooks.
force_defaultUse the force_default attribute to ensure that an attribute defined in a cookbook (by an attribute file or by a recipe) takes precedence over a defaultattribute set by a role or an environment.
normalA normal attribute is a setting that persists in the node object. A normal attribute has a higher attribute precedence than a default attribute.
overrideAn override attribute is automatically reset at the start of every chef-client run and has a higher attribute precedence than default, force_default, and normal attributes.
force_overrideUse the force_override attribute to ensure that an attribute defined in a cookbook (by an attribute file or by a recipe) takes precedence over an override attribute set by a role or an environment.
automaticAn automatic attribute contains data that is identified by Ohai at the beginning of every chef-client run.

Example :

  1. Generating the recipe,

    chef generate recipe attributes

  2. Open the recipe using vi command.

    vi test-cookbook/recipes/attributes.rb

  3. Now create a file using the Chef recipe, by editing the recipe.

  4. Check Ruby scripting, with output syntax Ok or Not.

    chef exec ruby -c test-cookbook/recipes/attributes.rb

  5. Execute the recipe

    chef-client -zr "recipe[test-cookbook::attributes]"

Output- basicinfo file is created with the content.

Attributes Array

A recipe can store attribute values using a multi-level hash or array.

For example, a group of attributes for web servers might be:

override_attributes(
  :apache => {
    :listen_ports => [ 80 ],
    :prefork => {
      :startservers => 20,
      :minspareservers => 20,
      :maxspareservers => 40
    }
  }
)

Attribute Precedence

Chef Infra Client applies attributes in the following order:

Application Order (Last One Wins)Attribute TypeSource Order
1defaultCookbook attribute fileRecipeEnvironmentRole
2force_defaultCookbook attribute fileRecipe
3normalJSON file passed with chef-client -jCookbook attribute fileRecipe
4overrideCookbook attribute fileRecipeRoleEnvironment
5force_overrideCookbook attribute fileRecipe
6automaticIdentified by Ohai at the start of a Chef Infra Client Run
0
Subscribe to my newsletter

Read articles from Apurva Rajmane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Apurva Rajmane
Apurva Rajmane