How to Create Cookbook and Recipe in Chef

Share via:

How to Create Cookbook and Recipe in Chef

Cookbooks

Cookbooks are fundamental working units of Chef, which consists of all the details related to working units, having the capability to modify configuration and the state of any system configured as a node on Chef infrastructure.

Cookbooks can perform multiple tasks.

Cookbooks contain values about the desired state.

Cookbook

Generally we create a cookbook inside the cookbooks directory.

A cookbook typically includes the following basic components:

Recipes
Attributes
Files
Libraries
Custom Resources
Templates
Ohai Plugins
Metadata

Recipe

A file that contains a set of instructions (resources) to be executed.

A recipe must be contained inside a Cookbook.

Implementation Steps 

  1. Create cookbooks.
  2. Create a new cookbook “ktexperts-cookbook”inside the cookbooks.
  3. Create  a new recipe “ktexperts-recipe” inside the cookbook “ktexperts-cookbook”.
  4. Go to cookbooks.
  5. Open the Recipe “ktexperts-recipe.rb” and write script to create file.
  6. Verify syntax of recipe.
  7. Execute the recipe Locally/Call chef-client.
  8. Verify file “ktexperts-file1”.
  9. Modify recipe “ktexperts-recipe.rb”
  10. Verify syntax of recipe.
  11. Execute the recipe Locally/Call chef-client.
  12. Verify file “ktexperts-file1”.
  13. Create a new  Recipe “ktexperts1-recipe”.
  14. Create a new  Recipe “ktexperts1-recipe” inside cookbook “ktexperts-cookbook”.
  15. Go to cookbooks.
  16. Open the  Recipe “ktexperts1-recipe.rb” and write script to install package and create a new file.
  17. Verify syntax of recipe.
  18. Execute the recipe/call chef-client.
  19. Verify file “ktexperts-file2”.
  20. Uninstall ‘tree” Package  and remove file “ktexperts-file2”.
  21. Execute the recipe/call chef-client.
  22. Verify ‘tree’ package and file “ktexperts-file2”.

Create cookbooks

Create a new directory “cookbooks”

The name of the directory “cookbooks” should be mandatory.

Create a new cookbook “ktexperts-cookbook”inside the cookbooks

We should be inside the cookbooks to create cookbook (mandatory).

Go inside the directory “cookbooks” and Create a cookbook.

Go inside the directory “cookbooks”

Using below command to create a new cookbook.

To see the list of component in cookbook

When you create cookbook by default we will get some components like recipes,files and directories.

Create  a new recipe “ktexperts-recipe” inside the cookbook “ktexperts-cookbook”

we should create the recipe inside the cookbook.

Go inside the cookbook then create a recipe

Go inside the cookbook

Run below command to create a new recipe

To see list of recipes in cookbook “ktexperts-cookbook”

we can see the recipe “ktexperts-recipe.rb” which was created earlier.

Go to cookbooks

Open the Recipe “ktexperts-recipe.rb” and write script to create file

Note

:wq! — to quit.

Verify syntax of recipe

You should be present inside the cookbooks.

Execute the recipe Locally/Call chef-client

We have to run chef-client manually.

-z—>Local Mode

r—->Runlist

Verify file “ktexperts-file1”

To see the content of file “ktexperts-file1”

Modify recipe “ktexperts-recipe.rb”
Open existing recipe “ktexperts-recipe.rb” and write script to add some content.

Note

:wq! — to quit.

Verify syntax of recipe

You should be present inside the cookbooks.

Execute the recipe Locally/Call chef-client

Verify file “ktexperts-file1”

To see the content of file “ktexperts-file1”

Create a new  Recipe “ktexperts1-recipe” inside cookbook “ktexperts-cookbook”

Go inside the cookbook and create second recipe

Go inside the cookbook “ktexperts-cookbook”

Run below command to create new recipe “ktexperts1-recipe”

To see the list of recipes in cookbook “ktexperts-cookbook”

We can see the recipe “ktexperts1-recipe.rb” which was created.

Go to cookbooks

Open the  Recipe “ktexperts1-recipe.rb” and write script to install package and create a new file

Note

:wq! — to quit.

Verify syntax of recipe

You should be present inside the cookbooks.

Execute the recipe/call chef-client

Verify file “ktexperts-file2”

To see the content of file “ktexperts-file2”

Uninstall ‘tree” Package  and remove file “ktexperts-file2”

Uninstall ‘tree” Package

Remove file “ktexperts-file2”

Execute the recipe/call chef-client

Verify ‘tree’ package and file “ktexperts-file2”

Verify ‘tree’ package

Verify file “ktexperts-file2”

To see the content of the file “ktexperts-file2”

To verify owner and group of the file “ktexperts-file2”

 

Thank you for giving your valuable time to read the above information.

Follow us on 

Website :  www.ktexperts.com

Facebook Page : KTexperts

Linkedin Page : KT EXPERTS

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading...

2 thoughts on “How to Create Cookbook and Recipe in Chef

  1. Thank you for the great write-up. I was trying to understand DevOps at a glance. Nothing could beat this session. Thank you again for being a great help.

Add Comment