Skip to main content

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 blog.opstree.com 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 '<html><title>Chef Session</title><body><h1>Hello this is Blog from Opstree !!</h1></body></html>'
end

file  '/usr/share/nginx/chef/index.html' do
 content '<html><title>Chef Session</title><body><h1>Hello this is Chef Opstree !!</h1></body></html>'
end

file '/etc/nginx/conf.d/blog.opstree.com.conf' do
 content IO.read('/vagrant/recipes/blog.opstree.com.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 blog.opstree.com.conf and chef.opstree.com.conf .

This recipe configure the two desired vhost chef.opstree.com and blog.opstree.com 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 blog.opstree.com.
  • 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 blog.opstree.com
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 blog.opstree.com
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)

Comments

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 release.properties 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…