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).

 

Leave a comment

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

X