Chef-Recipes Bake it calmly..

(source: google.com)
 

Introduction

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”
(source: google.com)

Prerequisites

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.

Recipe

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 chef.opstree.com and opstree.com/blog/ 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
end
 
package ‘nginx’ do
action :install
end
 
service ‘nginx’ do
action [ :enable, :start ]
end
 
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
end
directory ‘/usr/share/nginx/chef’ do
  recursive true
end
file  ‘/usr/share/nginx/blog/index.html’ do content ‘Chef Session

Hello this is Blog from Opstree !!’

end

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

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

end

file ‘/etc/nginx/conf.d/opstree.com/blog/.conf’ do content IO.read(‘/vagrant/recipes/opstree.com/blog/.conf’)
end

file ‘/etc/nginx/conf.d/chef.opstree.com.conf’ do

 content IO.read(‘/vagrant/recipes/chef.opstree.com.conf’)

end

service ‘nginx’ do

 action [:stop, :start]

end

 

Download the content of files opstree.com/blog/.conf and chef.opstree.com.conf .
 
This recipe configure the two desired vhost chef.opstree.com and opstree.com/blog/ for you. This recipe includes directory, file and service resources. It also includes IO.read 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 chef.opstree.com and opstree.com/blog/.
  • Make entry in /etc/hosts

 
This is also possible with chef but for for now we are doing it manually.
$ sudo vim /etc/hosts
 
127.0.0.1 opstree.com/blog/
127.0.0.1 chef.opstree.com
  • 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 chef.opstree.com
 
$ curl opstree.com/blog/
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 !!”
(source: google.com)

Author: saurabhvajpayee

Devops Engineer

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

Leave a Reply