Home » DevOps » Executing Cookbooks with Chef-Solo

About Arpit Aggarwal

Arpit Aggarwal
Arpit is a Consultant at Xebia in India. He has been designing and building J2EE applications since more than 4 years. He is fond of Object Oriented and lover of Functional programming and his specialties are Java, J2EE, Scala, SOA, Cloud and Big Data Technologies.

Executing Cookbooks with Chef-Solo

Chef-Solo is an open source tool that runs locally and allows us to provision the guest machine using chef cookbooks without the complication of any chef client and server configuration.

In this post, we will learn to execute a simple recipe which will create a file with some content inside /tmp directory using chef cookbook recipe.

To start with, we will create a directory where all our chef related content resides and for me it’s chef-repository, as follows:

$ mkdir chef-repository

Next, we will configure Knife inside our chef-repository to create cookbooks and recipes, as follows:

$ cd chef-repository
$ mkdir .chef
$ echo "cookbook_path [ '/vagrant/chef-repository/cookbooks' ]" > .chef/knife.rb

/vagrant/chef-repository/cookbooks specified above refers the path to chef cookbooks.

Now, we will create our first cookbook using knife which will also create recipe with name default.rb for us with default content, as follows:

$ knife cookbook create mycookbook

Edit the content of cookbooks/mycookbook/recipes/default.rb to create the file with name default-file with given content inside /tmp directory as below:

file "/tmp/default-file" do
  content "It's file created by Chef Recipe"
  owner "root"
  group "root"
  mode 00600
end

Move to directory ‘cookbooks’ and create chef configuration file solo.rb:

$ echo "cookbook_path ['$PWD']" > solo.rb

Next, run the recipe we created using chef-solo., as follows:

$ chef-solo -c solo.rb -o mycookbook::default;

Running multiple recipes inside cookbook?

We can run multiple recipes using chef-solo –override-runlist and –json-attributes command line options as follows:

a. Using chef-solo –override-runlist

chef-solo -c solo.rb -o recipe[mycookbook::default],mycookbook::first

mycookbook::first refers to the recipe with name first.rb inside directory cookbooks/mycookbook/recipes/

b. Using chef-solo –json-attributes

Create a .json file with any name for me it’s recipes.json with content as:

{
  "run_list": [
    "recipe[mycookbook::default]",
    "recipe[mycookbook::first]"
  ]
}

Then execute below command from directory ‘cookbooks’:

sudo chef-solo -c solo.rb -j recipes.json
Reference: Executing Cookbooks with Chef-Solo from our SCG partner Arpit Aggarwal at the Arpit Aggarwal blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of