There are many reasons why you might not want to have Chef Server in your deployment path. Or, as this example shows, you don’t want to host your own Chef Server. We instead use Hosted Chef, but purely as a cookbook repository. We will not have any nodes communicate directly with Chef Server, so we will not have to worry about the five-node limit for free accounts.
As with any knife bootstrap method, this is not suitable for auto-scaling. I will post an article on auto-scaling with Chef sometime in the future.
Let’s get started.
First, you will need to create a Hosted Chef account. Go ahead and do that while I wait.
Assuming you already have ChefDK installed, we install knife-zero:
1 |
chef gem install knife-zero |
Save the following as ~/.chef/knife.rb and update as needed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# See http://docs.chef.io/config_rb_knife.html for more information on knife configuration options cookbook_path = '/tmp/berkshelf/cookbooks' chef_repo_path = File.expand_path('../', cookbook_path) current_dir = File.dirname(__FILE__) log_level :info log_location STDOUT node_name '<chef-username>' client_key "#{current_dir}/<chef-keyname>.pem" chef_server_url 'https://api.chef.io/organizations/<chef-organization>' chef_repo_path chef_repo_path # https://knife-zero.github.io/tips/with_cookbook_manager/ knife[:before_bootstrap] = knife[:before_converge] = "berks vendor #{cookbook_path}" %w(roles environments data_bags).each do |dir| FileUtils.ln_sf File.realpath(dir), "#{chef_repo_path}/#{dir}" if Dir.exist?(dir) end |
All the values in brackets need to be updated, and are found in your Hosted Chef account.
This knife.rb uses Berkshelf to create a chef-repo style structure from your cookbook directory. If you have any of roles , environments , or data_bags directories in your cookbook directory, they will be symlinked correctly so that it follows the correct directory structure. Otherwise, using any of these features would not work correctly.
Go into the Chef cookbook directory you’d like to apply to a node. You can apply the cookbook as follows:
1 |
knife zero bootstrap --overwrite -r '<recipe or role>' -E <chef environment> root@<hostname or IP> |
You will notice that Berkshelf will download all of your cookbooks from Chef or Supermarket and install them in /tmp/berkshelf/cookbooks . The entire /tmp/berkshelf directory is copied to the node, and chef-client is run in local mode using the arguments you passed.
So long as your run_list had no issues, you should see your run complete successfully. Congratulations!
Leave a Reply