Chef Environment Tutorials
What is Chef Environment?
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.
For instance, one environment may be called "testing" and another may be called "production". Since you don't want any code that is still in testing on your production machines, each machine can only be in one environment. You can then have one configuration for machines in your testing environment, and a completely different configuration for computers in production.
Additional environments can be created to reflect each organization’s patterns and workflow. For example, creating production, staging, testing, and development environments. Generally, an environment is also associated with one (or more) cookbook versions.
What is _default envionment?
By default, an environment called "_default" is created. Each node will be placed into this environment unless another environment is specified. Environments can be created to tag a server as part of a process group.
What is _default envionment?
By default, an environment called "_default" is created. Each node will be placed into this environment unless another environment is specified. Environments can be created to tag a server as part of a process group.
What envionment contents?
name
description
cookbook A version constraint for a single cookbook
cookbook_versions A version constraint for a group of cookbooks
default_attributes
override_attributes
Chef Environment Syntax Patterns
name 'environment_name'
description 'environment_description'
cookbook OR cookbook_versions 'cookbook' OR 'cookbook' => 'cookbook_version'
default_attributes 'node' => { 'attribute' => [ 'value', 'value', 'etc.' ] }
override_attributes 'node' => { 'attribute' => [ 'value', 'value', 'etc.' ] }
How to create a envionment?
- Creating a Ruby file in the environments sub-directory of the chef-repo and then pushing it to the Chef server
- Creating a JSON file directly in the chef-repo and then pushing it to the Chef server
- Using knife
- Using the Chef management console web user interface
- Using the Chef server REST API
Create and Manage chef environment using Knife
$ knife environment create development
$ knife environment create dev -d --description "The development environment."
$ knife environment delete ENVIRONMENT_NAME
$ knife environment list -w
$ knife environment show
$ knife environment show devops -F json
$ knife environment list
{
"name": "development",
"description": "",
"cookbook_versions": {
},
"json_class": "Chef::Environment",
"chef_type": "environment",
"default_attributes": {
},
"override_attributes": {
}
}
Create chef environment using Ruby
$ cd ~/chef-repo/environments
$ navi no development.rb
name "development"
description "The master development branch"
cookbook_versions({
"nginx" => "<= 1.1.0",
"apt" => "= 0.0.1"
})
override_attributes ({
"nginx" => {
"listen" => [ "80", "443" ]
},
"mysql" => {
"root_pass" => "root"
}
})