Permalink
Browse files

Add instructor-led section exercises

  • Loading branch information...
1 parent 16fd704 commit 1edde01eb0562d5c1c1b7cb014351b8fe4e1d98f @jtimberman jtimberman committed Jun 1, 2012
@@ -470,3 +470,57 @@ Chef Node
* View the node object on the Chef Server with knife
* Create new attributes on the node with a cookbook and role.
* Use node attributes in a conditional in the recipe.
+
+# Cookbook Attributes
+
+Create the attributes file in the webserver cookbook.
+
+ cookbooks/webserver/attributes/default.rb
+
+It should have the following line:
+
+ default['webserver']['origin'] = "Cookbook"
+
+Upload the cookbook, and run Chef on the node. View the node's
+attribute with:
+
+ knife node show NODE_NAME -a webserver.origin
+
+# Role Attributes
+
+Create a "webserver" role.
+
+ @@@ruby
+ name "webserver"
+ run_list("recipe[webserver]")
+ default_attributes(
+ "webserver" => {
+ "origin" => "Role"
+ }
+ )
+
+Upload the role with knife and run Chef on the node. View the node's
+attribute with:
+
+ knife node show NODE_NAME -a webserver.origin
+
+# Attributes in a Recipe
+
+In the webserver default recipe, create a conditional based on the
+node's platform.
+
+ @@@ruby
+ if node['platform'] == "ubuntu"
+ log "I'm on Ubuntu #{node['platform_version']}"
+ else
+ log "I'm on #{node['platform']} #{node['platform_version']}"
+ end
+
+ node.set['webserver']['origin'] = "Recipe"
+ log "The webserver origin attribute is #{node['webserver']['origin']}"
+
+Upload the cookbook and run Chef. It should display two log lines.
+
+View the node's attribute with:
+
+ knife node show NODE_NAME -a webserver.origin
@@ -13,7 +13,7 @@ This work is licensed under a Creative Commons Attribution Share Alike 3.0 Unite
# Cookbooks Overview
-Cookbooks are the fundamental units of file distribution in Chef. They
+Cookbooks are the fundamental unit of file distribution for Chef. They
are "packages" for Chef recipes and other helper components.
They are designed to be sharable packages for managing infrastructure
@@ -71,6 +71,7 @@ Cookbook metadata contains documentation about the cookbook itself.
license "apachev2"
description "Configures web servers"
long_description "Configures web servers with a cool recipe"
+ version "1.0.0"
# Metadata Dependency Management
@@ -203,6 +204,26 @@ Become Chef resources:
# Resources
+This script:
+
+ @@@sh
+ mkdir /tmp/foo
+ touch /tmp/foo/bar
+ chmod baz:qux /tmp/foo/bar
+
+Can be Chef resources:
+
+ @@@ruby
+ directory "/tmp/foo"
+
+ file "/tmp/foo/bar" do
+ owner "baz"
+ group "qux"
+ action :touch
+ end
+
+# Resources
+
Resources take idempotent actions through *Providers*.
Providers know how to determine the current state of the resource on the node.
@@ -403,3 +424,85 @@ Cookbooks, Recipes and Resources
* Create a new cookbook
* Write a simple recipe with two resources
* Run Chef with the cookbook on a node
+
+# Create Webserver Cookbook
+
+In the top-level of the Chef repository, create the following
+directory:
+
+ cookbooks/webserver/recipes
+
+Create the metadata and default recipe.
+
+ cookbooks/webserver/recipes/default.rb
+ cookbooks/webserver/metadata.rb
+
+Create the templates directory and template file.
+
+ cookbooks/webserver/templates/default
+ cookbooks/webserver/templates/default/index.html
+
+# Metadata contents
+
+Use the following for the metadata.rb content:
+
+ maintainer "YOUR NAME"
+ version "1.0.0"
+
+# Recipe contents
+
+This is the content of `cookbooks/webserver/recipes/default.rb`.
+
+ @@@ruby
+ package "apache2" do
+ action :install
+ end
+
+ service "apache2" do
+ action [:enable, :start]
+ end
+
+ directory "/var/www" do
+ owner "www-data"
+ group "www-data"
+ end
+
+ template "/var/www/index.html" do
+ source "index.html.erb"
+ owner "www-data"
+ group "www-data"
+ mode 00644
+ end
+
+# Create a template file
+
+This is the content of `cookbooks/webserver/templates/default/index.html.erb`
+
+ @@@html
+ <pre>
+ Platform: <%= node['platform'] %>
+ Platform Version: <%= node['platform_version'] %>
+ Default IP Address: <%= node['ipaddress'] %>
+ Fully Qualified Domain Name: <%= node['fqdn'] %>
+ Node's Run List: <%= node.run_list.to_s %>
+ </pre>
+
+# Upload, Run Chef
+
+Upload the cookbook.
+
+ knife cookbook upload webserver
+
+Add the recipe to the node's run list. Replace `NODE_NAME` with your
+node's name.
+
+ knife node run list NODE_NAME 'recipe[webserver]'
+
+Run chef-client on the node. When complete, view the node on the Chef
+Server.
+
+ knife node show NODE_NAME
+
+# Exercise Complete
+
+Questions?
@@ -7,6 +7,8 @@ Section Objectives:
* Understand some of the common Ruby objects used in Chef
* Familiarity with the ways Chef uses Ruby for DSLs
+This section does not have a hands on exercise.
+
.notes These course materials are Copyright © 2010-2012 Opscode, Inc. All rights reserved.
This work is licensed under a Creative Commons Attribute Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us; or send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA.
Oops, something went wrong.

0 comments on commit 1edde01

Please sign in to comment.