# Chef Attributes

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 Type** | **Description** |
| --- | --- |
| default | A 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\_default | Use 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. |
| normal | A normal attribute is a setting that persists in the node object. A normal attribute has a higher attribute precedence than a default attribute. |
| override | An 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\_override | Use 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. |
| automatic | An 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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716105608275/0ee949cb-7e93-482f-8dc2-f5ea2fd15afd.png align="left")
    
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]"`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716105628012/49fb3c44-e752-4d31-859b-0c64b2c491b3.png align="left")

**Output-** basicinfo file is created with the content.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716105644247/610464e8-4864-4505-8f61-1784b383359a.png align="center")

### 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:

```plaintext
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 Type** | **Source Order** |
| --- | --- | --- |
| 1 | `default` | Cookbook attribute fileRecipeEnvironmentRole |
| 2 | `force_default` | Cookbook attribute fileRecipe |
| 3 | `normal` | JSON file passed with `chef-client -j`Cookbook attribute fileRecipe |
| 4 | `override` | Cookbook attribute fileRecipeRoleEnvironment |
| 5 | `force_override` | Cookbook attribute fileRecipe |
| 6 | `automatic` | Identified by Ohai at the start of a Chef Infra Client Run |
