Chef - Environment

Chef helps in performing environment specific configuration. It is always a good idea to have a separate environment for development, testing, and production.

Chef enables grouping nodes into separate environments to support an ordered development flow.

Creating an Environment

Creation of environment on the fly can be done using the knife utility. Following command will open a Shell’s default editor, so that one can modify the environment definition.

vipin@laptop:~/chef-repo $ knife environment create book { 
   "name": "book", 
   "description": "", 
   "cookbook_versions": { 
   "json_class": "Chef::Environment", 
   "chef_type": "environment", 
   "default_attributes": { 
   "override_attributes": { 
Created book 

Testing a Created Environment

vipin@laptop:~/chef-repo $ knife environment list 

List Node for All Environments

vipin@laptop:~/chef-repo $ knife node list 

_default Environment

Each organization will always start with at least a single environment called default environment, which is always available to the Chef server. A default environment cannot be modified in anyway. Any kind of changes can only be accommodated in the custom environment that we create.

Environment Attributes

An attribute can be defined in an environment and then used to override the default settings in the node. When the Chef client run takes place, then these attributes are compared with the default attributes that are already present in the node. When the environment attributes take precedence over the default attributes, Chef client will apply these settings and values when the Chef client run takes place on each node.

An environment attribute can only be either default_attribute or override_attribute. It cannot be a normal attribute. One can use default_attribute or override_attribute methods.

Attribute Type

Default − A default attribute is always reset at the start of every Chef client run and have the lowest attribute precedence.

Override − An override attribute is always reset at the start of every Chef client run and has a higher attribute precedence than default, force_default and normal. An override attribute is most often defined in the recipe but can also be specified in an attribute file for a role or for an environment.

Order of Applying an Attribute

Order Attribute