This document provides an overview of Chef resources and how to use the chef-apply command. It begins by explaining that a resource describes the desired state of an infrastructure element and defines the steps to achieve that state. Examples are given of package, service, and file resources. The document then demonstrates using chef-apply to install packages, create a file, and apply a recipe file to define the required state.
2. After completing this module, you should be able to:
Use Chef to install packages on your virtual
workstation
Use the chef-apply command
Create a basic Chef recipe file
Define Chef Resources
Objectives
Slide 2 of 55
3. Choose an Editor
You'll need to choose an editor to edit files:
emacs
nano
vi / vim
Slide 3 of 55
4. Linux Editor Reference
Tips for using these editors can be found below:
Nano: (usually touted as the easiest editor to get started with editing
through the command-line.)
OPEN FILE $ nano FILENAME
WRITE (When exiting) ctrl+x, y, ENTER
EXIT ctrl+x
VIM: (Vim, like vi, is more complex because of its different modes. )
OPEN FILE $ vim FILENAME
START EDITING i
WRITE FILE ESC, :w
EXIT ESC, :q
EXIT (don't write) ESC, :q!
Slide 4 of 55
5. Group Exercise: Find an Editor
How about Nano?
$whichnano
/usr/bin/which:nonanoin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
Slide 5 of 55
6. Group Exercise: Find an Editor
How about Nano?
$whichnano
/usr/bin/which:nonanoin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
How about vim?
$whichvim
/usr/bin/which:novimin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
Slide 6 of 55
7. Group Exercise: Find an Editor
How about Nano?
$whichnano
/usr/bin/which:nonanoin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
How about vim?
$whichvim
/usr/bin/which:novimin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
How about Emacs?
$whichemacs
/usr/bin/which:noemacsin(/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/chef/bin)
Slide 7 of 55
9. One of the best ways to learn a technology is to apply
the technology in every situation that it can be
applied.
Learning Chef
Slide 9 of 55
10. One of the best ways to learn a technology is to apply
the technology in every situation that it can be
applied.
A number of Chef tools are installed on the system so
lets put them to use.
Learning Chef
Slide 10 of 55
15. A resource is a statement of configuration policy.
Resources
Slide 15 of 55
16. A resource is a statement of configuration policy.
It describes the desired state of an element of your
infrastructure and the steps needed to bring that item
to the desired state.
Resources
https://docs.chef.io/resources.html
Slide 16 of 55
22. Group Exercise: Install nano, emacs or vim
$sudochef-apply-e"package'nano'"
Recipe:(chef-applycookbook)::(chef-applyrecipe)
*yum_package[nano]actioninstall
-installversion2.0.9-7.el6ofpackagenano
Slide 22 of 55
23. Group Exercise: Did I install my editor?
$whichnano
/bin/nano
Slide 23 of 55
24. What would happen if you ran the installation
command again?
Group Exercise: Test and Repair
Slide 24 of 55
25. What would happen if you ran the installation
command again?
What would happen if the package were to become
uninstalled?
Group Exercise: Test and Repair
Slide 25 of 55
26. chef-apply takes action only when it needs to. Think of
it as test and repair.
Chef looks at the current state of each resource and
takes action only when that resource is out of policy.
Test and Repair
Slide 26 of 55
28. "I heard Chef is written in Ruby. If that's the case its
required that we write a quick "Hello, world!"
application."
Group Exercise: Hello, World?
Slide 28 of 55
29. "I heard Chef is written in Ruby. If that's the case its
required that we write a quick "Hello, world!"
application."
Objective:
Create a recipe file that defines the policy,
The file named hello.txtis created with the content
"Hello, world!".
Group Exercise: Hello, World?
Slide 29 of 55
31. Group Exercise: Create a Recipe File Named hello.rb
Edit ~/hello.rb
file'hello.txt'do
content'Hello,world!'
end
The file named hello.txtis created with the content 'Hello, world!'
https://docs.chef.io/resources.html
Slide 31 of 55
32. Group Exercise: Can chef-apply Run a Recipe File?
$sudochef-apply--help
Usage:chef-apply[RECIPE_FILE|-eRECIPE_TEXT|-s][OPTIONS]
--[no-]color Usecoloredoutput,defaultstoenabled
-e,--executeRECIPE_TEXT Executeresourcessuppliedinastring
--force-formatter Useformatteroutputinsteadofloggeroutput
--force-logger Useloggeroutputinsteadofformatteroutput
-F,--formatFORMATTER outputformattouse
-jJSON_ATTRIBS, LoadattributesfromaJSONfileorURL
--json-attributes
-l,--log_levelLEVEL Settheloglevel(debug,info,warn,error,fatal)
--minimal-ohai Onlyrunthebareminimumohaipluginschefneedstofunction
--[no-]profile-ruby DumpcompleteRubycallgraphstackofentireChefrun(expertonly)
-s,--stdin ExecuteresourcesreadfromSTDIN
-v,--version Showchefversion
-W,--why-run Enablewhyrunmode
-h,--help Showthismessage
Slide 32 of 55
33. Group Exercise: Apply a recipe file
$sudochef-applyhello.rb
Recipe:(chef-applycookbook)::(chef-applyrecipe)
*file[hello.txt]actioncreate
-createnewfilehello.txt
-updatecontentinfilehello.txtfromnoneto315f5b
---hello.txt 2016-04-1115:01:29.875000726+0000
+++./.hello.txt20160411-7152-1d3f0yn 2016-04-1115:01:29.875000726+0000
@@-1+1,2@@
+Hello,world!
Slide 33 of 55
34. Group Exercise: What does hello.txtsay?
$cathello.txt
Hello,world!
Slide 34 of 55
35. What would happen if you ran the command again?
Group Exercise: Test and Repair
Slide 35 of 55
36. What would happen if you ran the command again?
What would happen if the file contents were
modified?
Group Exercise: Test and Repair
Slide 36 of 55
37. What would happen if you ran the command again?
What would happen if the file contents were
modified?
What would happen if the file were removed?
Group Exercise: Test and Repair
Slide 37 of 55
38. What would happen if you ran the command again?
What would happen if the file contents were
modified?
What would happen if the file were removed?
What would happen if the file permissions (mode),
owner, or group changed?
Have we defined a policy for these attributes?
Group Exercise: Test and Repair
Slide 38 of 55
41. file'hello.txt'do
content'Hello,world!'
end
The TYPE named NAME should be ACTION'd with
ATTRIBUTES
Example Resource Types:
cron, directory, dsc_resource, dsc_script, env, file, git,
group, ifconfig, link, log, mdadm, mount, package, reboot,
registry_key, remote_directory, remote_file, route, script,
service, template, user, windows_package,
windows_service and more...
Resource Definition
Slide 41 of 55
45. Read https://docs.chef.io/resource_file.html
Discover the file resource's:
default action.
default values for mode, owner, and group.
Update the file policy in hello.rbas follows:
The file named 'hello.txt' should be created with...
the content set to 'Hello, world!',
the mode set to '0644',
the owner is 'root',
and the group is 'root'.
Lab 2.1: The file Resource
Slide 45 of 55
46. Lab 2.1: The Updated file Resource
~/hello.rb
file'hello.txt'do
content'Hello,world!'
mode'0644'
owner'root'
group'root'
action:create
end
The default mode is set by the POSIX Access Control Lists.
The default owner is the current user (could change).
The default group is the POSIX group (if available).
The default action is to create (not necessary to define it).
Slide 46 of 55
47. Create a recipe file named setup.rbthat defines the policy:
The package named nano is installed.
The package named tree is installed.
The file named /etc/motdis created with the content
'Property of ...'.
Use chef-apply to apply the recipe file named setup.rb
Lab 2.2: workstation Setup Recipe
Slide 47 of 55
48. Lab 2.2: workstation Setup Recipe File
~/setup.rb
package'nano'
package'vim'
package'emacs'
package'tree'
file'/etc/motd'do
content'Propertyof...'
end
The package named nano is installed.
The package named tree is installed.
The file named /etc/motdis created with the content 'Property of ...'.
Slide 48 of 55
51. What is a resource?
What are some other possible examples of resources?
Discussion
Slide 51 of 55
52. What is a resource?
What are some other possible examples of resources?
How did the example resources we wrote describe the
desired state of an element of our infrastructure?
Discussion
Slide 52 of 55
53. What is a resource?
What are some other possible examples of resources?
How did the example resources we wrote describe the
desired state of an element of our infrastructure?
What does it mean for a resource to be a statement of
configuration policy?
Discussion
Slide 53 of 55
54. What questions can we answer for you?
chef-apply
Resources
Resource - default actions and default attributes
Test and Repair
Q&A
Slide 54 of 55