Always make sure you set your cookbook_path in knife.rb to something that makes sense for your working environment.

People often get confused by how/where the ‘knife’ command is finding cookbooks during commands like “knife cookbook upload” or even “knife cookbooks create.” I remember when I first leaned Chef, I would type “knife cookbook create foo_book” and watch the output as Knife happily created it for me. However, then I looked around and couldn’t find it anywhere. If I stumbled around the file system, I might eventually find it in some bizarre place like ~/chef-repo/cookbooks. Similarly, when I cloned a cookbook and tried to upload it with “knife cookbook upload” it would complain “ERROR: Could not find cookbook foo_book in your cookbook path, skipping it” even though I could see it right there in my directory!

The answer to this is simple: manipulate the “cookbook_path” variable in your ~/.chef/knife.rb file to point to the right location.

For example, when I work with Chef on one of my projects, I have the following directory set up for all my cookbooks:

~/git/chef/

This more-or-less contains all the cookbooks that I use. We follow the “one-cookbook-per-repository” pattern, so each cookbook tends to be in its own git repository sub-dir within that directory. In some cases, I have odd-ball cookbooks off in some other locations.

My ~/knife.rb file has the following settings:

cookbook_path [ '.', '..', './cookbooks', '~/git/chef' ]

This means that Chef (or Knife, really) will look for ./foo, ../foo, ./cookbooks/foo, and ~/git/chef/foo any time I make it look for the cookbook “foo.” This means I can do the following:

cd ~/git/chef
knife cookbook upload foo

and things will work the way I expect them. That is, it will find “foo” subdir in my current directory and properly upload it. In fact, since I have cookbook path entries set up, I could really be almost anywhere. When I use the command

knife cookbook upload foo

it means that knife uses the cookbook_path and works from a variety of directory locations where I might be. Consider each cookbook_path entry:

.: works from ~/git/chef
..: works from ~/git/chef/foo
./cookbooks: not relevant, since I don't have a cookbooks/foo anywhere
~/git/chef: finds '~/git/chef/foo' no matter what directory I am in

Similarly, when I run knife cookbook create baz, it uses the first entry of my cookbook path and creates a “./baz” directory with my new cookbook.

Moral of the story: always set your cookbook_path. Never accept the default.

Leave a comment

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

X