Chef-Recipes Bake it calmly..



This is very crucial state of your learnings with chef. It’s not easy to deposit that much of attention continuously so be calm and reassemble all your amplitude for the next bout. Straightway we are going to clash with chef-recipes. We will try to maintain the balance so we didn’t feel overfed.
“Never Eat More Then You Can Lift. Miss Piggy”


This article is written in consideration with centos as platform. We assume that you have basic understanding of Vagrant, Git and Chef-Resources. To know about chef resources follow our previous blogs of this series Chef Start here with ease.. .

Get started

Clone our git repository to create your learning vagrant environment.
  • Change your current directory to Chef/centos/chefRecipes. A vagrant file is present here with some bash provisioning to provide you a complete chef learning environment.
$ cat Vagrantfile
Trigger a vm using following commands.
$ vagrant up
$ vagrant ssh
This will spin up and login into a centos7 vagrant machine with chef installed in it.


A recipe is the first significant element of a cookbook to manage any environment. A recipe club together multiple resources (built-in and custom )and some ruby code. The recipes are ruby files with “.rb” extension. These are generally part of a cookbook. A recipe may be dependent over any other recipe. The recipe ensure the legitimate use of templates and files.
Recipes also contains attributes for different resources. These attributes help chef to maintain the desired state of any machine under chef’s attention.

Shape your first set of recipes

Let’s start with our first recipe. We deal with our common problem statement for all articles. We are installing nginx web server and then create two vhost and using a set of recipes. With this you can gently start feeling the power of chef and automation.
  • Create a nginxInstall.rb file

Create nginxInstall.rb using below command and put the following content in that file.
$ vim nginxInstall.rb
package ‘epel-release’ do
action :install
package ‘nginx’ do
action :install
service ‘nginx’ do
action [ :enable, :start ]
This will install nginx on your machine with default configuration, and also start it.
  • Create nginxVhost.rb file

Create a nginxvhost.rb file to configure your vhost. Paste the following content in it.
$ vim nginxVhost.rb
directory ‘/usr/share/nginx/blog’ do
   recursive true
directory ‘/usr/share/nginx/chef’ do
  recursive true
file  ‘/usr/share/nginx/blog/index.html’ do content ‘Chef Session

Hello this is Blog from Opstree !!’


file  ‘/usr/share/nginx/chef/index.html’ do

 content ‘Chef Session Hello this is Chef Opstree !!’


file ‘/etc/nginx/conf.d/’ do content‘/vagrant/recipes/’)

file ‘/etc/nginx/conf.d/’ do



service ‘nginx’ do

 action [:stop, :start]



Download the content of files and .
This recipe configure the two desired vhost and for you. This recipe includes directory, file and service resources. It also includes ruby function to read files.
Directory resource creates complete data directory structure for chef and blog vhost. File resource maintain content for index.html and .conf files of and
  • Make entry in /etc/hosts

This is also possible with chef but for for now we are doing it manually.
$ sudo vim /etc/hosts
  • Get set go

Now run your recipes one by one and sense the fascination with chef.
$ sudo chef-apply nginxInstall.rb
$ sudo chef-apply nginxVhost.rb
Now all is set, your virtual host are ready to visit.
$ curl
$ curl
Recipes are your first step towards chef expertise. Start behaving like flier, clench the nut bolts and start practising with chef-recipes. Took some basic problems and resolve them with recipes. More you dig into the sea more you get.
“The Great Recipes For Success Is To Work And Always Work. Leon Gambetta”
You are now going to be addict of chef and automation, be aware of your demands .
“Dear stomach you’re bored not hungry, So shut up !!”

Author: saurabhvajpayee

Devops Engineer

One thought on “Chef-Recipes Bake it calmly..”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: