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
- Create cookbooks.
- Create a new cookbook “ktexperts-cookbook”inside the cookbooks.
- Create a new recipe “ktexperts-recipe” inside the cookbook “ktexperts-cookbook”.
- Go to cookbooks.
- Open the Recipe “ktexperts-recipe.rb” and write script to create file.
- Verify syntax of recipe.
- Execute the recipe Locally/Call chef-client.
- Verify file “ktexperts-file1”.
- Modify recipe “ktexperts-recipe.rb”
- Verify syntax of recipe.
- Execute the recipe Locally/Call chef-client.
- Verify file “ktexperts-file1”.
- Create a new Recipe “ktexperts1-recipe”.
- Create a new Recipe “ktexperts1-recipe” inside cookbook “ktexperts-cookbook”.
- Go to cookbooks.
- Open the Recipe “ktexperts1-recipe.rb” and write script to install package and create a new file.
- Verify syntax of recipe.
- Execute the recipe/call chef-client.
- Verify file “ktexperts-file2”.
- Uninstall ‘tree” Package and remove file “ktexperts-file2”.
- Execute the recipe/call chef-client.
- Verify ‘tree’ package and file “ktexperts-file2”.
Create cookbooks
Create a new directory “cookbooks”
The name of the directory “cookbooks” should be mandatory.
1 2 3 |
[root@ip-172-31-42-243 ec2-user]# mkdir cookbooks [root@ip-172-31-42-243 ec2-user]# ls chef-workstation-0.13.35-1.el7.x86_64.rpm cookbooks |
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”
1 2 3 |
[root@ip-172-31-42-243 ec2-user]# cd cookbooks/ [root@ip-172-31-42-20 cookbooks]# Create a cookbook "ktexperts-cookbook" |
Using below command to create a new cookbook.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
[root@ip-172-31-42-20 cookbooks]# chef generate cookbook ktexperts-cookbook +---------------------------------------------+ Chef License Acceptance Before you can continue, 3 product licenses must be accepted. View the license at https://www.chef.io/end-user-license-agreement/ Licenses that need accepting: * Chef Workstation * Chef Infra Client * Chef InSpec Do you accept the 3 product licenses (yes/no)? > yes Persisting 3 product licenses... ✔ 3 product licenses persisted. +---------------------------------------------+ Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/ctl_chef.html#chef-generate-cookbook for more information. Generating cookbook ktexperts-cookboo - Ensuring correct cookbook content - Committing cookbook files to git Your cookbook is ready. Type `cd ktexperts-cookbook` to enter it. There are several commands you can run to get started locally developing and testing your cookbook. Type `delivery local --help` to see a full list of local testing commands. Why not start by writing an InSpec test? Tests for the default recipe are stored at: test/integration/default/default_test.rb If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb To see the list of files/directories inside the cookbook with tree structure First we have to install 'tree' package then we can see all files and directories in a tree structure. Install "tree" package [root@ip-172-31-42-243 cookbooks]# yumi install tree -y bash: yumi: command not found [root@ip-172-31-42-243 cookbooks]# [root@ip-172-31-42-243 cookbooks]# yum install tree -y Loaded plugins: extras_suggestions, langpacks, priorities, update-motd amzn2-core | 2.4 kB 00:00:00 Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.6.0-10.amzn2.0.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================ Package Arch Version Repository Size ================================================================================================ Installing: tree x86_64 1.6.0-10.amzn2.0.1 amzn2-core 47 k Transaction Summary ================================================================================================ Install 1 Package Total download size: 47 k Installed size: 83 k Downloading packages: tree-1.6.0-10.amzn2.0.1.x86_64.rpm | 47 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : tree-1.6.0-10.amzn2.0.1.x86_64 1/1 Verifying : tree-1.6.0-10.amzn2.0.1.x86_64 1/1 Installed: tree.x86_64 0:1.6.0-10.amzn2.0.1 Complete! |
To see the list of component in cookbook
When you create cookbook by default we will get some components like recipes,files and directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@ip-172-31-42-243 cookbooks]# tree . └── ktexperts-cookbook ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ └── default.rb ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ └── default_spec.rb └── test └── integration └── default └── default_test.rb 8 directories, 11 files |
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
1 2 |
[root@ip-172-31-42-243 cookbooks]# cd ktexperts-cookbook [root@ip-172-31-42-243 ktexperts-cookbook]# |
Run below command to create a new recipe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 ktexperts-cookbook]# chef generate recipe ktexperts-recipe Recipe: code_generator::recipe * directory[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes] action create (up to date) * cookbook_file[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/spec_helper.rb] action create_if_missing (up to date) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts-recipe_spec.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts-recipe_spec.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts-recipe_spec.rb from none to 4d7b2d (diff output suppressed by config) * directory[/home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default] action create (up to date) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts-recipe_test.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts-recipe_test.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts-recipe_test.rb from none to 456d42 (diff output suppressed by config) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts-recipe.rb] action create - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts-recipe.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts-recipe.rb from none to de7871 (diff output suppressed by config) To see the list of files/directories inside the recipe |
To see list of recipes in cookbook “ktexperts-cookbook”
we can see the recipe “ktexperts-recipe.rb” which was created earlier.
1 2 3 4 |
[root@ip-172-31-42-243 ktexperts-cookbook]# tree recipes/ recipes/ ├── default.rb ├── ktexperts-recipe.rb |
Go to cookbooks
1 2 |
[root@ip-172-31-42-243 ktexperts-cookbook]# cd .. [root@ip-172-31-42-243 cookbooks]# |
Open the Recipe “ktexperts-recipe.rb” and write script to create file
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb file 'ktexperts-file1' do content 'Ktexperts is a knowledge sharing platform' action :create end |
Note
:wq! — to quit.
Verify syntax of recipe
You should be present inside the cookbooks.
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the recipe Locally/Call chef-client
We have to run chef-client manually.
-z—>Local Mode
r—->Runlist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@ip-172-31-42-243 cookbooks]# sudo chef-client -zr "recipe[ktexperts-cookbook::ktexperts-recipe]" [2019-12-24T02:45:15+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-cookbook::ktexperts-recipe * file[/ktexperts-file1] action create - create new file /ktexperts-file1 - update content in file /ktexperts-file1 from none to 67bb1d --- /ktexperts-file1 2019-12-24 02:45:17.365179292 +0000 +++ /.chef-ktexperts-file120191224-10049-1am8v6q 2019-12-24 02:45:17.365179292 +0000 @@ -1 +1,2 @@ +Ktexperts is a knowledge sharing platform Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Verify file “ktexperts-file1”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin dev home lib local mnt proc run srv tmp var boot etc ktexperts-file1 lib64 media opt root sbin sys usr To see the content of the file "ktexperts-file1" |
To see the content of file “ktexperts-file1”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cat /ktexperts-file1 Ktexperts is a knowledge sharing platform |
Modify recipe “ktexperts-recipe.rb”
Open existing recipe “ktexperts-recipe.rb” and write script to add some content.
1 2 3 4 5 6 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb file '/ktexperts-file1' do content 'Ktexperts is a knowledge sharing platform It will help you to share knowledge together' action :create end |
Note
:wq! — to quit.
Verify syntax of recipe
You should be present inside the cookbooks.
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the recipe Locally/Call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-cookbook::ktexperts-recipe]" [2019-12-24T02:51:21+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-cookbook::ktexperts-recipe * file[/ktexperts-file1] action create - update content in file /ktexperts-file1 from 67bb1d to 80a6b9 --- /ktexperts-file1 2019-12-24 02:47:29.948762565 +0000 +++ /.chef-ktexperts-file120191224-10308-jdlf5v 2019-12-24 02:51:23.596032729 +0000 @@ -1,2 +1,3 @@ Ktexperts is a knowledge sharing platform + It will help you to share knowledge together Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Verify file “ktexperts-file1”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin dev home lib local mnt proc run srv tmp var boot etc ktexperts-file1 lib64 media opt root sbin sys usr To see the content of the file "ktexperts-file1" |
To see the content of file “ktexperts-file1”
1 2 3 |
[root@ip-172-31-42-243 cookbooks]# cat /ktexperts-file1 Ktexperts is a knowledge sharing platform It will help you to share knowledge together |
Create a new Recipe “ktexperts1-recipe” inside cookbook “ktexperts-cookbook”
Go inside the cookbook and create second recipe
Go inside the cookbook “ktexperts-cookbook”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cd ktexperts-cookbook/ [root@ip-172-31-42-243 ktexperts-cookbook]# |
Run below command to create new recipe “ktexperts1-recipe”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 ktexperts-cookbook]# chef generate recipe ktexperts1-recipe Recipe: code_generator::recipe * directory[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes] action create (up to date) * cookbook_file[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/spec_helper.rb] action create_if_missing (up to date) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts1-recipe_spec.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts1-recipe_spec.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/spec/unit/recipes/ktexperts1-recipe_spec.rb from none to 79188b (diff output suppressed by config) * directory[/home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default] action create (up to date) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts1-recipe_test.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts1-recipe_test.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/test/integration/default/ktexperts1-recipe_test.rb from none to 550679 (diff output suppressed by config) * template[/home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts1-recipe.rb] action create - create new file /home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts1-recipe.rb - update content in file /home/ec2-user/cookbooks/ktexperts-cookbook/recipes/ktexperts1-recipe.rb from none to 5ea820 (diff output suppressed by config) To see the list of files/directories inside the recipe |
To see the list of recipes in cookbook “ktexperts-cookbook”
We can see the recipe “ktexperts1-recipe.rb” which was created.
1 2 3 4 5 |
[root@ip-172-31-42-243 ktexperts-cookbook]# tree recipes/ recipes/ ├── default.rb ├── ktexperts1-recipe.rb └── ktexperts-recipe.rb |
Go to cookbooks
1 2 |
[root@ip-172-31-42-243 ktexperts-cookbook]# cd .. [root@ip-172-31-42-243 cookbooks]# |
Open the Recipe “ktexperts1-recipe.rb” and write script to install package and create a new file
1 2 3 4 5 6 7 8 9 10 11 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts1-recipe.rb package 'tree'do action :install end file '/ktexperts-file2' do content 'This is my website' action :create owner 'root' group 'root' end |
Note
:wq! — to quit.
Verify syntax of recipe
You should be present inside the cookbooks.
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts1-recipe.rb Syntax OK |
Execute the recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-cookbook::ktexperts1-recipe]" [2019-12-24T03:08:20+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts1-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 2 resources Recipe: ktexperts-cookbook::ktexperts1-recipe * yum_package[tree] action install (up to date) * file[/ktexperts-file2] action create - create new file /ktexperts-file2 - update content in file /ktexperts-file2 from none to 17512c --- /ktexperts-file2 2019-12-24 03:08:23.380845061 +0000 +++ /.chef-ktexperts-file220191224-10972-cdmoac 2019-12-24 03:08:23.380845061 +0000 @@ -1 +1,2 @@ +This is my website - change owner from '' to 'root' - change group from '' to 'root' Running handlers: Running handlers complete Chef Infra Client finished, 1/2 resources updated in 02 seconds |
Verify file “ktexperts-file2”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin etc ktexpertsfile lib media proc root srv usr boot home ktexperts-file1 lib64 mnt run sys var dev ktexperts-file2 local opt sbin tmp |
To see the content of file “ktexperts-file2”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cat /ktexperts-file2 This is my website |
Uninstall ‘tree” Package and remove file “ktexperts-file2”
Uninstall ‘tree” Package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
[root@ip-172-31-42-243 cookbooks]# yum remove tree Loaded plugins: extras_suggestions, langpacks, priorities, update-motd Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.6.0-10.amzn2.0.1 will be erased --> Finished Dependency Resolution amzn2-core/2/x86_64 | 2.4 kB 00:00:00 Dependencies Resolved ================================================================================================ Package Arch Version Repository Size ================================================================================================ Removing: tree x86_64 1.6.0-10.amzn2.0.1 @amzn2-core 83 k Transaction Summary ================================================================================================ Remove 1 Package Installed size: 83 k Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : tree-1.6.0-10.amzn2.0.1.x86_64 1/1 Verifying : tree-1.6.0-10.amzn2.0.1.x86_64 1/1 Removed: tree.x86_64 0:1.6.0-10.amzn2.0.1 Complete! |
Remove file “ktexperts-file2”
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# rm -rf /ktexperts-file2 [root@ip-172-31-42-243 cookbooks]# ls / bin dev home lib local mnt proc run srv tmp var boot etc ktexperts-file1 lib64 media opt root sbin sys usr Execute recipe/call chef-client |
Execute the recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-cookbook::ktexperts1-recipe]" [2019-12-24T03:16:10+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts1-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 2 resources Recipe: ktexperts-cookbook::ktexperts1-recipe * yum_package[tree] action install - install version 0:1.6.0-10.amzn2.0.1.x86_64 of package tree * file[/ktexperts-file2] action create - create new file /ktexperts-file2 - update content in file /ktexperts-file2 from none to 17512c --- /ktexperts-file2 2019-12-24 03:16:15.355369597 +0000 +++ /.chef-ktexperts-file220191224-11472-14yraes 2019-12-24 03:16:15.355369597 +0000 @@ -1 +1,2 @@ +This is my website - change owner from '' to 'root' - change group from '' to 'root' Running handlers: Running handlers complete Chef Infra Client finished, 2/2 resources updated in 04 seconds |
Verify ‘tree’ package and file “ktexperts-file2”
Verify ‘tree’ package
1 2 |
[root@ip-172-31-42-243 cookbooks]# which tree /bin/tree |
Verify file “ktexperts-file2”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin etc ktexperts-file2 local opt run sys var boot home lib media proc sbin tmp dev ktexperts-file1 lib64 mnt root srv usr |
To see the content of the file “ktexperts-file2”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cat /ktexperts-file2 This is my website[ |
To verify owner and group of the file “ktexperts-file2”
1 2 |
[root@ip-172-31-42-243 cookbooks]# ls -l /ktexperts-file2 -rw-r--r-- 1 root root 18 Dec 24 03:16 /ktexperts-file2 |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
priya
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.