How to create a cookbook and Recipe in CHEF

KUSHAGRA BANSAL
Apr 14 · 4 min read

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

Ø Wget https://packages.chef.io/files/stable/chef-workstation/21.4.365/el/7/chef-workstation-21.4.365-1.el7.x86_64.rpm

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:

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

KUSHAGRA BANSAL

Written by

DevOps Engineer

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store