1. Create a cookbook
First, from your
~/chef-repo
directory, create a cookbooks
directory.Terminal: ~/chef-repo
Now run the
chef
command to generate a cookbook named learn_chef_apache2
.Terminal: ~/chef-repo
The
cookbooks/learn_chef_apache2
part tells Chef to create a cookbook named learn_chef_apache2
under the cookbooks
directory.
Here's the directory structure that the command created.
Terminal: ~/chef-repo
Note the default recipe, named
default.rb
. This is where we'll move our Apache recipe in a moment.
Although it's not shown in the sample output for the
tree
command, the chef generate cookbook
command also creates a.delivery
directory. This directory contains starter content for working with Chef Automate. As you become more famililar with Chef, you can check out this tutorial to try Chef Automate for free.2. Create a template
Now we'll move the home page to an external file. First, run this command to generate the HTML file for our home page.
Terminal: ~/chef-repo
The file
index.html.erb
gets created under learn_chef_apache2/templates
.Terminal: ~/chef-repo
The .erb extension simply means that the file can have placeholders. More on that later.
Now copy the contents of the HTML file from your recipe to the new HTML file,
index.html.erb
.Editor: ~/chef-repo/cookbooks/learn_chef_apache2/templates/index.html.erb
1 2 3 4 5 |
|
Here, you're adding the web site content directly to your cookbook for learning purposes. In practice, your web site content would more likely be some build artifact, for example a .zip file on your build server. With Chef, you could pull updated web content from your build server and deploy it to your web server.
3. Update the recipe to reference the HTML template
Write out the recipe,
default.rb
, like this.Editor: ~/chef-repo/cookbooks/learn_chef_apache2/recipes/default.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | apt_update 'Update the apt cache daily' do frequency 86_400 action :periodic end package 'apache2' service 'apache2' do supports :status => true action [:enable, :start] end template '/var/www/html/index.html' do source 'index.html.erb' end |
4. Run the cookbook
Now run the cookbook. To do so, we use the
chef-client
command and specify what's called the run-list.Terminal: ~/chef-repo
Previously, you ran
In this example,
chef-client
to run a single recipe from the command line. A run-list specifies each of the individual recipes from your cookbook that you want to apply. Here, you applied just one recipe, but the run-list can contain multiple recipes from multiple cookbooks.In this example,
recipe[learn_chef_apache2]
is the same as specifying recipe[learn_chef_apache2::default]
, meaning we want to run the learn_chef_apache2
cookbook's default recipe, default.rb
.
Run
curl
again or refresh your web browser to confirm that your web page is still available.Terminal: ~/chef-repo
The result is the same as before, but with a cookbook things are now easier to manage.
Summary
Your web server is shaping up! With a cookbook you're now better organized. A cookbook adds structure to your work. You can now author your HTML code in its own file and use a template resource to reference it from your recipe.
You also saw the run-list. The run-list lets you specify which recipes to run, and the order in which to run them. This is handy once you have lots of cookbooks, and the order in which they run is important.
Keep in mind that the web server cookbook you wrote in this lesson likely won't be the one you'd use in production. Only youknow the specific needs for your infrastructure. You bring your requirements and Chef provides the tools that help you meet them.
Exercises
Exercise 1
How does a cookbook differ from a recipe?
Exercise 2
What's the run-list?
No comments:
Post a Comment