Resources
Resources
− The basic building blocks of Chef
− Define a part of your infrastructure
and its state
− Can be:
> A package to be installed
> A service that should be running
> A file that should be created
> Much much more
apache.rb
§ A collection of Resources from our
earlier chef-apply example.
package 'httpd’ do
action :install
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]’
end
Resources - Type
apache.rb
package 'httpd’ do
action :install § Resources
> Must have a TYPE
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]’
end
Resources - Name
apache.rb
§ Resources
> Must have a NAME
package 'httpd’ do
action :install
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]'
end
Resources - Parameters
apache.rb
§ Resources
> Can have PARAMETERS
package 'httpd’ do
action :install
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]'
end
Resources - Actions
apache.rb
§ Resources
> Take ACTION to put the resource in thedesired state
package 'httpd’ do
action :install
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]'
end
Resources - Notifications
apache.rb
§ Resources
> Can send NOTIFICATIONS to other resources
package 'httpd’ do
action :install
end
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>Hi There!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]'
end
Group Resources into Recipes
Recipes
− Are a collection of Resources that
define a specific task
− May use the results of a Search Query
to populate Resources
− May use Data Bags
− May have a dependency on one (or
more) recipes
− Must be stored in a Cookbook
− Must be added to a run list before it
can be used by chef-client
− Are always executed in the same order
as defined in the run list
Cookbooks
Cookbooks
− Are a collection of Recipes that
defines all desired states for one
application
− Also contain:
> Default values for Chef Variables
(Attributes)
> Static Files / Templates
> Custom Resources (libraries, lwrps, etc…)
> Testing code
− Are VERSIONED
− Can also depend on / include other
cookbooks
Cookbooks
− Are the basic unit of configuration and
policy distribution…think of it being
chef’s version of a RPM.
− Set sane defaults for expected
behavior
− Usually a 1:1 mapping of an
application or functionality
Problem: Manager wants a webserver to
serve up a simple web page
§ Problem:
− Manager wants a simple web page put up
§ Success?
− We can hit our node with a web browser and see our homepage
Remember our road map to success?
DIGCA!
Determine the Desired State of Node
− Install Apache package
− Start the Apache service
− Create a file for our index.html
Use chef generate to create cookbooks
$ chef generate --help
Usage: chef generate GENERATOR [options]
Available generators:
app Generate an application repo
cookbook Generate a single cookbook
recipe Generate a new recipe
attribute Generate an attributes file
template Generate a file template
file Generate a cookbook file
lwrp Generate a lightweight resource/provider
repo Generate a Chef policy repository
policyfile Generate a Policyfile for use with the install/push
commands (experimental)
Edit the default.rb recipe
(mysite/recipes/default.rb)
#
# Cookbook Name:: mysite
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
Add package resource to install Apache
(mysite/recipes/default.rb)
#
# Cookbook Name:: mysite
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
package 'httpd'
What did we just add?
(mysite/recipes/default.rb)
Is a package resource
§ Its name is ‘httpd’
§ It has no parameters
§ No action is defined, so it will
follow its default action. For
the package resource, the
default action is :install
package ‘httpd’ do
action :install
end
Add service resource to manage Apache’s
state
(mysite/recipes/default.rb)
#
# Cookbook Name:: mysite
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
package 'httpd’
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
What did we just add?
(mysite/recipes/default.rb)
§ It is a service resource service 'httpd' do
supports :reload => true
action [:start, :enable]
end
(mysite/recipes/default.rb)
§ It is a service resource
§ Its name is also ‘httpd’
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
§ It has a parameter that tells
chef that apache’s init script
supports a reload call
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
with 2 actions:
§ start
§ enable
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
Add file resource to create our index.html
file
(mysite/recipes/default.rb)
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>I love PANTS!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]’
end
What did we just add?
(mysite/recipes/default.rb)
§ It is a file resource
§ Whose name is '/var/www/html/
index.html’
§ With 4 parameters
§ Plus a notification to reload the
apache service if our file
changes
No action defined?
What is a sane default behavior
for our file resource?
file '/var/www/html/index.html' do
content '<html>I love PANTS!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]’
end
Our full default.rb recipe
(mysite/recipes/default.rb)
#
# Cookbook Name:: mysite
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
package 'httpd'
service 'httpd' do
supports :reload => true
action [:start, :enable]
end
file '/var/www/html/index.html' do
content '<html>I love PANTS!</html>’
mode '0644’
owner 'nobody’
group 'nobody’
notifies :reload, 'service[httpd]’
end
Upload your awesome new cookbook
$ knife cookbook upload mysite
How do we run this recipe? Run List!
A Run List defines all of the
information necessary for Chef to
configure a node into the desired
state
§ A Run List is:
− An ordered list of roles and/or recipes
that are run in the exact order defined
in the run-list
− Always specific to the node on which it
runs
− Stored as part of the Node Object on
the Chef server
What does that look like?
§ Recipes are specified by ‘recipe[name]’
§ This would call the default recipe from cookbook “name”
§ If we wanted a specific recipe from a cookbook, we could call it directly in
the run list with ‘recipe[name::recipe]’
§ Roles are specified by ‘role[name]’
Remember: Order Matters!
Add our default recipe to our node’s run
list
$ knife node run_list add node1 'recipe[mysite]'
Run chef-client on your node
sudo chef-client
What was all of that text earlier? (aka
READING the chef-client log)
sudo chef-client
[2015-08-13T03:55:16+00:00] INFO: Forking chef instance to
converge...
Starting Chef Client, version 12.4.1
[2015-08-13T03:55:16+00:00] INFO: *** Chef 12.4.1 ***
[2015-08-13T03:55:16+00:00] INFO: Chef-client pid: 21727
[2015-08-13T03:55:18+00:00] INFO: Run List is [recipe[mysite]]
[2015-08-13T03:55:18+00:00] INFO: Run List expands to [mysite]
[2015-08-13T03:55:18+00:00] INFO: Starting Chef Run for node1
[2015-08-13T03:55:18+00:00] INFO: Running start handlers
[2015-08-13T03:55:18+00:00] INFO: Start handlers complete.
PROBLEM: Manager got a new homepage!
§ This is referred to as configuration drift
§ We just need to update our cookbook with the new info and re-run
chef-client to bring our node into compliance with the new
requirements. (It's what chef was built for…)
Here is the new file
<html>
<title>Homepage For Gap, Inc.</title>
<body>
Welcome to Gap! Let’s sell some pants!
</body>
</html>
§ That’s a lot of html to pass in
the content parameter of the
file resource
§ Is there a better resource we
can use?
The cookbook_file resource
A cookbook_file resource manages files by using static files that exist
within a cookbook’s /files directory.
cookbook_file '/var/www/html/index.html' do
source 'index.html'
mode '0644'
owner 'nobody'
group 'nobody'
notifies :reload, 'service[httpd]'
end
Update our default recipe to use
cookbook_file resource
(mysite/recipes/default.rb)
package 'httpd'
service 'httpd' do
action [:start, :enable]
end
cookbook_file '/var/www/html/index.html' do
source 'index.html'
mode '0644'
owner 'nobody'
group 'nobody'
notifies :reload, 'service[httpd]'
end
Create the index.html file in the files/
default directory in our cookbook
(mysite/files/default/index.html)
<html>
<title>Homepage For Gap, Inc.</title>
<body>
Welcome to Gap! Let’s sell some pants!
</body>
</html>
No comments:
Post a Comment