Empower Vagrant with Chef

31 / Jul / 2016 by Rajdeep Singh

Screen Shot 2016-07-30 at 8.23.22 PM

Vagrant is a development friendly tool to make easy creation of development environments. In its own words it “Creates and configures lightweight, reproducible, and portable development environments”. Today we are going to learn how we can use chef-zero to provision guest OS for the development environment. We are not going to explore basic of Chef and Vagrant in this blog as I am assuming you already have beginner level competency in both.

Before you go ahead make sure you have these packages installed on your system:
1. Virtual Box
2. Vagrant

There is no such rocket science to install these packages, and If these do not exist on your machine, please follow the official guideline.

Resolve plugin dependency

The vagrant use provisioner to perform tasks on guest machine and these provisioners are available as a plugin. Let’s install all required plugin and understand why we need them.

Vagrant-Omnibus plugin is required for Vagrant to install chef-client on guest:

vagrant plugin install vagrant-omnibus

We are using vagrant-chef-zero plugin to provision local chef recipes on guest:

vagrant plugin install vagrant-chef-zero

The vagrant-berkshelf plugin download and install cookbooks and dependencies on the guest machine. But make sure Berksfile need to be present in the same working directory:

vagrant plugin install vagrant-berkshelf

Screen Shot 2016-07-30 at 8.20.12 PM

Create Vagrantfile

To quick start with vagrant execute “vagrant up“ in your cookbook directory. It will create a Vagrantfile in your working directory with default configuration:

Screen Shot 2016-07-30 at 11.20.04 AM

I have modified default Vagrantfile with the parameters of my choice. Here I have taken centos7 as the guest OS layer and defined some other values to hostname, IPaddress, port forwarding and compute units. You are free to modify these parameters:

### Virtual box Guest OS configuration ###### 
  Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-7.1"
  config.vm.hostname = "local-host”
  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "private_network", ip: “192.168.1.100”

  config.vm.provider "virtualbox" do |v|
    v.name = "local-host"
    v.memory = 2048
    v.cpus = 1
  end

Configure Chef provisioner

Let’s take an example to assign Nginx recipe to automate the installation of package and configuration. In below configuration we instruct Vagrant to perform:
a. Bootstrap guest machine with node name “local-host”
b. Install chef-client in it.
c. Search for cookbook named Nginx in current working directory
d. Assign recipe to the node and execute a set of commands.

#### Chef Configuration #####################
  config.vm.provision "chef_zero" do |chef|
    chef.node_name = "local-host”
    chef.environments_path = "../../environments"
    chef.environment = “local”
    chef.nodes_path = "nodes"
    chef.roles_path = "roles"
    chef.run_list = [ 'recipe[nginx::default]']
  end

You can download full Vagrantfile from here.

Now it’s time to fire it

It may require you to initialize a chef tool called berkshelf in the same directory; then you should be ready to start the vagrant provisioning:

berks install

It will create a Berksfile, and this will be used by Vagrant provisioner. Look’s like we are in good position to test it out. To bring the machine up execute:

vagrant up

Keep your eye on virtual box it will download centos7 box and create a virtual machine with defined configuration. Once the machine is up Vagrant will start provision on it. I have been using this to build development environment on a fresh machine, but it can be used to test your recipes on a virtual machine. The combination of Chef and Vagrant is fruitful and with fewer efforts.

FOUND THIS USEFUL? SHARE IT