Setting up Chef Knife workstation to use multiple Chef servers

When working with multiple Chef servers, this tip shows how to structure separate Knife configurations using Ruby path expansion and per-environment `.chef` directories. The approach simplifies switching contexts and keeps client keys and server URLs organized across development and production setups.

Coveros Staff

July 16, 2014

I have the problem of working against multiple open-source Chef servers to manage cookbooks, environments, etc in our continuous delivery pipeline. Chef and the “knife” tool, in general, like to use configuration information from ~/.chef/knife.rb. Within that file lies the all-important configuration item: chef_server_url. How do you deal with this if you’re working with multiple servers?

My solution is to use the expanded path of the “__FILE__” ruby constant to point to a particular .chef directory that has the configuration for a particular server. I saw the trick online eons ago and have it found particularly useful of late.

dir = File.expand\_path(File.dirname(\_\_FILE\_\_))
puts '=========== TRAINING CHEF SERVER ============'
puts " Using knife configuration in #{dir} "
puts '============================================='
log\_level :info
log\_location STDOUT
node\_name 'rmills'
client\_key File.join(dir,'rmills.pem')
validation\_client\_name 'chef-validator'
validation\_key File.join(dir,'chef-validator.pem')
syntax\_check\_cache\_path File.join(dir,'syntax\_check\_cache')
chef\_server\_url 'https://training-chef'
cookbook\_path \[ '.', '..', './cookbooks', '~/chef-repo/cookbooks' \]

I store this in ~/training-chef/.chef directory with the appropriate rmills.pem file that is needed. I store similar configuration files in other parallel “prod-chef” or “dev-chef” directories. For the one I use most commonly, I create a link: ln -s ~/dev-chef/.chef ~/.chef. This allows me to use that particular chef server from any location.

The only other notable trick is to use “.” and “..” in your cookbook path. This allows me to do “knife cookbook upload my_cookbook” from either “my_cookbook/.” or “my_cookbook/..” (parent directory of the cookbook).

Coveros Staff

Coveros Staff

This post represents the collective insights of the Coveros team. Our staff consists of software experts who bring deep experience in secure agile development, DevOps, testing, and software quality. Over the past 20 years, Coveros has trained more than 30,000 professionals and worked with half of the Fortune 100 companies on mission-critical software development challenges. We draw on this extensive experience to share practical insights, proven strategies, and real-world solutions that help organizations build better software faster and more securely.