How to create a cookbook and Recipe in CHEF
Version: 21.3.346
Prerequisite: https://bansalkushagra.medium.com/how-chef-works-eb4c7846819a
What’s Inside Cookbook?
A. Chefbook => Like .gitignore
B. Kitchen.yml => For testing Cookbook
C. Metadata.rb => name, version author name etc info of the cookbook.
D. Readme.md => information about usage of cookbook
E. Recipe => where we write code
F. Spec => It used for unit test
G. Test => It is used for integration test
Implementation =>
Step-1:
Create ac EC2 instance of Linux, update the instance and download the chef.
Follow the below command
Ø Yum update –y
Step-2:
.rpm file of chef-workstation will get the download. Install the fill.
Follow the below command =>
Ø yum install chef-workstation-21.4.365–1.el7.x86_64.rpm
Step-3:
To verify the installation must check the version of the chef.
Command:
Ø which chef
Ø chef –v
Step-4:
Create a cookbook directory where all type of cookbook data will be store.
Command:
Ø mkdir cookbooks
Ø cd cookbooks
Let us create a specific cookbook here:
Ø chef generate cookbook <Cookbook_Name>
Ø chef generate cookbook test-cookbook
Step-5:
Let see a tree structure of our system.
The tree is used to recursively list or display the content of a directory in a tree-like format. It outputs the directory paths and files in each sub-directory and a summary of a total number of sub-directories and files.
Command:
Ø yum install tree –y
Ø tree
Step-6:
Creating a Recipe for our cookbook.
Command:
Go to the cookbook directory i.e. test-cookbook and follow the below command.
Command:
Ø cd test-cookbook/
Ø chef generate recipe <recipe_Name>
Ø chef generate recipe test-recipe
Step-7:
Let’s modify a recipe.
Either go to directory i.e cookbooks>test-cookbook>recipes and use the following command
Ø vi test-recipe.rb
OR
Ø vi cookbooks/test-cookbook/recipes/test-recipe.rb
.rb extension is mandatory without it recipe will not be created.
Write the following code in the text editor and exit using the “ESC-:wq” command
Code:
file ‘/myfile’ do
content ‘Welcome to Technical Guftgu’
action :create
end
Step-8:
Let’s execute our recipe. It first calls ohai if there’s any update in the configurations file it will update the ohai otherwise it will show an output “up to date”
Command:
Ø chef-client –zr “recipe[<Cookbook_name>::<Recipe_Name>]”
Ø chef-client –zr “recipe[test-cookbook::test-recipe]”
If we again execute the recipe it will show an output “up to date” because it does not overwrite any configurations.
Step-9:
Let's update our recipe.
Go to the test-cookbook directory
Ø cd cookbooks > test-cookbook > recipes
Ø vi test-recipe.rb
OR,
Go to the home directory
Ø vi cookbooks/test-cookbook/recipes/recipe2.rb
Write the following code
.rb extension is mandatory
Execute the second updated recipe i.e. test-recipe
Ø chef-client –zr “recipe[test-cookbook::test-recipe]”
## Hosting a Website using Chef
Step-1:
Create a directory where all types of cookbook directories will be store.
Ø Mkdir cookbooks
Ø Cd cookbooks
Lets create a cookbook
Ø chef generate cookbook <Cookbook_Name>
Ø chef generate cookbook apache-cookbook
Let’s create a recipe
Ø chef generate recipe <Recipe_name>
Ø chef generate recipe apache-recipe
Write the following code in the recipe
Ø vi recipes/apache-recipe.rb
Code:
package ‘httpd’ do
action :install
end
file ‘/var/www/html/index.html’ do
content ‘Hosting a Website using Chef’
action :create
end
service ‘httpd’ do
action [:enable, :start]
end
Step-2:
Execute the recipe to host a website
Ø chef-client -zr “recipe[apache-cookbook::apache-recipe]”
Output: