Tag Archives: chef

Build your Vagrant box using Chef

What is Chef

The goal of Chef is to give you the ability to have an immutable infrastructure. This could be for your stand alone project or for an entire business. The purpose of Chef is to be able to create cookbooks that build your servers automatically. These cookbooks enable you to build your infrastructure in a logical and object oriented way. You don’t have to come from a server administration background to create them either. Chef was built using Ruby scripts. So as long as you have programmed in the past and know a little bit about Ruby you should be all set. For server knowledge all you really have to have is a basic understanding of how servers work. You will also have to have an understanding of what components you need. After that you can browse around for cookbooks and find what you are looking to install. After you build your initial cookbook or download an existing one you will start seeing how simple it can be. I would suggest that you start out with an existing cookbook. You will start finding out that the majority if not all cookbooks you need were already built by the community. Most developers give you full access to their cookbooks Github page. If for some reason you cannot find an existing cookbook you should be able to reference others for how to do it. When I setup my Chef infrastructure all cookbooks were already created. I simply had to modify a couple of them to fit my needs.

Why use Chef

Chef has many use cases. The main one of course is the ability to have an immutable infrastructure. These days with virtualization being so hot it’s important to be able to create a new instance of your infrastructure without having to thinking about. Traditionally before Chef server administrators would write their own shell scripts. After creating these shell script’s they would run them manually after installing the fresh OS. Not only was it time consuming to run these scripts on each server but they also had to be updated all the time. For example if a new version of the application came out. So they tended to get out dated faster. So a tool came out called Puppet. The purpose of Puppet was to take all of these shell scripts and to put them into a tool. This tool would provide some infrastructure so it was easier to manage the scripts. It also allowed you to create generic scripts so when an update came out the process was easier to upgrade. Puppet allows you to define a series of scripts to run. It was definitely a step up from running all of those shell scripts manually. It was a better way to manage these scripts and keep them organized.

After Puppet was out for a while Chef came out. Chef took a lot of the ideas from Puppet and made them better and even easier to use. Chef is the tool that allows a developer to create the infrastructure. It takes infrastructure out of the hands of server administrator’s and into the hands of developers. The reason why Chef is so friendly and powerful is because it was written in Ruby. Ruby is a very elegant language and is very human friendly. Also it’s an object oriented language so it’s very easy to abstract your cookbooks.

Another advantage to Chef is it has great enterprise support. If you sign up for Chef enterprise you can configure which cookbooks run and when. Then you can run a simple knife command and it will interact with the Chef enterprise server. It will look for your cookbooks defined per server and run them in the order you put them in. You can set a different order per server. You can also set it to run different cookbooks per server. Chef enterprise is actually free up to five nodes. If you are at all interested in it it’s a great place to start. So you can create up to five different configurations for free.

How do you use Chef with Vagrant

Vagrant ships with Chef Solo integrated. It’s very easy to get Vagrant started with Chef. The first thing you need to do is get Vagrant installed and setup. You can follow the following instructions on how to do that. After you get that installed you need to setup your Vagrant file with all of your chef configurations.

The following shows some of the configuration needed to get it up and running. The first line is the operating system you want to use. For my case it was ubuntu 14.04 but you can use whatever you like. This even includes Redhat. The next line is even more important. This will tell it to use the latest omnibus version. This line tells Vagrant to install chef on the server before anything else. When you put in :latest in it will take whatever the latest version of Chef available at the given time.

 

  config.vm.box = "chef/ubuntu-14.04"
  config.omnibus.chef_version = :latest

This next set of code is the configuration for Chef to use. These configuration settings are what Chef Enterprise allows you to do through their nice GUI interface. In Vagrant of chef solo you have to define these in the Vagrant file. In turn for Chef Enterprise you define them using their tool.

  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = "cookbooks" # path to your cookbooks
    chef.data_bags_path = "data_bags" # path to your data_bags
    chef_gem_path    = "/opt/chef/embedded/lib/ruby/gems/1.9.1"
    chef.binary_env  = "GEM_PATH=#{chef_gem_path} GEM_HOME=#{chef_gem_path}"
    chef.binary_path = "/opt/chef/bin"

    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
    chef.add_recipe "oh-my-zsh"

    chef.json = {
      oh_my_zsh: {
        users: [
          {
            login: 'vagrant',
            theme: 'af-magic',
            plugins: [
              'git',
              'vi-mode',
              'rails',
              'ruby',
              'bundler',
              'virtualenv',
            ]
          },
        ],
      },
    }
  end

You can also run Chef solo by itself. If you use it standalone without Vagrant you have to tie everything together. I am not familiar with that and cannot speak to how that would work. I hope that this overview helps you learn a little bit more about Chef. I hope that it at least sparks your interest enough to play around with it. Vagrant is a great place to start if you do not have a server to work with.

For a working sample visit my vagrant-chef repo.