Skip to main content

Chef-Resources Elementary ingredient..


Resources are the “elementary ingredient” of Chef house. Before mounting up on the spikes of chef’s elevations, it's favorable to tailor your suit with basics. Combining common ingredients to make a new play substance is always thrilling!. I wish I had some magic secrets or shortcuts to share, but the truth is that you have to start with the ABC's and then you can rule over the game.

Trigger a VM

Git clone our repository dedicated for this blog series.

Change directory to Chef/centos/chefResourceBackings. Here you find a Vagrantfile which spin up a centos7 machine with chefdk and other tools installed in it.
$ cat Vagrantfile

Launch a new vagrant machine and login.
$ vagrant up

$ vagrant ssh
Your learning environment is ready. Although in this blog we do not focus a lot on practicals, we try to clear our theoretical concepts.

Backings of Resources

Chef resources are the statements which define the configuration approach for any element. From the officials of chef, resources are

  • Describes the desired state for a configuration item
  • Declares the steps needed to bring that item to the desired state
  • Specifies a resource type—such as package, template, or service
  • Lists additional details (also known as resource properties), as necessary
  • Are grouped into recipes, which describe working configurations

Chef works with two basic parts, properties and actions.


Properties are the attributes and definitions for the target element of any resources. Some common properties for all resources without exception.


What to do when a resource is fail in its doings i.e. continue or stop. Default value is false or stop.   


This is an optional property. You can explicitly define the correct provider for a resource. In common practices this is not mandatory to specified.


Number of attempts that a resource tries in chef-run. Default value is 0.


Time duration between two consecutive tries. Default value is 2.


This defines that the data in resource is sensitive and is not logged by chef-client. Default value is false. This property only applies to the execute, file and template resources.


Actions differ for distinct resources. A common action for every resource is :nothing. Nothing action stated that resource should not do anything until it is notified by any other resource.  

Some Basic Resources

Lets took example of some of the basic resources.
  • Package
This resource manage packages. This install, uninstall and upgrade any package. This resource is also a platform for some other dependent package management resources such as apt_package, dpkg_package, gem_package, yum_package etc. Available actions are :install, :purge, :remove, :upgrade and :nothing.

package 'tar' do
 version '1.26-29.el7'
 action :install
package 'tar' do
 action :remove
  • File
This is responsible to manage files on a machine. Available actions are :create, :create_if_missing, :delete, :nothing and :touch.


file '/tmp/test.txt' do
 content 'This is testing file.'
 mode '0755'
 owner 'root'
 group 'root'

file '/tmp/test2.txt' do
 action :create
This will create a test.txt file under /tmp directory and put the contents of string.txt file into it.

  • Service

Subjected to manage services. Available actions are :disable, :enable, :nothing, :reload, :restart, :start and :stop.


service 'nginx'  do
 action :start


service 'nginx' do
 supports :status => true, :restart => true, :reload => true
 action [ :enable, :start ]
This will ensure enable then start options for nginx service. Before this, install nginx using package resource.
  • Template
This resource calls chef templates to dynamically generate static files. Templates are ".erb" files with some variables and placed under “/templates” directory of cookbook. Available actions are  :create, :create_if_missing, :delete, :nothing and :touch.  Template resource only available with cookbooks, you can not use this resource with chef-apply from command line.


template '/tmp/sshd_config' do
 source 'sshd_config.erb'
 owner 'root'
 group 'root'
 mode '0755'

template '/tmp/config.conf' do
 source 'config.conf.erb'
   :config_var => 'mytext'
This will pass variables to config.conf.erb file to generate config.conf file.

Extended Resource

Have some attention towards less likely used but important resources.  
  • Git
This resource manages git repository by interacting with source code management system. Git version 1.6.5 (or higher) is required. Available actions are :checkout, :export, :nothing and :sync.


git '/opt/mygit/' do
 repository ''
 revision 'master'
 action :sync
  • Link
Resource is responsible to manage soft and hard links. Available actions are :create, :delete and :nothing.

link '/tmp/myfile' do
 to '/etc/ssh/sshd_config'

link '/tmp/myhardfile' do
 to '/etc/ssh/sshd_config'
 link_type :hard
The default link_type is :symbolic.  

  • Script

This resources is used to execute external script. The supported interpreters are Bash, csh, Perl, Python, or Ruby. Available actions are :run and :nothing.

script 'extract_module' do
 interpreter 'bash'
 cwd '/tmp'
 code <<-EOH
   mkdir -p /tmp/mytest
   touch /tmp/mytest/file.txt
  • Cron

To manage your cron jobs use this resource. If a property is not specified then default ‘*’ value is taken. Available actions are :create, :delete and :nothing.

cron 'noop' do
 hour '5'
 minute '0'
 command '/bin/true'


cron 'tuesdaycron' do
 minute '50'
 hour '11'
 weekday '2'
 command '/bin/true'
 action :create
This will run the cron job only on tuesday 11:50am.

To test all resources available above, go to resource directory under “chefResourceBackings” directory.  All resources are available as bash script.

Custom Resources

It’s an extension of the initially available properties.  Chef allows you to create your own resources. These custom resources extend the basic definitions of the built-in resources. The custom resources resides in the “/resources” directory of any cookbook. Conventionally name of any custom resource is the name of cookbook and name of resource file separated by underscore (_).  Custom resource creation is not an initial task for us so we skip this for now.

This is difficult to cover all about the resource(every resource and their properties or actions) in this article. Refer to chef-resources for more information.

It’s hard to get this boring stuff exciting in any manner. But it's a fact that every serious effort makes you closer towards the excellence.

“The Expert In Anything Was Once A Beginner. “

Wanna be an adroit of Chef then from now “practice like a devil play like an angel”.


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…