In Lab 4: Chef Basic Commands of Recipes & Cookbooks, participants typically delve into the essential commands and concepts related to creating and managing recipes and cookbooks in Chef. This lab focuses on hands-on experience with commands such as 'chef generate' to create new cookbooks and recipes, 'kitchen converge' to apply configurations to a test environment, and 'kitchen verify' to validate the applied configurations. Participants may also explore writing custom recipes, defining attributes, and structuring cookbooks effectively. The lab aims to deepen participants' understanding of Chef's core components, enabling them to author and manage configurations that define the desired state of their infrastructure. Successful completion of Lab 4 equips participants with the skills necessary to leverage Chef efficiently for automating and maintaining consistent configurations across their infrastructure.
cd test-cookbook
chef generate recipe recipe1
cd ..
vi test-cookbook/recipes/recipe1.rb
(Press i)
package ‘finger’ do
action :install
end
file ‘/myfile2’ do
content “This is my second file”
action :create
owner ‘root’
group ‘root’
end
chef exec ruby -c test-cookbook/recipe/recipe1.rb
We run chef-client to apply recipe to bring node into desired state, we call this process as “Convergence”.
chef-client –zr “ recipe\[test-cookbook::recipe1\]”
ls /
which finger
cd test-cookbook
chef generate recipe recipe2
cd ..
vi test-cookbook/recipes/recipe2.rb
(Press i)
package ‘httpd’ do
action :install
end
file ‘/var/www/html/index.html’ do
content “Hello Dear Students!!!”
action :create
end
service ‘httpd’ do
action \[:enable, :start\]
end
(Press esc)
:wq!
chef exec ruby -c test-cookbook/recipe/recipe2.rb
chef-client –zr “ recipe\[test-cookbook::recipe2\]”
Paste Public IP in the Browser
cd test-cookbook
chef generate recipe recipe3
cd ..
vi test-cookbook/recipes/recipe3.rb
(Press i)
package ‘httpd’ do
action :install
end
file ‘/var/www/html/index.html’ do
content “<h1>Hello Dear Students!!!</h1>
<img src=‘rst.png’>”
action :create
end
remote\_file ‘/var/www/html/rst.png’ do
source “https://rst-bucket-1.s3.amazonaws.com/WhatsApp+Image+2020-03-13+at+22.12.27.jpeg”
end
service ‘httpd’ do
action \[:enable, :start\]
end
(Press esc)
:wq!
chef exec ruby -c test-cookbook/recipe/recipe3.rb
chef-client –zr “ recipe\[test-cookbook::recipe3\]”
Paste Public IP in the Browser
We have a web application to be deployed into 1000 nodes & we need to know some details of each server. Because we need to mention that in configuration file of each node. This information is varied from system to system. These details we call “Attributes” Chef-client tool gathers these Attributes from ohai store and puts in configuration files, instead of hard coding these attributes, we mention as variables
cd test-cookbook
chef generate recipe recipe4
cd ..
vi test-cookbook/recipes/recipe4.rb
(Press i)
file ‘/robofile’ do
content “This is to get Attributes
HOSTNAME: #{node\[‘hostname’\]}
IPADDRESS: #{node\[‘ipaddress’\]}
CPU: #{node\[‘cpu’\]\[‘0’\]\[‘mhz’\]}
MEMORY: #{node\[‘memory’\]\[‘total’\]}”
owner ‘root’
group ‘root’
action :create
end
(Press esc)
:wq!
chef exec ruby -c test-cookbook/recipe/recipe3.rb
chef-client –zr “ recipe\[test-cookbook::recipe3\]”
ls /
cat /robofile
To execute Linux commands
execute “run a script” do
command <<-EOH
mkdir /Panchanandir
touch /Panchananfile
EOH
end
We can create users and groups
user “Ram” do
action :create
end
group “devops” do
action :create
members ‘Ram’
append true
end
To deal with multiple resources at a time
\[‘httpd’,’mariadb-server’,’unzip’,’git’,’vim’\].each do |p|
package p do
action :install
end
end
\[‘Ram’,’Shyam’,’Ajay’,’Vijay’,’Rohan’,’Sohan’\].each do |q|
user q do
action :create
end
end