SlideShare a Scribd company logo
Write your first cookbook step by step
- Ishant Kumar
Here, we’ll talk about how to write your first cookbook step by step. If
you are curious to know how to setup Chef Environment, you can
always have a look in our slideshare slide Understand Chef.
A cookbook is the fundamental unit of configuration and policy
distribution. Each cookbook defines a scenario, such as everything
needed to install and configure httpd server, and then it contains all of
the components that are required to support that scenario.
Action Performed in order to write a cookbook and apply it to node :
— create a new cookbook and add a recipe to it
— upload your cookbook to the Chef server
— configure your node’s run list
— run chef-client to trigger the configuration process on your target
node.
Note : before doing any knife operation your current directory should
be .chef directory
Create cookbook by passing the command
#knife cookbook create webserver
You can see the contents of webserver cookbook by changing
cookbooks directory like below
So above image shows the directory structure of any typical chef
cookbook.
Write the recipe by editing the file
/cookbooks/webserver/recipes/default.rb
The code written above is nothing but Ruby DSL. The first block installs httpd server , second
block starts the service and third block copies the file (index.html) from cookbook files
directory to /var/www/html/ of web server.
Now lets edit the content of index.html
Now your cookbook is ready to upload .
Run this command in order to upload it to server.
#knife cookbook upload webserver
Test the cookbook by running chef-client on the node which is already
registered or ready to register. If you want to understand how to setup
node, check our previous slideshare slide Understand Chef .
Output
After executing chef-client, we can test the sample site by hitting the
public ip in the browser like below .
[Optional ] We can also bootstrap the node :
If we have any machine with public IP and its credentials
From knife we’ll bootstrap a node (e.g. EC2 Instance) with its public ip,
assign it a Chef Recipe and will run chef-client.
Bootstrap means it will connect to EC2 instance, will install chef-client
, copy validation key & client.rb and run chef-client. As we are passing
recipe name in bootstrap command it will also run the cookbook
recipe on node by putting recipe in its run list.
Command :
knife bootstrap <machine_ip> -x user_name -i <key_path> -r
recipe[recipe_name] –sudo
For Example :
knife bootstrap <machineIP> -x ec2-user -i xyz.pem -r recipe[test] –
sudo
This will install httpd server on machine and we can test the sample
site by hitting the public ip in the browser like below .
Thanks
Looking Forward for your interest in DevOpsJourney.com
http://devopsjourney.com
Email us: mail@devopsjourney.com

More Related Content

PDF
Testable Infrastructure with Chef, Test Kitchen, and Docker
PPTX
Testing for infra code using test-kitchen,docker,chef
PDF
Test Driven Development with Chef
PDF
Test-Driven Infrastructure with Chef
PPTX
CLUG 2014-10 - Cookbook CI with Jenkins
PPTX
London Community Summit 2016 - Fresh New Chef Stuff
PDF
Server Installation and Configuration with Chef
PDF
Chef Automate Workflow Demo
Testable Infrastructure with Chef, Test Kitchen, and Docker
Testing for infra code using test-kitchen,docker,chef
Test Driven Development with Chef
Test-Driven Infrastructure with Chef
CLUG 2014-10 - Cookbook CI with Jenkins
London Community Summit 2016 - Fresh New Chef Stuff
Server Installation and Configuration with Chef
Chef Automate Workflow Demo

What's hot (20)

PPTX
Compliance Automation with Inspec Part 4
PDF
Compliance Automation Workshop
PPTX
Chef introduction
PDF
Automating Infrastructure with Chef
PPTX
Understand Chef
PDF
Compliance as Code
PDF
Infrastructure Automation with Chef
PPTX
Chef Workflow Demo
PPTX
Azure handsonlab
PDF
Introduction to Chef
PDF
Docker
PDF
Learning chef
PDF
Introduction to Chef - April 22 2015
PPTX
Application Automation with Habitat
PDF
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
PDF
Introduction to Chef - Techsuperwomen Summit
PDF
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
KEY
Michelin Starred Cooking with Chef
PPT
Chef, Devops, and You
PDF
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Compliance Automation with Inspec Part 4
Compliance Automation Workshop
Chef introduction
Automating Infrastructure with Chef
Understand Chef
Compliance as Code
Infrastructure Automation with Chef
Chef Workflow Demo
Azure handsonlab
Introduction to Chef
Docker
Learning chef
Introduction to Chef - April 22 2015
Application Automation with Habitat
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
Introduction to Chef - Techsuperwomen Summit
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
Michelin Starred Cooking with Chef
Chef, Devops, and You
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Ad

Similar to How to Write Chef Cookbook (20)

PDF
Cloud Automation with Opscode Chef
PPTX
Cook Infrastructure with chef -- Justeat.IN
PDF
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
PDF
IT Automation with Chef
PDF
Cloud Automation with Opscode Chef
PDF
Cloud Automation with Opscode Chef
PDF
Ignite Talk on Chef
ODP
Introduction to Chef
PDF
Chef Provisioning a Chef Server Cluster - ChefConf 2015
PDF
Cloud Automation with Opscode Chef
PDF
Infrastructure as-code-guide-en-v6 299129
PDF
Automating your infrastructure with Chef
PDF
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
PDF
Chef for beginners module 4
PPTX
Understand AWS OpsWorks - A DevOps Tool from AWS
PPTX
Cooking chef
PDF
Introduction To Continuous Compliance & Remediation
PDF
Nike popup compliance workshop
PPTX
Kickstarter - Chef Opswork
PPTX
DevOps hackathon Session 2: Basics of Chef
Cloud Automation with Opscode Chef
Cook Infrastructure with chef -- Justeat.IN
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
IT Automation with Chef
Cloud Automation with Opscode Chef
Cloud Automation with Opscode Chef
Ignite Talk on Chef
Introduction to Chef
Chef Provisioning a Chef Server Cluster - ChefConf 2015
Cloud Automation with Opscode Chef
Infrastructure as-code-guide-en-v6 299129
Automating your infrastructure with Chef
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
Chef for beginners module 4
Understand AWS OpsWorks - A DevOps Tool from AWS
Cooking chef
Introduction To Continuous Compliance & Remediation
Nike popup compliance workshop
Kickstarter - Chef Opswork
DevOps hackathon Session 2: Basics of Chef
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Monthly Chronicles - July 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
A Presentation on Artificial Intelligence
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Cloud computing and distributed systems.
KodekX | Application Modernization Development
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Monthly Chronicles - July 2025
The AUB Centre for AI in Media Proposal.docx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Reach Out and Touch Someone: Haptics and Empathic Computing
A Presentation on Artificial Intelligence

How to Write Chef Cookbook

  • 1. Write your first cookbook step by step - Ishant Kumar
  • 2. Here, we’ll talk about how to write your first cookbook step by step. If you are curious to know how to setup Chef Environment, you can always have a look in our slideshare slide Understand Chef. A cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure httpd server, and then it contains all of the components that are required to support that scenario.
  • 3. Action Performed in order to write a cookbook and apply it to node : — create a new cookbook and add a recipe to it — upload your cookbook to the Chef server — configure your node’s run list — run chef-client to trigger the configuration process on your target node. Note : before doing any knife operation your current directory should be .chef directory
  • 4. Create cookbook by passing the command #knife cookbook create webserver
  • 5. You can see the contents of webserver cookbook by changing cookbooks directory like below So above image shows the directory structure of any typical chef cookbook.
  • 6. Write the recipe by editing the file /cookbooks/webserver/recipes/default.rb
  • 7. The code written above is nothing but Ruby DSL. The first block installs httpd server , second block starts the service and third block copies the file (index.html) from cookbook files directory to /var/www/html/ of web server.
  • 8. Now lets edit the content of index.html Now your cookbook is ready to upload .
  • 9. Run this command in order to upload it to server. #knife cookbook upload webserver Test the cookbook by running chef-client on the node which is already registered or ready to register. If you want to understand how to setup node, check our previous slideshare slide Understand Chef .
  • 11. After executing chef-client, we can test the sample site by hitting the public ip in the browser like below .
  • 12. [Optional ] We can also bootstrap the node : If we have any machine with public IP and its credentials From knife we’ll bootstrap a node (e.g. EC2 Instance) with its public ip, assign it a Chef Recipe and will run chef-client. Bootstrap means it will connect to EC2 instance, will install chef-client , copy validation key & client.rb and run chef-client. As we are passing recipe name in bootstrap command it will also run the cookbook recipe on node by putting recipe in its run list.
  • 13. Command : knife bootstrap <machine_ip> -x user_name -i <key_path> -r recipe[recipe_name] –sudo For Example : knife bootstrap <machineIP> -x ec2-user -i xyz.pem -r recipe[test] – sudo
  • 14. This will install httpd server on machine and we can test the sample site by hitting the public ip in the browser like below .
  • 15. Thanks Looking Forward for your interest in DevOpsJourney.com http://devopsjourney.com Email us: mail@devopsjourney.com