Skip to main content

Chef-Cookbooks Roast it perfectly..


This is our first major step towards our learnings. We are now quite sound with the ABC’s of chef. Now we announce the most compelling facet of chef. Cookbooks are the most crucial segment of the chef’s kingdom. Nothing worth having comes easy, so kickoff with more efforts.

A Real Test Of a Good Chef Is, Perfectly Roasted Chicken. julia child

Wait wait wait !! fire your all cylinders but remember to breath.


This article assumes that you are aware with the basics of Git and Vagrant. You know the basic functioning of chef and its recipes and resources. This article is written with centos7 platform. To know about chef follow our previous blogs of this series Chef Start here with ease.. .

Get started

Clone our git repo and fire up a vagrant box with this.

  • Go to Chef/centos/chefCookbooks directory. This directory contains a Vagrantfile, which can launch a centos7 vagrant machine with Chefdk and other essential tools installed.
$ cat Vagrantfile

  • Download Chefdk using below available command

$ cd Chef/centos/chefCookbooks

$ wget

  • This directory also includes a knife.rb file which sets the cookbook folder path and default editor for the virtual machine.

  • Launch a new vagrant machine and login into it.
$ vagrant up

$ vagrant ssh

Your working environment is ready. Let's start with chef cookbooks.

Create your first cookbook

First create a dedicated directory for our cookbooks. As in knife.rb file it will be created by following command.

$ mkdir /vagrant/cookbooks

Chef manage its cookbooks using a version control system so next we initialize and also make our first commit for /vagrant/cookbooks directory. Provide your name and email for git configuration.
$ mkdir /vagrant/cookbooks
$ cd /vagrant/cookbooks
$ git init
$ git add .
$ git config --global ""
$ git config --global "Your Name"
$ git commit -m "Initial Commit"

Now you are ready to start with the creation of your first cookbook.

Call up with knife

We are using knife to generate are cookbooks. Generate your cookbook for installing nginx and to set a virtual hosts and with this.

Generate our first cookbook using below written command. This command setup copyright, email, license, and readme format options for your cookbook.
$ knife cookbook create nginxVhost -C "Saurabh Vajpayee" -m "" -I nginxv1 -r md

Let's create a recipe

Create the default recipe with below provided command and put below available content.

$ vim /vagrant/cookbooks/nginxVhost/recipes/default.rb

package 'epel-release' do
action :install

package 'nginx' do
action :install

directory "#{node['nginx']['webroot']}" do
 recursive true

template "/etc/nginx/conf.d/#{node['nginx']['conffile']}" do
 source 'chefmanagedconf.conf.erb'
   :port => "#{node['nginx']['port']}",
   :servername => "#{node['nginx']['servername']}",
   :webroot => "#{node['nginx']['webroot']}"

template "#{node['nginx']['webroot']}/index.html" do
 source 'index.html.erb'
   :servername => "#{node['nginx']['servername']}"

line = " #{node['nginx']['servername']}"
file ='/etc/hosts')
file.insert_line_if_no_match(/#{line}/, line)

service 'iptables' do
 action :stop

service 'nginx' do
action :restart

This file includes multiple chef resources and some variable like 'webroot', conffile, port and servername. These variables have their default values under attribute directory and used in the templates which are next to create.

Create attribute file

This file contains the default value of your variables used in recipes. Create a default.rb file and place these values.

$ chef generate attribute /vagrant/cookbooks/nginxVhost/ default

$ vim /vagrant/cookbooks/nginxVhost/attributes/default.rb

default['nginx']['port'] = "80"
default['nginx']['webroot'] = "/usr/share/nginx/blog"
default['nginx']['servername'] = ""
default['nginx']['conffile'] = ""

Create templates

Create template file to provide dynamic touch to your files. First create template for “configuration” files, and put below provided content.

$ chef generate template /vagrant/cookbooks/nginxVhost/ chefmanagedconf.conf

$ vim /vagrant/cookbooks/nginxVhost/templates/default/chefmanagedconf.conf.erb

   listen       <%= @port  %>;
   server_name  <%= @servername  %>;

   location / {
       root   <%= @webroot  %>;
       index  index.html index.htm;

   error_page  404              /404.html;
   location = /404.html {
       root   <%= @webroot  %>;

   # redirect server error pages to the static page /50x.html
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   <%= @webroot  %>;

And next place template for index.html file with following content.

$ chef generate template /vagrant/cookbooks/nginxVhost/ index.html

$ vim /vagrant/cookbooks/nginxVhost/templates/default/index.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">

<html xmlns="" xml:lang="en">
       <title>Test Page for the Opstree Server </title>

       <h1>Welcome to <strong> <%= @servername %> </strong></h1>

Your cookbook is ready for initial workings. Let’s run it.

Run and feel like a million bucks

Run your cookbook with below command, and relax for a while.

$ sudo chef-client --local-mode  --runlist 'recipe[nginxVhost]'
By default this cookbook setup vhost if you want to setup another vhost then overiride default values of variables. To do this create a json file which declares the new values for variables.

$ vim /vagrant/runlist.json

"nginxVhost": {
"webroot" : "/usr/share/nginx/chef",
"servername" : "",
"conffile": ""

"run_list": [


And run chef-client once again with following commands.

$ sudo chef-client --local-mode  -j /vagrant/runlist.json

Now you  have power to create as many vhost automatically with chef.

Verify the vhost

$ curl
$ curl

From now we have to work hard to match the expectation of the industry. You are now developing into a chef proficient. Do some experiments and play hard.


  1. The information on this blog is very useful and very interesting. If anyone needs to know about these just check at Devops Online Training Hyderabad

  2. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training Hyderabad


Post a Comment

Popular posts from this blog

EC2 Ssh Connection Refused

When ssh: connect to host ip_address port 22 Connection refused

Unable to access server???
Exactly when you see the error - “ssh: connect to host ip_address port 22: Connection refused” while connecting your AWS EC2 Instance. In order to find solution of the problem, you will go to AWS forum and other channels where you need to answers several questions first. But it's very difficult to find the actual problem. In order to get clues what the problem is, we should provide as many details as possible about what we have tried and the results we are getting. Because there are hundreds of reason why a server or service might not be accessible, also connectivity is one of the toughest issue to diagnose, especially when you are hosting something critical on your box. I've seen several topics on this problem, but none offers a solution to it.  I was not aware for what should I look at first. So I walk through from the very basics and investigated the following thing Use of verbose while ss…

jgit-flow maven plugin to Release Java Application

Introduction As a DevOps I need a smooth way to release the java application, so I compared two maven plugin that are used to release the java application and in the end I found that Jgit-flow plugin is far better than maven-release plugin on the basis of following points: Maven-release plugin creates .backup and files to your working directory which can be committed mistakenly, when they should not be. jgit-flow maven plugin doesn't create these files or any other file in your working directory.Maven-release plugin create two tags.Maven-release plugin does a build in the prepare goal and a build in the perform goal causing tests to run 2 times but jgit-flow maven plugin builds project once so tests run only once.If something goes wrong during the maven plugin execution, It become very tough to roll it back, on the other hand jgit-flow maven plugin makes all changes into the branch and if you want to roll back just delete that branch.jgit-flow maven plugin doesn…

VPC per envrionvment versus Single VPC for all environments

This blog talks about the two possible ways of hosting your infrastructure in Cloud, though it will be more close to hosting on AWS as it is a real life example but this problem can be applied to any cloud infrastructure set-up. I'm just sharing my thoughts and pros & cons of both approaches but I would love to hear from the people reading this blog about their take as well what do they think.

Before jumping right away into the real talk I would like to give a bit of background on how I come up with this blog, I was working with a client in managing his cloud infrastructure where we had 4 environments dev, QA, Pre Production and Production and each environment had close to 20 instances, apart from applications instances there were some admin instances as well such as Icinga for monitoring, logstash for consolidating logs, Graphite Server to view the logs, VPN server to manage access of people.

At this point we got into a discussion that whether the current infrastructure set-u…