1.Using chef server
2.Using chef-solo
If you want to manage cookbook deployments in an efficient way ,then it is better to set up a chef server and manage your nodes using the chef server. If you want to run your cookbooks without setting up a chef server, then chef-solo would do the work.
In this tutorial I am going to explain how to write a chef cookbook for apache web server to run a static website and deploy the cookbook on a ubuntu 13.04 node using chef-server and chef solo.
Ubuntu 13.04 server is used throughout this tutorial.
CD in to the .chef folder and issue the following command in the terminal to create a cookbook .
Knife cookbook create apache2
Knife cookbook create apache2 –o c:chefcookbooks
├─attributes
├─definition
├─files
└─default
├─libraries
├─providers
├─recipes
├─resources
└─templates
└─default
The uses of every folder can be found in the opscode docs.
Creating Recipe For Apache2 Installation:
package 'apache2' do
action :install
package 'unzip' do
action :install
end
The above snippet will install unzip functionality in the server, which we will need later to unzip the website files.
cookbook_file '/var/www/site.zip' do
source 'site.zip'
mode '0755'
owner 'root'
group node['apache']['root_group']end
cookbook_file resource creates a site.zip file and copies the contents from the apache2 cookbooks files/default/site.zip directory to /var/www/site.zip in the server. Make the css, images ,html and js in to a zip file and copy it the apache2/files/default directory. Source is the site.zip file in our cookbook. Chef looks for files under the /files/default folder , so you don’t have to mention the source path explicitly.
node[‘apache’][‘root_group’] is the attribute which we mention in apache2/attribute/default.rb file. You have to create a default.rb file in the /attribute folder in your cookbook.
default['apache']['root_group'] = ‘root’
So node[‘apache’][‘root_group’] sets the value ‘root’ in the recipe.
bash "extracting files" do
cwd "/var/www"
code <<-EOH
unzip site.zip
EOH
not_if {File.exists?("/var/www/images")}
end
template "/var/www/index.html" do
source "index.html.erb"
mode '0644'
owner 'root'
group node['apache']['root_group']end
The template resource is used to make changes to the existing files in the server. In ouu use case, we have to replace the index.html file which is already present in the server with our sites index.html. For this we have to create a index.html.erb file under apache2/templates/default/index.html.erb and copy the index contents of your sites index file. /var/www/index.html is the path of the file in the server. Source is the template file in our cookbook. By default chef looks for template files under the default folder, so you don’t have to mention the path explicitly in the source attribute.
Final recipe:
package 'apache2' do
end
package 'unzip' do
end
cookbook_file '/var/www/site.zip' do
source 'site.zip'
mode '0755'
owner 'root'
group node['apache']['root_group']end
bash "extracting files" do
cwd "/var/www"
code <<-EOH
unzip site.zip
EOH
not_if {File.exists?("/var/www/images")}
end
template "/var/www/index.html" do
source "index.html.erb"
mode '0644'
owner 'root'
group node['apache']['root_group']end
Knife cookbook test apache2
Deploying the cookbook using chef server:
Knife cookbook upload apache2
2.Add the cookbook to the runlist of a node using the following command.
Knife node run_list add node_name recipe[‘apache2’]
3.Once the cookbook is added to the node’s runlist , run the chef client in the node to deploy the apache2 cookbook from the chef server.
Chef-client
Deploying the cookbook using chef solo:
1.You don’t need a chef server to deploy cookbooks using chef solo. But you should have the cookbooks in the node which you are running chef solo.
1. Create a solo.rb file and copy the following configurations in the file.
cookbook_path "etc/chef/cookbooks"
log_level :debug
log_location "etc/chef/solo.log"
cookbook_path is the path to the cookbook in the node. It can be anywhere in the server , you have to mention it accordingly in the solo.rb file. Log_level is set to debug so that you can see the debug information while running chef-solo. You can also set the log level to info mode.
We supply the run list to the node using a json file.You can also pass attributes using the json file. Create a file solo.json and copy the following configurations .
{
"run_list": [ "recipe"],
}
Put the solo.rb and solo.json files inside /etc/chef folder. CD in to /etc/chef and run chef solo using the following command.
Chef-solo -c solo.rb –j solo.json (or)
Chef-solo –c /path/to/solo.rb -j /path/to/solo.json