Skip to content

Instantly share code, notes, and snippets.

@metaxis
Created December 8, 2010 02:08
Show Gist options
  • Save metaxis/732788 to your computer and use it in GitHub Desktop.
Save metaxis/732788 to your computer and use it in GitHub Desktop.
Lab: Create a Web Server cookbook

Chef Fundamentals Labs

Lab 4 Create a Web Server cookbook

Exercise 1 - Create a new cookbook called webserver and launch new web servers on EC2

a. Create a new cookbook called webserver

Use knife cookbook --help to figure out how to create new cookbook. Also refer to the student guide for examples

b. Update the metadata.rb

Make sure you add a dependency (depends) for the apache2 cookbook. See http://wiki.opscode.com/display/chef/Cookbooks for details

c. Update the the default.rb recipe in the ../cookbooks/webserver/recipes directory

#
# Cookbook Name:: webserver
# Recipe:: default
#
# Copyright 2010, Opscode, Inc..
#
# All rights reserved - Do Not Redistribute
#

include_recipe "apache2"

template "/var/www/index.html" do
  source "index.html.erb"
  owner "root"
  group "root"
  mode "0644"
end

This simple recipe will install apache and create a default page that will updated in the template in the next step.

d. Create the default web page Template file

<html>
        <head>
                <title>Welcome to <%= node[:hostname]%></title>
        </head>
        <body>
                Chef rocks...you have reached:
                <ul>
                        <li><b>FQDN</b>: <%= node[:fqdn] %></li>
                        <li><b>Public FQDN</b>: <%= node[:ec2][:public_hostname]%></li>
                        <li><b>IP Address</b>: <%= node[:ipaddress] %></li>
                        <li><b>Public IP</b>: <%= node[:ec2][:public_ipv4] %></li>
                        <li><b>Platform</b>: <%= node[:platform] %></li>
                        <li><b>Plaform Version</b>: <%= node[:platform_version] %></li>
                        <li><b>Run List</b>: <%= node.run_list %></li>
                </ul>
        </body>
</html>

The name of the erb file should match the name of the template source name specified in the recipe. Create the file in ../templates/default directory.

e. Upload the new Webserver cookbook

knife cookbook ...

f. Run the new Webserver cookbook on your node

  1. add it to the runlist of your node and run chef-client

    knife node ... sudo chef-client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment