Having separate environment for development,testing and production is a good way to be able to develop and test cookbook updates,and other configurations changes in isolation.Chef enables you to group your nodes into separate environments so as to support an ordered development flow.
Example:
assume that you have a node named server in the _default environment and that you have at least one cookbook (I will use the ntp cookbook) registered with your chef server.
How to do it?
How to manipulate environment using knife?
Note:
This is only a good idea if you want to play around. For serious work, please create files describing your environments and put them under version control as described in the there's more section of this recipe.
Make sure you have set your EDITOR environment variable to your preferred one.
1) Create your environment on-the-fly using knife.The following command will open your shell's default editor so that you can modify the environment definition:
local@workstation:~/chef-repo $ knife environment create dev
{
"name": "dev",
"description": "",
"cookbook_versions": {
},
"json_class": "Chef::Environment",
"chef_type": "environment"
"default_attributes": {
},
"override_attributes":{
}
}
Created dev
2) List the available environments:
local@workstation:~/chef-repo $ knife environment list
_default
dev
3) List the nodes for all the environments:
local@workstation:~/chef-repo $ knife node list
server
4) Verify that the node server is not in the dev environment yest by listing nodes in the devenvironment only:
local@workstation:~/chef-repo $ knife node list -E dev
local@workstation:~/chef-repo $
5) Change the environment of the server to dev using knife:
local@workstation:~/chef-repo $ knife node environment set server book
server:
chef_environment: dev
6) List the nodes in the dev environment again:
local@workstation:~/chef-repo $ knife node list -E dev
server
7) Use specific cookbook versions and override certain attributes for the environment:
local@workstation:~/chef-repo $ knife environment edit dev
{
"name": "dev"
"description": "",
"cookbook_versions": {
"ntp": "1.6.8"
},
"json_class": "Chef::Environment",
"chef_type": "environment",
"default_attributes": {
},
"override_attributes":{
"ntp": {
"servers": ["0.europe.pool.ntp.org", "1.europe.pool.ntp.org", "2.europe.pool.ntp.org",
"3.europe.pool.ntp.org"]
}
}
Saved dev
How it works?
A common use of environment is to promote cookbook updates from development to staging and then in to the production.Additionally,they enable you to use different cookbook version on separate set of nodes and environment-specific attributes.You might have nodes with less memory in your staging environment as in your production environment.By using the MySQL service to consume less memory on staging than on production.
Note:
The Chef server always has an environment called _default,which cannot be edited or deleted.All the nodes go in there if you don't specify any other environment.Make sure you have set.
Be aware that roles are not environment-specific.You may use environment-specific run lists,though.
The node's environment can be queried using the node.chef_environment method inside your cookbooks.
There's more:
If you want your environments to be under version control (you should ),a better way to create a new environment is to create a new ruby file in the environment directory inside your Chef repository:
local@workstation:~/chef-repo $ cd environment
local@workstation:~/chef-repo $ subldev.rb
name "dev"
You should add, commit and push your new environemnt file to Github:
local@workstation:~/chef-repo $ git add environments/dev.rb
local@workstation:~/chef-repo $ git commit -a -m "the dev environment"
local@workstation:~/chef-repo $ git push
Now,you can create the environment on the Chef server from the newly created file using knife:
local@workstation:~/chef-repo $ knife environment from file dev.rb
Created environement dev
There is a way to migrate all the nodes from one environment to another by using knife exec:
local@workstation:~/chef-repo $ knife exec -E 'nodes.transform("chef_environment:_default") { |n|n.chef_environment("dev")
You can limit you search for nodes in a specific environment:
local@workstation:~/chef-repo $ knife search node "chef_environment:dev"
1 item found
No comments:
Post a Comment