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.
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
package ‘tar’ do
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.’
file ‘/tmp/test2.txt’ do
This will create a test.txt file under /tmp directory and put the contents of string.txt file into it.
Subjected to manage services. Available actions are :disable, :enable, :nothing, :reload, :restart, :start and :stop.
service ‘nginx’ do
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.
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
template ‘/tmp/config.conf’ do
:config_var => ‘mytext’
This will pass variables to config.conf.erb file to generate config.conf file.
Have some attention towards less likely used but important resources.
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.
Resource is responsible to manage soft and hard links. Available actions are :create, :delete and :nothing.
link ‘/tmp/myfile’ do
link ‘/tmp/myhardfile’ do
The default link_type is :symbolic.
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
mkdir -p /tmp/mytest
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
cron ‘tuesdaycron’ do
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.
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”.