Running a chef cookbook using chef-solo

Pre-requisites — Familiarity with Chef

Was looking at chef for the first time and was trying to figure out how to test a cookbook recipe locally. Thought I did share one of the simplest and easiest ways of testing a recipe that I followed.

chef-solo is a command that executes the chef client without the need of a chef server. chef-solo is part of the Chef Development Toolkit (ChefDK) that can be installed from here.

Once you have installed the ChefDK, we can use the below commands to create our first cookbook:

>> chef -v
Chef Development Kit Version: 1.3.43
chef-client version: 12.19.36
delivery version: master (dd319aa632c2f550c92a2172b9d1226478fea997)
berks version: 5.6.4
kitchen version: 1.16.0
>> chef generate cookbook first_cookbook
Generating cookbook test
- Ensuring correct cookbook file content
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
Your cookbook is ready. Type `cd first_cookbook` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.
Why not start by writing a test? Tests for the default recipe are stored at:
test/smoke/default/default_test.rb
If you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb

Add the below lines to first_cookbook/recipes/default.rb file

file “/home/user/hello.txt” do
content “Hello, this is my first cookbook recipe\n”
action :create
end

Let us write a solo.rb to execute the test recipe:

solo.rb
--------
file_cache_path “/home/user/cache”
cookbook_path “/home/user/chef”

Create a json file to specify the recipes to be run

web.json
--------
{
“run_list”: [ “recipe[first_cookbook]” ]
}

Now, execute the below chef-solo command to test your recipe.

>> chef-solo -c solo.rb -j web.json
[2017-05-12T18:34:00+05:30] INFO: Forking chef instance to converge...
Starting Chef Client, version 12.19.36
[2017-05-12T18:34:01+05:30] INFO: *** Chef 12.19.36 ***
[2017-05-12T18:34:01+05:30] INFO: Platform: x86_64-linux
[2017-05-12T18:34:01+05:30] INFO: Chef-client pid: 26623
[2017-05-12T18:34:10+05:30] INFO: Setting the run_list to ["recipe[first_cookbook]"] from CLI options
[2017-05-12T18:34:10+05:30] INFO: Run List is [recipe[first_cookbook]]
...

Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: first_cookbook::default
* file[/home/user/hello.txt] action create[2017-05-12T18:34:10+05:30] INFO: Processing file[/home/user/hello.txt] action create (first_cookbook::default line 8)
(up to date)
[2017-05-12T18:34:10+05:30] INFO: Chef Run complete in 0.327877198 seconds
Running handlers:
[2017-05-12T18:34:10+05:30] INFO: Running report handlers
Running handlers complete
[2017-05-12T18:34:10+05:30] INFO: Report handlers complete
Chef Client finished, 0/1 resources updated in 09 seconds

You can add another recipe second.recipe and update the run_list to run this as well as below:

web.json
--------
{
“run_list”: [ “recipe[first_cookbook]”,
“recipe[first_cookbook::second]”
]
}

Next, will be experimenting running the same cook book using travisci, shall post soon :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.