Chef Cookbooks?

This is our third post on Chef Series, we are already done with Simple Chef-workstation model, Chef objects and recipes. If you have missed reading it, kindly check: Start Automation with Chef?

Chef objects and Recipes?

Lets learn something about Chef Cookbooks and “chef” command line utility, a quick recap what actually chef coobooks are:

A cookbook is an ordered structure that contains recipes. It also many other things but lets focus on the creating a simple cookbook for our understanding before proceeding with advanced concepts.

Chef Automation

A “chef” is a command line utility which allows to generate cookbooks with well defined structure.

Lets begin with the cookbook creations:

1.) Before starting anything, the first step is to create a cookbooks directory.

2.) Then understand the command line utility “chef” to get familiar with it.

3.) Create your first cookbook and understand the by default cookbook directory/file structure

4.) Create/Move recipes to the cookbook directory

5.) Apply/Run chef-client and see the deployment status.

Demonstration with simple example:

Go to home directory or any other specific directory and create cookbooks directory.

mkdir cookbooks

Use help option and understand “chef” command line utility and different options. Its like a man page.

chef -help

chef generate -help

Use “generate” option of “chef” utility and create your first cookbook and understand the default directory/file structure created after execution of cookbook generate command.

chef generate cookbook cookbooks/thinknyx

Generating cookbook thinknyx
– Ensuring correct cookbook file content
– Ensuring delivery configuration
– Ensuring correct delivery build cookbook content

Your cookbook is ready. Type `cd cookbooks/thinknyx` 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

At initial stages of learning, lets focus only on recipe folder, go to the recipe folder inside our cookbook and notice default.rb file. This is where we store the recipes in our cookbook. If you check the content, it will be empty. Usually we keep very common recipe here which we need in our whole environment by default.

cat default.rb
#
# Cookbook Name:: thinknyx
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

Let’s create a recipe inside default.rb:

cat default.rb
#
# Cookbook Name:: thinknyx
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
file ‘/etc/issue’ do
content ‘This is Thinknyx Production Server, access is illegal for unauthorized users’
end

Cookbook is generated, Recipe is created inside default.rb, Now its time to learn how to apply/run our cookbooks. We will use the same “chef-client” utility to run/apply our cookbook which we used for individual recipe in our previous post.

chef-client –local-mode /home/cookbooks/thinknyx/recipes/default.rb
[2016-09-08T22:01:39-07:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.13.37
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2016-09-08T22:01:44-07:00] WARN: Node thinknyx.chef.com has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/cookbooks/thinknyx/recipes/default.rb
* file[/etc/issue] action create
– update content in file /etc/issue from 43e4fb to f986e6
— /etc/issue 2014-09-15 04:39:07.000000000 -0700
+++ /etc/.chef-issue20160908-7443-1xda18c 2016-09-08 22:01:44.729010344 -0700
@@ -1,4 +1,2 @@
-Red Hat Enterprise Linux Server release 6.6 (Santiago)
-Kernel \\r on an \\m
–
+This is Thinknyx Production Server, access is illegal for unauthorized users
– restore selinux security context

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 05 seconds

And we are all done with the apply/run. Result is below:

cat /etc/issue
This is Thinknyx Production Server, access is illegal for unauthorized users

We are all done with the cookbook generation, recipe creation and cookbook run. This is very basic example to run the concepts of cookbook.

In our next post we will bring some advanced concepts to create cookbooks at other location instead of default.rb.

Leave a Comment

Your email address will not be published.