This document provides an overview of organizing recipes and using version control with Chef cookbooks. It begins by outlining the learning objectives, then asks and answers some common questions about modifying recipes, packaging recipes into cookbooks, and using version control. It introduces the basics of Git version control and walks through exercises to generate a sample cookbook, add recipes, and initialize the cookbook as a Git repository. The document demonstrates how to create, apply, and verify a sample Apache web server recipe to install and configure Apache.
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
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
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
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
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
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
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
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
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