My (very) personal chef tutorial Part V: Using Berkshelf

In the former parts of this tutorial we only wrote our own cookbooks. But how are existing cookbooks reused ? In this part of the tutorial we will look at Berkshelf that helps managing own cookbooks and cookbook dependencies.There are thousands of cookbooks ready for usage on the internet. The chef supermarket and github are only two of the places where you can find cookbooks for almost everything.

One way to reuse those cookbooks is by using Berkshelf. In order to use berkshelf run the command

in the cookbooks directory.

That command creates a lot of new files. Among them is a .git directory that you should delete if the cookbook is already part of a larger repository.

The most important file created is the Berskfile. This is the file to declare cookbook dependencies in.

In the following example we will reuse the getting started cookbook just for the sake of demonstration. The first thing to do, is to set a dependency in the files metadata.rb an Berksfile:

After declaring the dependency we can use the recipes of the getting started cookbook in our cookbook. The example in default.rb just uses the default recipe:

All that recipe does is put a file called chef-getting-started.txt in the users home.

Next we tell berks to install the cookbooks:

This command needs to be run in the directory of the cookbook.

That’s it. Testing this cookbook is a little bit more complicated when using chef-client in local mode. In order to test this example follow these steps:

1. Create a folder for testing:

2. Run the following command in the cookbook directory

This tells berks to copy all the cookbooks and their dependencies declared in the Berksfile to that folder.

3. Create a file called client.rb in the test folder having the following content:

4. Change to the test folder and execute the following command:

That should create the file mentioned in your home folder.

As already explained there are many sources for getting the right recipe. In this case I used chef supermarket. In order to install from other sources you need to alter the dependency syntax in the Berksfile. Here are some examples:

The first two cookbooks come from the supermarket. The third one is installed locally and the third is from a git repository.

This concludes this introduction into dependency management using berks.

The last part of this tutorial is going to explore some of the best resources on the web for writing recipes.






One thought on “My (very) personal chef tutorial Part V: Using Berkshelf”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">