Chef objects and Recipes?

In our previous post, we started chef with simple configuration using Chef-workstation model. If you have missed reading it, kindly check: Start Automation with Chef?

Lets keep the initial learning phase of Chef Configuration Management tool as simple as possible. The installation of ChefDK (chef development kit on chef-workstation) will install many chef development utilities, out of which few to note at this stage are:

chef – command line tool for chef

chef-client – to run chef recipes (code), cookbooks (structured codes)

knife – for communication and bootstrapping

Berkshelf – cookbook dependency manager

Test Kitchen – for testing created cookbooks

We will use mentioned utilities in our chef series when and where required.

Chef Automation

Lets start with some of the most important objects used in chef codes –

1.) Resources – The smallest building block of Chef is called “Chef Resource”. Example: Lets suppose you want to create a file in your Linux server using chef, you need some way to direct chef to create a file for you. This is what a “Resource” will do for you. Chef have many default inbuilt resources like file, user, group, service, package etc which you will use to create a specific code.

Here is the list of complete resources available for Chef:

2.) Recipe – A resource is used to create a recipe which is an independent chef code. Example: Lets say you want to create a file in Linux server, for which you will be using a “Resource Type” file and create a code as per requirements (like mode = 0777, owner = thinknyx, group = thinknyx, content = “This is our first Chef recipe” etc.) Now this complete process of using a resource and configuring it as per our requirements forms a “Recipe” which we will apply on our servers to automate our infrastructure as a code.

3.) Cookbook – In simple layman language we can say, a cookbook is a combination of recipes which build your complete application in a code. Or in technical language we can say cookbook is a ordered structure of coding/recipes. Example: If you have to maintain 1000’s of recipes in unordered manner it would be almost impossible to maintain them, but if we have a ordered structure of recipes we can manage them with easy.

Let’s see some of the simple Recipe examples, go to any directory (usually home directory) in your chef-workstation and create a file ending with .rb extension (rb is for ruby language, as chef is based on ruby):

1.) File creation recipe: create a file thinknyxfile.rb –

file ‘/etc/motd’ do

content ‘This is Thinknyx chef recipe”


where: file -> Resource type

/etc/motd -> is the file name

do -> start instruction

content -> Attributes for file resource

action -> When you see action, its again the instruction to perform the specific task

2.) Package installation recipe: create a file thinknyxpkg.rb

package ‘ntpd’ do

action :install


3.) Service start recipe: create a file thinknyxser.rb

service ‘ntp’ do

action [ :enable, :start]


where: action [ :enable, :start], enable will checkon the service across persistent reboots and start will start the service.

Similarly you can go through the different resources and practice creating recipes using multiple resources. You can also create recipes using multiple resources in single recipe file.

This is all about creating recipes, lets see how to execute them on our local chef-workstation. Here we will use the chef-client command line utility:

chef-client –local-mode <recipe-name> where –local-mode is to run the recipe in local server

chef-client –local-mode thinknyxfile.rb

chef-client –local-mode thinknyxpkg.rb

chef-client –local-mode thinknyxser.rb

Once executed, check the output in server for /etc/motd file, ntp package and service.

In our next post we will show how to create and use chef cookbooks.

Leave a Comment

Your email address will not be published.