SlideShare a Scribd company logo
Cookbooks
Organizing Recipes
Slide 1 of 57
After completing this module, you should be able to:
Modify a recipe
Use version control
Generate a Chef cookbook
Define a Chef recipe that sets up a web server
Objectives
Slide 2 of 57
Questions You May Have
Slide 3 of 57
Thinking about the workstation recipe, could we do
something like that for a web server?
Questions You May Have
Slide 4 of 57
Thinking about the workstation recipe, could we do
something like that for a web server?
Is there a way to package up recipes you create with a
version number (and maybe a README)?
Questions You May Have
Slide 5 of 57
Thinking about the workstation recipe, could we do
something like that for a web server?
Is there a way to package up recipes you create with a
version number (and maybe a README)?
I think Chef is able to generate something called a
cookbook. Shouldn't we start thinking about some
version control so we don't lose all our hard work?
Questions You May Have
Slide 6 of 57
Slide 7 of 57
Versioning Pros and Cons
Slide 8 of 57
Versioning Pros and Cons
Saving a copy of the original file as another filename.
$cpsetup.rbsetup.rb.bak
Slide 9 of 57
Versioning Pros and Cons
Saving a copy of the original file as another filename.
$cpsetup.rbsetup.rb.bak
or
$cpsetup.rb{,.`date+%Y%m%d%H%M`}
Slide 10 of 57
Versioning Pros and Cons
Saving a copy of the original file as another filename.
$cpsetup.rbsetup.rb.bak
or
$cpsetup.rb{,.`date+%Y%m%d%H%M`}
or
$cpsetup.rb{,.`date+%Y%m%d%H%M-$USER`}
Slide 11 of 57
Git Version Control
git is a distributed revision control system with an emphasis on speed, data
integrity, and support for distributed, non-linear workflows.
We will be using git throughout the rest of this workshop.
Slide 12 of 57
Add the additional policy to setup.rb:
The package named git is installed.
Then apply this recipe with chef-apply.
Lab Exercise: Install git
Slide 13 of 57
Lab Exercise: Adding the git Package
~/setup.rb
package'nano'
package'vim'
package'emacs'
package'tree'
package'git'
file'/etc/motd'do
content'Propertyof...'
end
Slide 14 of 57
Lab Exercise: Re-apply the Setup Recipe
$sudochef-applysetup.rb
Recipe:(chef-applycookbook)::(chef-applyrecipe)
*yum_package[nano]actioninstall(uptodate)
*yum_package[vim]actioninstall(uptodate)
*yum_package[emacs]actioninstall(uptodate)
*yum_package[tree]actioninstall(uptodate)
*yum_package[git]actioninstall
-installversion1.7.1-4.el6_7.1ofpackagegit
*file[/etc/motd]actioncreate(uptodate)
Slide 15 of 57
How are we going to manage this file?
Does it need a README?
Group Exercise: Create a Cookbook
Slide 16 of 57
How are we going to manage this file?
Does it need a README?
Objective:
☐ Use chef (CLI) to generate a cookbook to store our setup
recipe.
☐ Add the workstation cookbook to version control.
Group Exercise: Create a Cookbook
Slide 17 of 57
What is chef?
An executable program that allows you generate cookbooks and cookbook
components.
Slide 18 of 57
What can chef do?
$chef--help
Usage:
chef-h/--help
chef-v/--version
chefcommand[arguments...][options...]
AvailableCommands:
exec Runsthecommandincontextoftheembeddedruby
env PrintsenvironmentvariablesusedbyChefDK
gem Runsthegemcommandincontextoftheembeddedruby
generate Generateanewapp,cookbook,orcomponent
shell-init InitializeyourshelltouseChefDKasyourprimaryruby
install InstallcookbooksfromaPolicyfileandgeneratealockedcookbookset
update UpdatesaPolicyfile.lock.jsonwithlatestrun_listandcookbooks
...
undelete Undoadeletecommand
verify TesttheembeddedChefDKapplications
Slide 19 of 57
Cookbooks
What is a Chef Cookbook?
Slide 20 of 57
A Chef Cookbook is the fundamental unit of
configuration and policy distribution.
Cookbooks
What is a Chef Cookbook?
Slide 21 of 57
A Chef Cookbook is the fundamental unit of
configuration and policy distribution.
Each cookbook defines a scenario, such as everything
needed to install and configure MySQL, and then it
contains all of the components that are required to
support that scenario.
Cookbooks
What is a Chef Cookbook?
Slide 22 of 57
A Chef Cookbook is the fundamental unit of
configuration and policy distribution.
Each cookbook defines a scenario, such as everything
needed to install and configure MySQL, and then it
contains all of the components that are required to
support that scenario.
Read the first three paragraphs here:
http://docs.chef.io/cookbooks.html
Cookbooks
What is a Chef Cookbook?
Slide 23 of 57
What Can chef generate Do?
$chefgenerate--help
Usage:chefgenerateGENERATOR[options]
Availablegenerators:
app Generateanapplicationrepo
cookbook Generateasinglecookbook
recipe Generateanewrecipe
attribute Generateanattributesfile
template Generateafiletemplate
file Generateacookbookfile
lwrp Generatealightweightresource/provider
repo GenerateaChefpolicyrepository
policyfile GenerateaPolicyfileforusewiththeinstall/pushcommands
generator CopyChefDK'sgeneratorcookbooksoyoucancustomizeit
Slide 24 of 57
What Can chef generate cookbook Do?
$chefgeneratecookbook--help
Usage:chefgeneratecookbookNAME[options]
-C,--copyrightCOPYRIGHT Nameofthecopyrightholder-default...
-m,--emailEMAIL Emailaddressoftheauthor-defaults...
-a,--generator-argKEY=VALUE UsetosetarbitraryattributeKEYto...
-I,--licenseLICENSE all_rights,httpd,mit,gplv2,gplv3-...
-gGENERATOR_COOKBOOK_PATH, UseGENERATOR_COOKBOOK_PATHforthe...
--generator-cookbook
Slide 25 of 57
Group Exercise: Let's Create a Cookbook
$cd~
$chefgeneratecookbookworkstation
InstallingCookbookGems:
CompilingCookbooks...
Recipe:code_generator::cookbook
*directory[/home/bmeyer/workstation]actioncreate
-createnewdirectory/home/bmeyer/workstation
*template[/home/bmeyer/workstation/metadata.rb]actioncreate_if_missing
-createnewfile/home/bmeyer/workstation/metadata.rb
-updatecontentinfile/home/bmeyer/workstation/metadata.rbfromnoneto0c09e4
(diffoutputsuppressedbyconfig)
*template[/home/bmeyer/workstation/README.md]actioncreate_if_missing
-createnewfile/home/bmeyer/workstation/README.md
-updatecontentinfile/home/bmeyer/workstation/README.mdfromnoneto590727
(diffoutputsuppressedbyconfig)
...
*execute[initialize-git]actionrun
-executegitinit.
*cookbook_file[/home/bmeyer/workstation/.gitignore]actioncreate
-createnewfile/home/bmeyer/workstation/.gitignore
-updatecontentinfile/home/bmeyer/workstation/.gitignorefromnonetodd37b2
(diffoutputsuppressedbyconfig)
Slide 26 of 57
Group Exercise: The Cookbook Has a README.md file
$treeworkstation
workstation
├──Berksfile
├──chefignore
├──metadata.rb
├──README.md
├──recipes
│ └──default.rb
├──spec
│ ├──spec_helper.rb
...
└──test
└──integration
...
└──helpers
└──serverspec
└──spec_helper.rb
10directories,9files
Slide 27 of 57
The description of the cookbook's features written in
Markdown.
README.md
http://daringfireball.net/projects/markdown/syntax
Slide 28 of 57
Group Exercise: The Cookbook Has Some Metadata
$treeworkstation
workstation
├──Berksfile
├──chefignore
├──metadata.rb
├──README.md
├──recipes
│ └──default.rb
├──spec
│ ├──spec_helper.rb
│ └──unit
│ └──recipes
│ └──default_spec.rb
10directories,9files
Slide 29 of 57
Every cookbook requires a small amount of metadata.
metadata.rb
Slide 30 of 57
Every cookbook requires a small amount of metadata.
Metadata is stored in a file called metadata.rbthat
lives at the top of each cookbook’s directory.
metadata.rb
http://docs.chef.io/config_rb_metadata.html
Slide 31 of 57
Group Exercise: Let's Take a Look at the Metadata
$catworkstation/metadata.rb
name 'workstation'
maintainer 'TheAuthors'
maintainer_email'you@example.com'
license 'all_rights'
description 'Installs/Configuresworkstation'
long_description'Installs/Configuresworkstation'
version '0.1.0'
Slide 32 of 57
Group Exercise: The Cookbook Has a Folder for Recipes
$treeworkstation
workstation
├──Berksfile
├──chefignore
├──metadata.rb
├──README.md
├──recipes
│ └──default.rb
├──spec
│ ├──spec_helper.rb
│ └──unit
│ └──recipes
│ └──default_spec.rb
10directories,9files
Slide 33 of 57
Group Exercise: The Cookbook Has a Default Recipe
$catworkstation/recipes/default.rb
#CookbookName::workstation
#Recipe::default
#
#Copyright(c)2015TheAuthors,AllRightsReserved.
Slide 34 of 57
Group Exercise: Move our setup.rb Recipe into the Cookbook
$mv-vsetup.rbworkstation/recipes/
`setup.rb'->`workstation/recipes/setup.rb'
Slide 35 of 57
This is a probably a good point to capture the initial state
of our cookbook.
Objective:
☑ Use chef (CLI) to generate a cookbook to store our setup
recipe.
☐ Add the workstation cookbook to version control.
Group Exercise: Version Control
Slide 36 of 57
Group Exercise: Move into the Cookbook Directory
$cdworkstation
Slide 37 of 57
Group Exercise: Initialize the Directory as a git Repository
$gitinit
ReinitializedexistingGitrepositoryin/home/chef/workstation/.git/
Slide 38 of 57
Group Exercise: Use git add to Stage Files to be Committed
$gitadd.
Slide 39 of 57
The staging area has a file, generally contained in
your Git directory, that stores information about what
will go into your next commit.
It’s sometimes referred to as the “index”, but it’s also
common to refer to it as the staging area.
Staging Area
http://git-scm.com/book/en/v2/Getting-Started-Git-Basics
Slide 40 of 57
Group Exercise: Use git status to View the Staged Files
$gitstatus
#Onbranchmaster
#
#Initialcommit
#
#Changestobecommitted:
# (use"gitrm--cached<file>..."tounstage)
#
# newfile: .gitignore
# newfile: .kitchen.yml
# newfile: Berksfile
# newfile: README.md
# newfile: chefignore
# newfile: metadata.rb
# newfile: recipes/default.rb
# newfile: recipes/setup.rb
# newfile: spec/spec_helper.rb
# newfile: spec/unit/recipes/default_spec.rb
# newfile: test/integration/default/serverspec/default_spec.rb
# newfile: test/integration/helpers/serverspec/spec_helper.rb
#
Slide 41 of 57
Group Exercise: Use git commit to Save the Staged Changes
$gitcommit-m"Initialworkstationcookbook"
[master(root-commit)ae6968b]Initialworkstationcookbook
Committer:ChefDKUser<chef@ip-172-31-11-224.ec2.internal>
Yournameandemailaddresswereconfiguredautomaticallybased
onyourusernameandhostname.Pleasecheckthattheyareaccurate.
Youcansuppressthismessagebysettingthemexplicitly:
gitconfig--globaluser.name"YourName"
gitconfig--globaluser.emailyou@example.com
Iftheidentityusedforthiscommitiswrong,youcanfixitwith:
gitcommit--amend--author='YourName<you@example.com>'
12fileschanged,207insertions(+),0deletions(-)
createmode100644.gitignore
createmode100644.kitchen.yml
createmode100644Berksfile
createmode100644README.md
createmode100644chefignore
createmode100644metadata.rb
createmode100644recipes/default.rb
createmode100644recipes/setup.rb
...
Slide 42 of 57
If you use git versioning you should ultimately push
the local git repository to a shared remote git
repository.
In this way others could collaborate with you from a
centralized location.
Git Version Control
Slide 43 of 57
Group Exercise: Move out of the Workstation Cookbook
$cd~
Slide 44 of 57
Use chef generate to create a cookbook named apache.
Write and apply a recipe named server.rbwith the
policy:
The package named httpd is installed.
The file named /var/www/html/index.htmlis
created with the content <h1>Hello,world!</h1>
The service named httpd is started and enabled.
Apply the recipe with chef-apply.
Verify the site is available by running curl
http://localhost.
Lab Exercise: Setting up a Web Server
Slide 45 of 57
Lab Exercise: Create a Cookbook
$chefgeneratecookbookapache
InstallingCookbookGems:
CompilingCookbooks...
Recipe:code_generator::cookbook
*directory[/home/bmeyer/apache]actioncreate
-createnewdirectory/home/bmeyer/apache
*template[/home/bmeyer/apache/metadata.rb]actioncreate_if_missing
-createnewfile/home/bmeyer/apache/metadata.rb
-updatecontentinfile/home/bmeyer/apache/metadata.rbfromnoneto37ed5f
(diffoutputsuppressedbyconfig)
*template[/home/bmeyer/apache/README.md]actioncreate_if_missing
-createnewfile/home/bmeyer/apache/README.md
-updatecontentinfile/home/bmeyer/apache/README.mdfromnoneto5c3d3a
(diffoutputsuppressedbyconfig)
...
*cookbook_file[/home/bmeyer/apache/.gitignore]actioncreate
-createnewfile/home/bmeyer/apache/.gitignore
-updatecontentinfile/home/bmeyer/apache/.gitignorefromnonetodd37b2
(diffoutputsuppressedbyconfig)
Slide 46 of 57
Lab Exercise: Create a Cookbook
$chefgeneraterecipeapacheserver
InstallingCookbookGems:
CompilingCookbooks...
Recipe:code_generator::recipe
*directory[./apache/spec/unit/recipes]actioncreate(uptodate)
*cookbook_file[./apache/spec/spec_helper.rb]actioncreate_if_missing(uptodate)
*template[./apache/spec/unit/recipes/server_spec.rb]actioncreate_if_missing
-createnewfile./apache/spec/unit/recipes/server_spec.rb
-updatecontentinfile./apache/spec/unit/recipes/server_spec.rbfromnonetoa43970
(diffoutputsuppressedbyconfig)
*template[./apache/recipes/server.rb]actioncreate
-createnewfile./apache/recipes/server.rb
-updatecontentinfile./apache/recipes/server.rbfromnoneto3d6b92
(diffoutputsuppressedbyconfig)
Slide 47 of 57
Lab Exercise: Create the Server Recipe
~/apache/recipes/server.rb
package'httpd'
file'/var/www/html/index.html'do
content'<h1>Hello,world!</h1>'
end
service'httpd'do
action[:enable,:start]
end
Slide 48 of 57
Lab Exercise: Apply the Server Recipe
$sudochef-applyapache/recipes/server.rb
Recipe:(chef-applycookbook)::(chef-applyrecipe)
*yum_package[httpd]actioninstall
-installversion2.4.6-40.el7.centosofpackagehttpd
*file[/var/www/html/index.html]actioncreate
-createnewfile/var/www/html/index.html
-updatecontentinfile/var/www/html/index.htmlfromnoneto17d291
---/var/www/html/index.html 2016-04-0409:50:59.758483838-0500
+++/var/www/html/.chef-index.html20160404-21732-1o44k9g 2016-04-0409:50:59.758483838-0500
@@-1+1,2@@
+<h1>Hello,world!</h1>
*service[httpd]actionenable
-enableserviceservice[httpd]
*service[httpd]actionstart
-startserviceservice[httpd]
Slide 49 of 57
Lab Exercise: Verify Website is Available
$curllocalhost
<h1>Hello,world!</h1>
Slide 50 of 57
Group Exercise: Commit Your Work
$cdapache
$gitinit
$gitadd.
$gitcommit-m"InitialApacheCookbook"
Slide 51 of 57
What file would you read first when examining a
cookbook?
Discussion
Slide 52 of 57
What file would you read first when examining a
cookbook?
What other recipes might you include in the apache or
workstation cookbook?
Discussion
Slide 53 of 57
What file would you read first when examining a
cookbook?
What other recipes might you include in the apache or
workstation cookbook?
Can resources accept multiple actions?
Discussion
Slide 54 of 57
What file would you read first when examining a
cookbook?
What other recipes might you include in the apache or
workstation cookbook?
Can resources accept multiple actions?
How often would you commit changes with version
control?
Discussion
Slide 55 of 57
Cookbooks
Versions
Version control
Q&A
What questions can we answer for you?
Slide 56 of 57
On to Module 4
Slide 57 of 57

More Related Content

PDF
Chef for beginners module 4
PDF
Chef for beginners module 2
PDF
Chef for beginners module 1
PDF
Chef for beginners module 5
PDF
Docker Docker Docker Chef
PDF
Test kitchen 1.0 - Fletcher Nichol
PDF
Chef basics - write infrastructure as code
PPTX
Cooking chef
Chef for beginners module 4
Chef for beginners module 2
Chef for beginners module 1
Chef for beginners module 5
Docker Docker Docker Chef
Test kitchen 1.0 - Fletcher Nichol
Chef basics - write infrastructure as code
Cooking chef

What's hot (20)

PDF
Using Test Kitchen for testing Chef cookbooks
PDF
Making TDD [Somewhat] Bearable on OpenStack
PDF
Hand Crafted Artisanal Chef Resources
PDF
Compliance Automation Workshop
ODP
Chef training Day4
ODP
Chef training Day5
ODP
Chef training - Day3
PPTX
What Makes a Good Chef Cookbook? (May 2014 Edition)
PDF
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
ODP
Chef training - Day2
PDF
Cookbook Reusability @ Chef Community summit 2014
PDF
IT Automation with Chef
PDF
Testable Infrastructure with Chef, Test Kitchen, and Docker
PPTX
Vagrant introduction for Developers
PDF
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
PDF
Environments - Fundamentals Webinar Series Week 5
ZIP
Drupal Deployment
PDF
Cloud Automation with Opscode Chef
PDF
Automating your workflow with Gulp.js
PDF
Test Driven Development with Chef
Using Test Kitchen for testing Chef cookbooks
Making TDD [Somewhat] Bearable on OpenStack
Hand Crafted Artisanal Chef Resources
Compliance Automation Workshop
Chef training Day4
Chef training Day5
Chef training - Day3
What Makes a Good Chef Cookbook? (May 2014 Edition)
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Chef training - Day2
Cookbook Reusability @ Chef Community summit 2014
IT Automation with Chef
Testable Infrastructure with Chef, Test Kitchen, and Docker
Vagrant introduction for Developers
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
Environments - Fundamentals Webinar Series Week 5
Drupal Deployment
Cloud Automation with Opscode Chef
Automating your workflow with Gulp.js
Test Driven Development with Chef
Ad

Viewers also liked (10)

PDF
Chef Fundamentals Training Series Module 6: Roles, Environments, Community Co...
PDF
Git and Github
PDF
Introduction to git
PDF
Git Tutorial 教學
PDF
Getting Git Right
PDF
[NDC16] Effective Git
PPTX
DevOps 101
PPSX
PDF
Git 101: Git and GitHub for Beginners
PPTX
Introducing DevOps
Chef Fundamentals Training Series Module 6: Roles, Environments, Community Co...
Git and Github
Introduction to git
Git Tutorial 教學
Getting Git Right
[NDC16] Effective Git
DevOps 101
Git 101: Git and GitHub for Beginners
Introducing DevOps
Ad

Similar to Chef for beginners module 3 (20)

PDF
Testing Your Automation Code (Vagrant Version)
PDF
Testing your-automation-code (vagrant version) v0.2
PDF
Introduction to Chef - April 22 2015
PDF
Testing Your Automation Code (Docker Version)
PDF
OSDC 2016 - Kaiten Zushi - Chef at Goodgame Studios by Jan Ulferts
PDF
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
PDF
Introduction to Cooking with Chef
ODP
Chef, Vagrant, and VirtualBox
PDF
Chef - industrialize and automate your infrastructure
PDF
Chef, Vagrant and Friends
PDF
How to become a chef
PDF
How to Become a Сhef
PDF
PPTX
Chef Jumpstart
PPTX
Chef Tutorial for DEVOPS Newbies
PPTX
Chef at EIS
PDF
Cooking an Omelette with Chef
PDF
Building Better Chef Community Cookbooks
PDF
Package Management and Chef - ChefConf 2015
PDF
Chef Conf 2015: Package Management & Chef
Testing Your Automation Code (Vagrant Version)
Testing your-automation-code (vagrant version) v0.2
Introduction to Chef - April 22 2015
Testing Your Automation Code (Docker Version)
OSDC 2016 - Kaiten Zushi - Chef at Goodgame Studios by Jan Ulferts
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
Introduction to Cooking with Chef
Chef, Vagrant, and VirtualBox
Chef - industrialize and automate your infrastructure
Chef, Vagrant and Friends
How to become a chef
How to Become a Сhef
Chef Jumpstart
Chef Tutorial for DEVOPS Newbies
Chef at EIS
Cooking an Omelette with Chef
Building Better Chef Community Cookbooks
Package Management and Chef - ChefConf 2015
Chef Conf 2015: Package Management & Chef

More from Chef (20)

PPTX
Habitat Managed Chef
PPTX
Automation, Audits, and Apps Tour
PPTX
Automation, Audits, and Apps Tour
PPTX
London Community Summit 2016 - Adopting Chef Compliance
PPTX
Learning from Configuration Management
PPTX
London Community Summit 2016 - Fresh New Chef Stuff
PPTX
London Community Summit - Chef at SkyBet
PPTX
London Community Summit - From Contribution to Authorship
PPTX
London Community Summit 2016 - Chef Automate
PPTX
London Community Summit 2016 - Community Update
PPTX
London Community Summit 2016 - Habitat
PPTX
Compliance Automation with Inspec Part 4
PPTX
Compliance Automation with Inspec Part 3
PPTX
Compliance Automation with Inspec Part 2
PPTX
Compliance Automation with Inspec Part 1
PPTX
Application Automation with Habitat
PPTX
Achieving DevOps Success with Chef Automate
PDF
Nike pop up habitat
PDF
Nike popup compliance workshop
PDF
Chef Automate Workflow Demo
Habitat Managed Chef
Automation, Audits, and Apps Tour
Automation, Audits, and Apps Tour
London Community Summit 2016 - Adopting Chef Compliance
Learning from Configuration Management
London Community Summit 2016 - Fresh New Chef Stuff
London Community Summit - Chef at SkyBet
London Community Summit - From Contribution to Authorship
London Community Summit 2016 - Chef Automate
London Community Summit 2016 - Community Update
London Community Summit 2016 - Habitat
Compliance Automation with Inspec Part 4
Compliance Automation with Inspec Part 3
Compliance Automation with Inspec Part 2
Compliance Automation with Inspec Part 1
Application Automation with Habitat
Achieving DevOps Success with Chef Automate
Nike pop up habitat
Nike popup compliance workshop
Chef Automate Workflow Demo

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
System and Network Administration Chapter 2
CHAPTER 2 - PM Management and IT Context
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How Creative Agencies Leverage Project Management Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
ISO 45001 Occupational Health and Safety Management System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
System and Network Administration Chapter 2

Chef for beginners module 3

  • 2. After completing this module, you should be able to: Modify a recipe Use version control Generate a Chef cookbook Define a Chef recipe that sets up a web server Objectives Slide 2 of 57
  • 3. Questions You May Have Slide 3 of 57
  • 4. Thinking about the workstation recipe, could we do something like that for a web server? Questions You May Have Slide 4 of 57
  • 5. Thinking about the workstation recipe, could we do something like that for a web server? Is there a way to package up recipes you create with a version number (and maybe a README)? Questions You May Have Slide 5 of 57
  • 6. Thinking about the workstation recipe, could we do something like that for a web server? Is there a way to package up recipes you create with a version number (and maybe a README)? I think Chef is able to generate something called a cookbook. Shouldn't we start thinking about some version control so we don't lose all our hard work? Questions You May Have Slide 6 of 57
  • 8. Versioning Pros and Cons Slide 8 of 57
  • 9. Versioning Pros and Cons Saving a copy of the original file as another filename. $cpsetup.rbsetup.rb.bak Slide 9 of 57
  • 10. Versioning Pros and Cons Saving a copy of the original file as another filename. $cpsetup.rbsetup.rb.bak or $cpsetup.rb{,.`date+%Y%m%d%H%M`} Slide 10 of 57
  • 11. Versioning Pros and Cons Saving a copy of the original file as another filename. $cpsetup.rbsetup.rb.bak or $cpsetup.rb{,.`date+%Y%m%d%H%M`} or $cpsetup.rb{,.`date+%Y%m%d%H%M-$USER`} Slide 11 of 57
  • 12. Git Version Control git is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows. We will be using git throughout the rest of this workshop. Slide 12 of 57
  • 13. Add the additional policy to setup.rb: The package named git is installed. Then apply this recipe with chef-apply. Lab Exercise: Install git Slide 13 of 57
  • 14. Lab Exercise: Adding the git Package ~/setup.rb package'nano' package'vim' package'emacs' package'tree' package'git' file'/etc/motd'do content'Propertyof...' end Slide 14 of 57
  • 15. Lab Exercise: Re-apply the Setup Recipe $sudochef-applysetup.rb Recipe:(chef-applycookbook)::(chef-applyrecipe) *yum_package[nano]actioninstall(uptodate) *yum_package[vim]actioninstall(uptodate) *yum_package[emacs]actioninstall(uptodate) *yum_package[tree]actioninstall(uptodate) *yum_package[git]actioninstall -installversion1.7.1-4.el6_7.1ofpackagegit *file[/etc/motd]actioncreate(uptodate) Slide 15 of 57
  • 16. How are we going to manage this file? Does it need a README? Group Exercise: Create a Cookbook Slide 16 of 57
  • 17. How are we going to manage this file? Does it need a README? Objective: ☐ Use chef (CLI) to generate a cookbook to store our setup recipe. ☐ Add the workstation cookbook to version control. Group Exercise: Create a Cookbook Slide 17 of 57
  • 18. What is chef? An executable program that allows you generate cookbooks and cookbook components. Slide 18 of 57
  • 19. What can chef do? $chef--help Usage: chef-h/--help chef-v/--version chefcommand[arguments...][options...] AvailableCommands: exec Runsthecommandincontextoftheembeddedruby env PrintsenvironmentvariablesusedbyChefDK gem Runsthegemcommandincontextoftheembeddedruby generate Generateanewapp,cookbook,orcomponent shell-init InitializeyourshelltouseChefDKasyourprimaryruby install InstallcookbooksfromaPolicyfileandgeneratealockedcookbookset update UpdatesaPolicyfile.lock.jsonwithlatestrun_listandcookbooks ... undelete Undoadeletecommand verify TesttheembeddedChefDKapplications Slide 19 of 57
  • 20. Cookbooks What is a Chef Cookbook? Slide 20 of 57
  • 21. A Chef Cookbook is the fundamental unit of configuration and policy distribution. Cookbooks What is a Chef Cookbook? Slide 21 of 57
  • 22. A Chef Cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure MySQL, and then it contains all of the components that are required to support that scenario. Cookbooks What is a Chef Cookbook? Slide 22 of 57
  • 23. A Chef Cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure MySQL, and then it contains all of the components that are required to support that scenario. Read the first three paragraphs here: http://docs.chef.io/cookbooks.html Cookbooks What is a Chef Cookbook? Slide 23 of 57
  • 24. What Can chef generate Do? $chefgenerate--help Usage:chefgenerateGENERATOR[options] Availablegenerators: app Generateanapplicationrepo cookbook Generateasinglecookbook recipe Generateanewrecipe attribute Generateanattributesfile template Generateafiletemplate file Generateacookbookfile lwrp Generatealightweightresource/provider repo GenerateaChefpolicyrepository policyfile GenerateaPolicyfileforusewiththeinstall/pushcommands generator CopyChefDK'sgeneratorcookbooksoyoucancustomizeit Slide 24 of 57
  • 25. What Can chef generate cookbook Do? $chefgeneratecookbook--help Usage:chefgeneratecookbookNAME[options] -C,--copyrightCOPYRIGHT Nameofthecopyrightholder-default... -m,--emailEMAIL Emailaddressoftheauthor-defaults... -a,--generator-argKEY=VALUE UsetosetarbitraryattributeKEYto... -I,--licenseLICENSE all_rights,httpd,mit,gplv2,gplv3-... -gGENERATOR_COOKBOOK_PATH, UseGENERATOR_COOKBOOK_PATHforthe... --generator-cookbook Slide 25 of 57
  • 26. Group Exercise: Let's Create a Cookbook $cd~ $chefgeneratecookbookworkstation InstallingCookbookGems: CompilingCookbooks... Recipe:code_generator::cookbook *directory[/home/bmeyer/workstation]actioncreate -createnewdirectory/home/bmeyer/workstation *template[/home/bmeyer/workstation/metadata.rb]actioncreate_if_missing -createnewfile/home/bmeyer/workstation/metadata.rb -updatecontentinfile/home/bmeyer/workstation/metadata.rbfromnoneto0c09e4 (diffoutputsuppressedbyconfig) *template[/home/bmeyer/workstation/README.md]actioncreate_if_missing -createnewfile/home/bmeyer/workstation/README.md -updatecontentinfile/home/bmeyer/workstation/README.mdfromnoneto590727 (diffoutputsuppressedbyconfig) ... *execute[initialize-git]actionrun -executegitinit. *cookbook_file[/home/bmeyer/workstation/.gitignore]actioncreate -createnewfile/home/bmeyer/workstation/.gitignore -updatecontentinfile/home/bmeyer/workstation/.gitignorefromnonetodd37b2 (diffoutputsuppressedbyconfig) Slide 26 of 57
  • 27. Group Exercise: The Cookbook Has a README.md file $treeworkstation workstation ├──Berksfile ├──chefignore ├──metadata.rb ├──README.md ├──recipes │ └──default.rb ├──spec │ ├──spec_helper.rb ... └──test └──integration ... └──helpers └──serverspec └──spec_helper.rb 10directories,9files Slide 27 of 57
  • 28. The description of the cookbook's features written in Markdown. README.md http://daringfireball.net/projects/markdown/syntax Slide 28 of 57
  • 29. Group Exercise: The Cookbook Has Some Metadata $treeworkstation workstation ├──Berksfile ├──chefignore ├──metadata.rb ├──README.md ├──recipes │ └──default.rb ├──spec │ ├──spec_helper.rb │ └──unit │ └──recipes │ └──default_spec.rb 10directories,9files Slide 29 of 57
  • 30. Every cookbook requires a small amount of metadata. metadata.rb Slide 30 of 57
  • 31. Every cookbook requires a small amount of metadata. Metadata is stored in a file called metadata.rbthat lives at the top of each cookbook’s directory. metadata.rb http://docs.chef.io/config_rb_metadata.html Slide 31 of 57
  • 32. Group Exercise: Let's Take a Look at the Metadata $catworkstation/metadata.rb name 'workstation' maintainer 'TheAuthors' maintainer_email'you@example.com' license 'all_rights' description 'Installs/Configuresworkstation' long_description'Installs/Configuresworkstation' version '0.1.0' Slide 32 of 57
  • 33. Group Exercise: The Cookbook Has a Folder for Recipes $treeworkstation workstation ├──Berksfile ├──chefignore ├──metadata.rb ├──README.md ├──recipes │ └──default.rb ├──spec │ ├──spec_helper.rb │ └──unit │ └──recipes │ └──default_spec.rb 10directories,9files Slide 33 of 57
  • 34. Group Exercise: The Cookbook Has a Default Recipe $catworkstation/recipes/default.rb #CookbookName::workstation #Recipe::default # #Copyright(c)2015TheAuthors,AllRightsReserved. Slide 34 of 57
  • 35. Group Exercise: Move our setup.rb Recipe into the Cookbook $mv-vsetup.rbworkstation/recipes/ `setup.rb'->`workstation/recipes/setup.rb' Slide 35 of 57
  • 36. This is a probably a good point to capture the initial state of our cookbook. Objective: ☑ Use chef (CLI) to generate a cookbook to store our setup recipe. ☐ Add the workstation cookbook to version control. Group Exercise: Version Control Slide 36 of 57
  • 37. Group Exercise: Move into the Cookbook Directory $cdworkstation Slide 37 of 57
  • 38. Group Exercise: Initialize the Directory as a git Repository $gitinit ReinitializedexistingGitrepositoryin/home/chef/workstation/.git/ Slide 38 of 57
  • 39. Group Exercise: Use git add to Stage Files to be Committed $gitadd. Slide 39 of 57
  • 40. The staging area has a file, generally contained in your Git directory, that stores information about what will go into your next commit. It’s sometimes referred to as the “index”, but it’s also common to refer to it as the staging area. Staging Area http://git-scm.com/book/en/v2/Getting-Started-Git-Basics Slide 40 of 57
  • 41. Group Exercise: Use git status to View the Staged Files $gitstatus #Onbranchmaster # #Initialcommit # #Changestobecommitted: # (use"gitrm--cached<file>..."tounstage) # # newfile: .gitignore # newfile: .kitchen.yml # newfile: Berksfile # newfile: README.md # newfile: chefignore # newfile: metadata.rb # newfile: recipes/default.rb # newfile: recipes/setup.rb # newfile: spec/spec_helper.rb # newfile: spec/unit/recipes/default_spec.rb # newfile: test/integration/default/serverspec/default_spec.rb # newfile: test/integration/helpers/serverspec/spec_helper.rb # Slide 41 of 57
  • 42. Group Exercise: Use git commit to Save the Staged Changes $gitcommit-m"Initialworkstationcookbook" [master(root-commit)ae6968b]Initialworkstationcookbook Committer:ChefDKUser<chef@ip-172-31-11-224.ec2.internal> Yournameandemailaddresswereconfiguredautomaticallybased onyourusernameandhostname.Pleasecheckthattheyareaccurate. Youcansuppressthismessagebysettingthemexplicitly: gitconfig--globaluser.name"YourName" gitconfig--globaluser.emailyou@example.com Iftheidentityusedforthiscommitiswrong,youcanfixitwith: gitcommit--amend--author='YourName<you@example.com>' 12fileschanged,207insertions(+),0deletions(-) createmode100644.gitignore createmode100644.kitchen.yml createmode100644Berksfile createmode100644README.md createmode100644chefignore createmode100644metadata.rb createmode100644recipes/default.rb createmode100644recipes/setup.rb ... Slide 42 of 57
  • 43. If you use git versioning you should ultimately push the local git repository to a shared remote git repository. In this way others could collaborate with you from a centralized location. Git Version Control Slide 43 of 57
  • 44. Group Exercise: Move out of the Workstation Cookbook $cd~ Slide 44 of 57
  • 45. Use chef generate to create a cookbook named apache. Write and apply a recipe named server.rbwith the policy: The package named httpd is installed. The file named /var/www/html/index.htmlis created with the content <h1>Hello,world!</h1> The service named httpd is started and enabled. Apply the recipe with chef-apply. Verify the site is available by running curl http://localhost. Lab Exercise: Setting up a Web Server Slide 45 of 57
  • 46. Lab Exercise: Create a Cookbook $chefgeneratecookbookapache InstallingCookbookGems: CompilingCookbooks... Recipe:code_generator::cookbook *directory[/home/bmeyer/apache]actioncreate -createnewdirectory/home/bmeyer/apache *template[/home/bmeyer/apache/metadata.rb]actioncreate_if_missing -createnewfile/home/bmeyer/apache/metadata.rb -updatecontentinfile/home/bmeyer/apache/metadata.rbfromnoneto37ed5f (diffoutputsuppressedbyconfig) *template[/home/bmeyer/apache/README.md]actioncreate_if_missing -createnewfile/home/bmeyer/apache/README.md -updatecontentinfile/home/bmeyer/apache/README.mdfromnoneto5c3d3a (diffoutputsuppressedbyconfig) ... *cookbook_file[/home/bmeyer/apache/.gitignore]actioncreate -createnewfile/home/bmeyer/apache/.gitignore -updatecontentinfile/home/bmeyer/apache/.gitignorefromnonetodd37b2 (diffoutputsuppressedbyconfig) Slide 46 of 57
  • 47. Lab Exercise: Create a Cookbook $chefgeneraterecipeapacheserver InstallingCookbookGems: CompilingCookbooks... Recipe:code_generator::recipe *directory[./apache/spec/unit/recipes]actioncreate(uptodate) *cookbook_file[./apache/spec/spec_helper.rb]actioncreate_if_missing(uptodate) *template[./apache/spec/unit/recipes/server_spec.rb]actioncreate_if_missing -createnewfile./apache/spec/unit/recipes/server_spec.rb -updatecontentinfile./apache/spec/unit/recipes/server_spec.rbfromnonetoa43970 (diffoutputsuppressedbyconfig) *template[./apache/recipes/server.rb]actioncreate -createnewfile./apache/recipes/server.rb -updatecontentinfile./apache/recipes/server.rbfromnoneto3d6b92 (diffoutputsuppressedbyconfig) Slide 47 of 57
  • 48. Lab Exercise: Create the Server Recipe ~/apache/recipes/server.rb package'httpd' file'/var/www/html/index.html'do content'<h1>Hello,world!</h1>' end service'httpd'do action[:enable,:start] end Slide 48 of 57
  • 49. Lab Exercise: Apply the Server Recipe $sudochef-applyapache/recipes/server.rb Recipe:(chef-applycookbook)::(chef-applyrecipe) *yum_package[httpd]actioninstall -installversion2.4.6-40.el7.centosofpackagehttpd *file[/var/www/html/index.html]actioncreate -createnewfile/var/www/html/index.html -updatecontentinfile/var/www/html/index.htmlfromnoneto17d291 ---/var/www/html/index.html 2016-04-0409:50:59.758483838-0500 +++/var/www/html/.chef-index.html20160404-21732-1o44k9g 2016-04-0409:50:59.758483838-0500 @@-1+1,2@@ +<h1>Hello,world!</h1> *service[httpd]actionenable -enableserviceservice[httpd] *service[httpd]actionstart -startserviceservice[httpd] Slide 49 of 57
  • 50. Lab Exercise: Verify Website is Available $curllocalhost <h1>Hello,world!</h1> Slide 50 of 57
  • 51. Group Exercise: Commit Your Work $cdapache $gitinit $gitadd. $gitcommit-m"InitialApacheCookbook" Slide 51 of 57
  • 52. What file would you read first when examining a cookbook? Discussion Slide 52 of 57
  • 53. What file would you read first when examining a cookbook? What other recipes might you include in the apache or workstation cookbook? Discussion Slide 53 of 57
  • 54. What file would you read first when examining a cookbook? What other recipes might you include in the apache or workstation cookbook? Can resources accept multiple actions? Discussion Slide 54 of 57
  • 55. What file would you read first when examining a cookbook? What other recipes might you include in the apache or workstation cookbook? Can resources accept multiple actions? How often would you commit changes with version control? Discussion Slide 55 of 57
  • 56. Cookbooks Versions Version control Q&A What questions can we answer for you? Slide 56 of 57
  • 57. On to Module 4 Slide 57 of 57