Refactoring a cookbook refers to the process of breaking down a recipe into smaller parts & storing them separately.
Let's refactor the apache cookbook:
[sahil@cwork recipes]$ cat default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html"
if node["platform"] == "ubuntu"
package_name = "apache2"
service_name = "apache2"
doc_root = "/var/www"
end
package package_name do
action :install
end
service service_name do
action [:start, :enable]
end
#cookbook_file "#{doc_root}/index.html" do
# source "index.html"
#mode "0644"
#end
template "#{doc_root}/index.html" do
source "index.html.erb"
mode "0644"
end
From this cookbook we can take the conditional logic part responsible for identifying the platform & the variable declarations & put them in a separate file cookbooks/apache/attributes/default.rb. We'll modify the if statement to a case statement:
case node["platform"]
when "centos"
default["package_name"] = "httpd"
default["service_name"] = "httpd"
default["doc_root"] = "/var/www/html"
when "ubuntu"
default["package_name"] = "apache2"
default["service_name"] = "apache2"
default["doc_root"] = "/var/www"
end
Introducing dependencies in cookbooks:
We can make cookbooks dependent on other cookbooks such that the dependencies get applied before the main cookbook is run. For example, if I want to introduce a dependency in the apache cookbook I need to edit the cookbooks/apache/metadata.rb file & add the following line:
depends cook_book_name
Note: We can also update the cookbook version by editing this file.
Defining a custom attribute:
We can define a custom attribute for a client node that is not returned by OHAI by default. To do this we need to edit the cookbook_name/attributes/default.rb file & define the custom attribute. For example:
default["company"] = "TEST_COMPANY"
After defining this attribute we can use it in erb files for our recipes.
Roles introduction:
Roles allow you to encapsulate the run lists & attributes required for a server to perform a function as per user expectation. Roles make it easy to configure many nodes identically avoiding repetition.
Creating a role:
We'll create a role called webservers. Create a webservers.rb file in chef-repo/roles path with the following content:
name "webserver"
description "web servers"
run_list "recipe[apache], recipe[testrecipe]"
default_attributes({
"company" => "NEW_COMPANY"
})
Custom attributes defined in roles will override the ones defined in individual cookbooks.
To upload a role type the following command:
sudo knife role from file webservers.rb
To view available roles type:
sudo knife role list
To view details of a role:
sudo knife role show webservers
To add the role to the node's run list, modify the run list from the chef server GUI.
Environments:
An environment includes the attributes necessary for for configuring the infrastructure in that environment. Environments allow use to share cookbooks & roles & isolate resources within a single organization. An interesting feature of environments is that they allow cookbook version constraints to be implemented.
To create an environment:
Under chef-repo/environments directory create a .rb file for the specific environment. For example dev.rb to represent the dev environment. Populate this file with the following content:
name "development"
description "development environment"
cookbook "apache", "=2.0.0"
To view available versions of a cookbook type:
sudo knife cookbook show cookbook_name
To upload an environment to the chef server type:
sudo knife environment from file dev.rb
You can modify the environment attribute of a node from the chef server web interface.
Let's refactor the apache cookbook:
[sahil@cwork recipes]$ cat default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html"
if node["platform"] == "ubuntu"
package_name = "apache2"
service_name = "apache2"
doc_root = "/var/www"
end
package package_name do
action :install
end
service service_name do
action [:start, :enable]
end
#cookbook_file "#{doc_root}/index.html" do
# source "index.html"
#mode "0644"
#end
template "#{doc_root}/index.html" do
source "index.html.erb"
mode "0644"
end
From this cookbook we can take the conditional logic part responsible for identifying the platform & the variable declarations & put them in a separate file cookbooks/apache/attributes/default.rb. We'll modify the if statement to a case statement:
case node["platform"]
when "centos"
default["package_name"] = "httpd"
default["service_name"] = "httpd"
default["doc_root"] = "/var/www/html"
when "ubuntu"
default["package_name"] = "apache2"
default["service_name"] = "apache2"
default["doc_root"] = "/var/www"
end
With this done, we'll need to modify our cookbook as well which now looks like:
package node["package_name"] do
action :install
end
service node["service_name"] do
action [:start, :enable]
end
template "#{node["doc_root"]}/index.html" do
source "index.html.erb"
mode "0644"
end
We can make cookbooks dependent on other cookbooks such that the dependencies get applied before the main cookbook is run. For example, if I want to introduce a dependency in the apache cookbook I need to edit the cookbooks/apache/metadata.rb file & add the following line:
depends cook_book_name
Note: We can also update the cookbook version by editing this file.
Defining a custom attribute:
We can define a custom attribute for a client node that is not returned by OHAI by default. To do this we need to edit the cookbook_name/attributes/default.rb file & define the custom attribute. For example:
default["company"] = "TEST_COMPANY"
After defining this attribute we can use it in erb files for our recipes.
Roles introduction:
Roles allow you to encapsulate the run lists & attributes required for a server to perform a function as per user expectation. Roles make it easy to configure many nodes identically avoiding repetition.
Creating a role:
We'll create a role called webservers. Create a webservers.rb file in chef-repo/roles path with the following content:
name "webserver"
description "web servers"
run_list "recipe[apache], recipe[testrecipe]"
default_attributes({
"company" => "NEW_COMPANY"
})
Custom attributes defined in roles will override the ones defined in individual cookbooks.
To upload a role type the following command:
sudo knife role from file webservers.rb
To view available roles type:
sudo knife role list
To view details of a role:
sudo knife role show webservers
To add the role to the node's run list, modify the run list from the chef server GUI.
Environments:
An environment includes the attributes necessary for for configuring the infrastructure in that environment. Environments allow use to share cookbooks & roles & isolate resources within a single organization. An interesting feature of environments is that they allow cookbook version constraints to be implemented.
To create an environment:
Under chef-repo/environments directory create a .rb file for the specific environment. For example dev.rb to represent the dev environment. Populate this file with the following content:
name "development"
description "development environment"
cookbook "apache", "=2.0.0"
To view available versions of a cookbook type:
sudo knife cookbook show cookbook_name
To upload an environment to the chef server type:
sudo knife environment from file dev.rb
You can modify the environment attribute of a node from the chef server web interface.
No comments:
Post a Comment