Run List and include_recipe in Chef
chef-client
We run chef-client to apply recipe to bring mode into desired state.
We call this process as convergence.
Run List
Where we mentioned the recipes we called Run List.
Run the recipes in a sequence order that we mentioned in Run List.
To apply each recipe from each cookbook (not multiple recipes from same cookbook) at a time by using chef-client.
To run the command be inside the cookbooks.
Note
Condition
One recipe from one cookbook only.
Include_recipe
To call recipe/recipes from another recipe with in the same cookbook.
To run multiple recipes from same cookbook.
Here comes the default recipe into action.
We need to mention recipes inside the default recipe (same cookbook)
we can mention any number of recipes inside the default recipe.
All recipes must be same cookbook.
We can use default recipe to call remaining recipes.
Run the default recipe be inside the cookbooks.
Note
Condition
All recipes must be same cookbook.
Implementation Steps
Run List
- See the list of available cookbook.
- See the recipes inside the two cookbook “ktexperts-apache-cookbook” and “ktexperts-cookbook”.
- Modify the recipe”ktexperts-apache-recipe.rb”.
- Verify syntax of recipe.
- Modify the recipe “ktexperts-recipe.rb.
- Verify syntax of recipe.
- Execute the Recipe/call chef-client based on Run List condition (one recipe from one cookbook).
- Verify content of Apache Web Server.
- Verify user,group and file.
Include_recipe
- To see the list of inside the two cookbook “ktexperts-apache-cookbook”.
- Add ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb.
- Modify the recipe”ktexperts-apache-recipe.rb”.
- Modify the recipe”ktexperts-sample-recipe.rb”.
- Execute the default Recipe/call chef-client.
- Verify file “ohaifile”.
- Verify content of Apache Web Server.
Combine Run List and include_recipe
Execute the multiple recipes from multiple cookbook
Run List
To see the list of available cookbook
[root@ip-172-31-42-243 cookbooks]# ls
ktexperts-apache-cookbook ktexperts-cookbook
To see the recipes inside the two cookbook “ktexperts-apache-cookbook” and “ktexperts-cookbook”
we have 3 recipes from both 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 |
[root@ip-172-31-42-243 cookbooks]# tree . ├── <span style="color: #ff0000;"><strong>ktexperts-apache-cookbook</strong></span> │ ├── CHANGELOG.md │ ├── chefignore │ ├── kitchen.yml │ ├── LICENSE │ ├── metadata.rb │ ├── Policyfile.rb │ ├── README.md │ ├── recipes │ │ ├── <span style="color: #0000ff;"><strong>default.rb</strong></span> │ │ ├── <span style="color: #0000ff;"><strong>ktexperts-apache-recipe.rb</strong></span> │ │ └── <span style="color: #0000ff;"><strong>ktexperts-sample-recipe.rb</strong></span> │ ├── spec │ │ ├── spec_helper.rb │ │ └── unit │ │ └── recipes │ │ ├── default_spec.rb │ │ ├── ktexperts-apache-recipe_spec.rb │ │ └── ktexperts-sample-recipe_spec.rb │ └── test │ └── integration │ └── default │ ├── default_test.rb │ ├── ktexperts-apache-recipe_test.rb │ └── ktexperts-sample-recipe_test.rb └──<strong><span style="color: #ff0000;"> ktexperts-cookbook</span></strong> ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ ├── <strong><span style="color: #0000ff;">default.rb</span></strong> │ ├── <span style="color: #0000ff;"><strong>ktexperts1-recipe.rb</strong></span> │ └── <span style="color: #0000ff;"><strong>ktexperts-recipe.rb</strong></span> ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ ├── default_spec.rb │ ├── ktexperts1-recipe_spec.rb │ └── ktexperts-recipe_spec.rb └── test └── integration └── default ├── default_test.rb ├── ktexperts1-recipe_test.rb └── ktexperts-recipe_test.rb 16 directories, 34 files |
Note
We have to follow the Run List condition,i.e we need to take one one recipe from both cookbook.
Now,I am taking ktexperts-apache-recipe.rb from ktexperts-apache-cookbook and ktexperts-sample-recipe.rb from ktexperts-apache-cookbook.
Modify the recipe”ktexperts-apache-recipe.rb”
Open existing recipe “ktexperts-apache-recipe.rb” and write script to change the content of apache web server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb package 'httpd' do action :install end file '/var/www/html/index.html' do content "Ktexperts is a knowledge sharing platform &&& It will help you to share knowledge &&& It is a very good platform to learn new things" action :create end service 'httpd' do action [ :enable, :start ] end |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb Syntax OK |
Modify the recipe “ktexperts-recipe.rb
Open existing recipe ktexperts-recipe.rb” and write script to create user,group and file
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb user "ram" group "ktexperts-DevOps" file "/ramfile" |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client based on Run List condition (one recipe from one 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 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-apache-cookbook::ktexperts-apache-recipe],recipe[ktexperts-cookbook::ktexperts-recipe]" [2019-12-28T18:56:30+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-apache-cookbook::ktexperts-apache-recipe", "ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 4 resources Recipe: ktexperts-apache-cookbook::ktexperts-apache-recipe * file[/var/www/html/index.html] action create - update content in file /var/www/html/index.html from abf0bd to 2b6ea9 --- /var/www/html/index.html 2019-12-28 18:52:27.973759188 +0000 +++ /var/www/html/.chef-index20191228-14396-1r9ny9v.html 2019-12-28 18:56:32.776737865 +0000 @@ -1,2 +1,4 @@ +Ktexperts is a knowledge sharing platform &&& +It will help you to share knowledge &&& It is a very good platform to learn new things Recipe: ktexperts-cookbook::ktexperts-recipe * linux_user[ram] action create - create user ram * group[ktexperts-DevOps] action create - create group ktexperts-DevOps * file[/ramfile] action create - create new file /ramfile Running handlers: Running handlers complete Chef Infra Client finished, 4/4 resources updated in 01 seconds |
Verify content of Apache Web Server
Copy IPV4 Public IP from Chef-Workstation.
Search PV4 Public IP in browser
we can able to see the content of apache web server.
Verify user,group and file
Verify user “ram”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/passwd ram:x:1005:1008::/home/ram:/bin/bash |
Verify group “ktexperts-DevOps”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/group ktexperts-DevOps:x:1009: |
Verify file “ramfile”
1 2 3 |
[root@ip-172-31-42-243 cookbooks]# ls / bin dev home ktexpertsfile ktexperts-file2 lib64 media opt ramfile robofile run srv tmp var boot etc ktexpertsdir ktexperts-file1 lib local mnt proc rammyfile root sbin sys usr |
Include_recipe
To see the list of inside the two cookbook “ktexperts-apache-cookbook”
we can see 3 recipes inside the cookbook “ktexperts-apache-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 |
[root@ip-172-31-42-243 cookbooks]# tree ktexperts-apache-cookbook/ <strong><span style="color: #ff0000;">ktexperts-apache-cookbook/</span></strong> ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ ├── <span style="color: #3366ff;"><strong>default.rb</strong></span> │ ├── <span style="color: #3366ff;"><strong>ktexperts-apache-recipe.rb</strong></span> │ └── <span style="color: #3366ff;"><strong>ktexperts-sample-recipe.rb</strong></span> ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ ├── default_spec.rb │ ├── ktexperts-apache-recipe_spec.rb │ └── ktexperts-sample-recipe_spec.rb └── test └── integration └── default ├── default_test.rb ├── ktexperts-apache-recipe_test.rb └── ktexperts-sample-recipe_test.rb 7 directories, 17 files |
Note
If we want to run multiple recipes from same cookbook,we have to mention remaining recipes inside the default recipe
All recipes must be same cook only.
Now,I am going to mention ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb of ktexperts-apache-cookbook.
Add ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/default.rb include_recipe "ktexperts-apache-cookbook::ktexperts-apache-recipe" include_recipe "ktexperts-apache-cookbook::ktexperts-sample-recipe" |
Note
:wq! — to quit
Modify the recipe”ktexperts-apache-recipe.rb”
Open existing recipe “ktexperts-apache-recipe.rb” and write script to change the content of apache web server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb #package 'httpd' do # action :install #end file '/var/www/html/index.html' do content "wwww.ktexperts.com" action :create end #service 'httpd' do # action [ :enable, :start ] #end |
Note
:wq! — to quit
Modify the recipe”ktexperts-sample-recipe.rb “
Open existing recipe “ktexperts-sample-recipe.rb ” and write script to update the system information.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb file '/ohaifile' 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 |
Note
:wq! — to quit
Execute the default 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 26 27 28 29 30 31 32 33 34 35 36 37 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-apache-cookbook::default]" [2019-12-29T03:35:11+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-apache-cookbook::default"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 2 resources Recipe: ktexperts-apache-cookbook::ktexperts-apache-recipe * file[/var/www/html/index.html] action create - update content in file /var/www/html/index.html from 11c544 to d1137a --- /var/www/html/index.html 2019-12-29 03:02:33.192987380 +0000 +++ /var/www/html/.chef-index20191229-3964-ux9yky.html 2019-12-29 03:35:12.941206740 +0000 @@ -1,4 +1,2 @@ -Ktexperts is a knowledge sharing platform &&& -It will help you to share knowledge &&& -It is a very good platform to learn new things +wwww.ktexperts.com Recipe: ktexperts-apache-cookbook::ktexperts-sample-recipe * file[/ohaifile] action create - create new file /ohaifile - update content in file /ohaifile from none to 5bf5c3 --- /ohaifile 2019-12-29 03:35:12.989206354 +0000 +++ /.chef-ohaifile20191229-3964-19ue0ad 2019-12-29 03:35:12.989206354 +0000 @@ -1 +1,6 @@ +This is to get Attributes + HOSTNAME: ip-172-31-42-243 + IPADDRESS: 172.31.42.243 + CPU: 2400.057 + MEMORY: 1007272kB - change owner from '' to 'root' - change group from '' to 'root' Running handlers: Running handlers complete Chef Infra Client finished, 2/2 resources updated in 01 seconds |
Verify file “ohaifile”
1 2 3 |
[root@ip-172-31-42-243 cookbooks]# ls / bin dev home ktexpertsfile ktexperts-file2 lib64 media ohaifile proc rammyfile root sbin sys usr boot etc ktexpertsdir ktexperts-file1 lib local mnt opt ramfile robofile run srv tmp var |
To see the content of ohaifile
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# cat /ohaifile This is to get Attributes HOSTNAME: ip-172-31-42-243 IPADDRESS: 172.31.42.243 CPU: 2400.057 |
Verify content of Apache Web Server
Copy IPV4 Public IP from Chef-Workstation.
Search PV4 Public IP in browser
we can able to see the content of apache web server.
Combine Run List and include_recipe
As of now we have seen Run List and include_recipe
Run List —–> one recipe from one cookbook
include_recipe —–> All recipes must be same cookbook.
Combine Run List and include-_recipe ——–> All recipes from from different cookbook.
we can run any number of recipes from any number of cookbook.
Note
Here,I am modifying the recipe.
we can modify if required.
Execute the multiple recipes from multiple cookbook
I didn’t do any changes in the all recipes that’s why it’s showing 0/5 resources.
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-apache-cookbook],recipe[ktexperts-cookbook]" [2019-12-26T17:56:49+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-apache-cookbook", "ktexperts-cookbook"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 4 resources Recipe: ktexperts-apache-cookbook::ktexperts-apache-recipe * yum_package[httpd] action install (up to date) * file[/var/www/html/index.html] action create (up to date) * service[httpd] action enable (up to date) * service[httpd] action start (up to date) Recipe: ktexperts-apache-cookbook::ktexperts-sample-recipe * file[/robofile] action create (up to date) Running handlers: Running handlers complete Chef Infra Client finished, 0/5 resources updated in 03 seconds |
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
I was looking for something to educate my team as part of a new requirement. My college recommended me this site and I found it outstanding.