DevOps

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.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button