Thursday 20 October 2016

Create a cookbook

1. Create a cookbook

First, from your ~/chef-repo directory, create a cookbooks directory.

Terminal: ~/chef-repo

 
skytap@host-1:~$ mkdir cookbooks
Now run the chef command to generate a cookbook named learn_chef_apache2.

Terminal: ~/chef-repo

                  
skytap@host-1:~$ chef generate cookbook cookbooks/learn_chef_apache2
Generating cookbook learn_chef_apache2- Ensuring correct cookbook file content - Ensuring delivery configuration - Ensuring correct delivery build cookbook content  Your cookbook is ready. Type `cd cookbooks/learn_chef_apache2` to enter it. There are several commands you can run to get started locally developing and testing your cookbook.Type `delivery local --help` to see a full list. Why not start by writing a test? Tests for the default recipe are stored at: test/recipes/default_test.rb If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb
The cookbooks/learn_chef_apache2 part tells Chef to create a cookbook named learn_chef_apache2 under the cookbooksdirectory.
Here's the directory structure that the command created.

Terminal: ~/chef-repo

                   
skytap@host-1:~$ tree cookbooks
cookbooks└── learn_chef_apache2    ├── Berksfile    ├── README.md    ├── chefignore    ├── metadata.rb    ├── recipes    │   └── default.rb    ├── spec    │   ├── spec_helper.rb    │   └── unit    │       └── recipes    │           └── default_spec.rb    └── test        └── recipes            └── default_test.rb 7 directories, 8 files
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

         
skytap@host-1:~$ chef generate template cookbooks/learn_chef_apache2 index.html
Recipe: code_generator::template   * directory[cookbooks/learn_chef_apache2/templates/default] action create     - create new directory cookbooks/learn_chef_apache2/templates/default    * template[cookbooks/learn_chef_apache2/templates/index.html.erb] action create     - create new file cookbooks/learn_chef_apache2/templates/index.html.erb      - update content in file cookbooks/learn_chef_apache2/templates/index.html.erb from none to e3b0c4      (diff output suppressed by config)  
The file index.html.erb gets created under learn_chef_apache2/templates.

Terminal: ~/chef-repo

                      
skytap@host-1:~$ tree cookbooks
cookbooks└── learn_chef_apache2    ├── Berksfile    ├── README.md    ├── chefignore    ├── metadata.rb    ├── recipes    │   └── default.rb    ├── spec    │   ├── spec_helper.rb    │   └── unit    │       └── recipes    │           └── default_spec.rb    ├── templates    │   ├── default    │   └── index.html.erb    └── test        └── recipes            └── default_test.rb 9 directories, 9 files
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

  
    

hello world
  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

                     
skytap@host-1:~$ sudo chef-client --local-mode --runlist 'recipe[learn_chef_apache2]'
[2016-10-19T19:06:38+00:00] WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.14.89resolving cookbooks for run list: ["learn_chef_apache2"]Synchronizing Cookbooks:  - learn_chef_apache2 (0.1.0)Installing Cookbook Gems:Compiling Cookbooks...Converging 4 resourcesRecipe: learn_chef_apache2::default  * apt_update[Update the apt cache daily] action periodic (up to date)  * apt_package[apache2] action install (up to date)  * service[apache2] action enable (up to date)  * service[apache2] action start (up to date)  * template[/var/www/html/index.html] action create    - update content in file /var/www/html/index.html from 2914aa to ef4ffd    (no diff) Running handlers:Running handlers completeChef Client finished, 1/5 resources updated in 02 seconds
Previously, you ran 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

      
skytap@host-1:~$ curl localhost
      

hello world

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?
Answer 
A recipe is a collection of resources, and typically configures a software package or some piece of infrastructure. A cookbook groups together recipes and other information in a way that is more manageable than having just recipes alone.
For example, in this lesson you used a template resource to manage your HTML home page from an external file. The recipe stated the configuration policy for your web site, and the template file contained the data. You used a cookbook to package both parts up into a single unit that you can later deploy.

Exercise 2

What's the run-list?
Answer 
The run-list lets you specify which recipes to run, and the order in which to run them. The run-list is important for when you have multiple cookbooks, and the order in which they run matters.

No comments:

Post a Comment