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 :
Generating the recipe,
chef generate recipe attributes
Open the recipe using
vi
command.vi test-cookbook/recipes/attributes.rb
Now create a file using the Chef recipe, by editing the recipe.
Check Ruby scripting, with output syntax Ok or Not.
chef exec ruby -c test-cookbook/recipes/attributes.rb
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 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 |
Subscribe to my newsletter
Read articles from Apurva Rajmane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by