Backend and Infrastructure

Provisioning with Chef

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

In the previous lesson we discussed why you would want to use a configuration management tool. In this lesson we'll dive into how you automate the customization of new servers using the configuration management tool, Chef. Most of the work in assembling Chef recipes is already done for you. In this lesson you will be able to see how the pieces fit together; however, you will not be required to download each of the individual recipes to get everything working.

Lesson Outcomes

By the end of this lesson you will be able to describe how to add a Chef recipe to your setup, and re-provision Vagrant.

Lesson Summary

In the video, I've already setup Chef recipes, roles, and cookbooks for you. So instead of having to download all the pieces separately, you can just start with the code bundle for this lesson.

The outline provided below, however, includes each of the steps that I went through to prepare the configuration Chef recipes and roles that you're downloading.

Locating and Using Chef Cookbooks

  1. Locate Chef cookbooks for AMP stack, drush, and Drupal-specific PHP libraries you want pre-loaded.
  1. Place downloaded cookbooks into a sub-folder in your project directory. For example: chef-recipes/cookbooks.
  2. Create a role file to include all of the recipes (from each of the cookbooks) you want to load. This file can be placed into its own roles sub-directory. For example: chef-recipes/roles/lamp_stack.rb.
  3. Update the Vagrantfile to include:
  • path to cookbooks folder
  • path to roles folder
  • role(s) to load for this server
  • new values for any of the default settings for your recipes
  1. Provision the machine to enable / set-up / trigger the Chef configuration settings:
  • $ vagrant provision

The machine is now configured, but does not have Drupal installed. This is on purpose. You probably have a specific Drupal project you're working on.

Customizing Configuration Files to Use Chef Recipes

The role files contain a list of individual recipes, and/or roles that must be run to provision a specific type of server. Generally there are three or four parts to them.

  • name
  • description
  • run list (recipes and roles to add)
  • default settings for recipes (optional)

You should investigate the contents of the folder chef-recipes/roles. There is a detailed README tailored to this learning series, as well as the two roles that were created for this lesson.

The Vagrantfile is well commented, this section includes a quick references of the settings that have been updated in the file.

  config.vm.provision :chef_solo do |chef|
     # default settings
     chef.cookbooks_path = "path_to_cookbooks_folder"
     chef.roles_path = "path_to_roles_folder"

     # roles and recipes to load
     # recipes are generally loaded from within roles
     chef.add_role "name_of_role"
     chef.add_recipe "name_of_recipe"

     # overrides for settings in recipes
     chef.json = {
       "name_of_recipe" => {
         "name_of_setting" => "new_value",
       },
     }
  end

Gotchas

  • If you are using these recipes for Drupal 8: the recipe provided for Drush won't work. You'll need to install Drush version 7 or greater (compatibility chart on the Drush project page).
  • You will probably want to update Chef with the omnibus plugin for Vagrant if the cookbook you want to add requires a newer version of Chef. On the host machine, install the plugin: $ vagrant plugin install vagrant-omnibus. Note: Chef must be updated before it is "run". For this lesson, I opted to use older versions of the cookbooks. 20 hours of my life spent working on upgrades that didn't work. The later scripts provide more flexibility (e.g. install ANY version of Drush, not just 5.8), but they work well enough for our needs.
  • Apt will only run if the cache is older than a day. The first time the machine is provisioned, this might be a problem. You can force updating the cache with the following setting in Vagrantfile: chef.json = { "apt" => {"compiletime" => true} }
  • If you are running into problems with permissions, refer to the tips in Sharing Files Between Your Guest and Host Machine.
  • Windows users will also need to download the following cookbooks, and add them to the directory you have configured for chef.cookbooks_path
    • chef-handler
    • chef-sugar
    • iis
    • windows
    • xml
    • yum
    • yum-epel