Associated Project(s)

Building a Development Environment With Vagrant and Puppet

Vagrant 1.5 and later provides Hyper-V support, this means that you can use it to create and manage your development environment, regardless of operating system, on Windows hosts. Vagrant managed work environments can be controlled by a single, consistent workflow, to help you maximize the productivity and flexibility of your team. In this tutorial we show you how to use Vagrant to create and manage your development virtual machines, but we go further than that. Vagrant also has built in support for Puppet, we will show you how to use Puppet to configure your VMs. Because this configuration is defined in a sharable text file this can then be used to deploy you application once ready.

We’ll cover the basics in this tutorial, our goal is not to go into the details of Vagrant or Puppet, our goal is to demonstrate the key features of these tools that are relevant to you and your developer team.

Setup

Enable Hyper-v

Hyper-V is included in almost all versions of Windows 8, but in most cases it is not enabled by default. To enable it go to the start screen and type "Programs and features" and select the settings option that is provided. In the resulting dialog click "Turn Windows features on or off" on the left:

clip_image001

 

In the resulting dialog click the checkbox next to "Hyper-V" and click "OK"

clip_image002

SSH Client (optional)

You can use the Hyper-V Manager to connect to your virtual machine once it is started. However, you may prefer to connect from the command line so that you can script actions. If you want to connect from the command line you will need an SSH client.

You have plenty of choices here such as Cygwin, MinGW or Git for Windows which all include SSH clients. If you don't have a preference already you should opt for Git for Windows since you can then use Git to manage your Vagrant configurations. In this tutorial I will use the Git Bash shell to ensure I have SSH on my path.

Alternatively you can use a GUI client like Putty with the credentials that are provided when you run "vagrant ssh" without a command line SSH client installed (or "vagrant ssh-config" if you have installed a client).

Vagrant 1.5

Go to the Vagrant web site and select download

clip_image003

Select the Universal (32 and 64 bit) windows package

clip_image004

Select "Run"

clip_image005

Follow the Wizard

clip_image006

Most people will leave all the options as the default, after a short while the install will be complete.

clip_image007

A system restart is required. Once you have restarted you should be ready to go. Test it by opening a shell and entering the command 'vagrant --version. You should see a response of 'Vagrant 1.5.0' (or later if there has been a new release)

clip_image008

Creating our first Linux VM

Vagrant has some great documentation that will tell you everything you need to know about how to use Vagrant. The Vagrant Hyper-V documentation provides specific information on how to work with Hyper-V. In this tutorial we won't go into detail we'll just show you the minimum you need to know to create a Linux based VM using Hyper-V.

Vagrant Cloud provides a number of base "boxes" for your use. These are preconfigured Vagrant environments. You can create your own Vagrant Hyper-V boxes (including Windows boxes), but in this tutorial we will use the Vagrant Cloud provided Ubuntu 12.04 LTS version.

Create a working directory:

mkdir puppetDemo

cd puppetDemo

Within that directory run (as administrator) the command:

vagrant init cirex/ubuntu-13.10

This will create a Vagrantfile in your directory. This file defines the virtual machine you are going to create next.

Now run (as administrator) the command:

vagrant up --provider=hyperv

If you started your console session without admin privileges you will need to restart the console before running this command.

The first time you run this command your machine will download the box from vagrantcloud.com. This is a fairly large file so it may take a while, but once it has been downloaded there will be no need to repeat again in the future. Once the box is downloaded Vagrant begins to configure the VM. Since Vagrant doesn't (yet) know how to configure new networks for Hyper-V and so you may be asked what kind of network switch you want to use. Once you have answered this question Vagrant finishes creating virtual machine and starts it up.

The machines IP address should be displayed in the console output. You will need this later.

Logging into the Machine

Just to prove things are working you can now connect to the virtual machine using either SSH or the Hyper-V Manager. To use SSH type the command:

vagrant ssh

If it doesn't work see the section on SSH Client above. To use Hyper-V manager simply double click on the virtual machine you want to connect to.

By default the machine is configured to use password-based authentication. This can be changed in the Vagrantfile but for now we'll stick with the default setup in which the username and password are both 'vagrant'. You won’t need to supply the password if you are connecting via SSH as the keys are set-up for you (remember these keys are insecure so don’t use them in production, this is just a development environment convenience).

Version Control your Development Environment

It is good practice to version control your development environment. Assuming you have Git installed (and if you followed my advice above you will have) you can simply run the following commands:

git init

git add Vagrantfile

git commit -m "Starting our Vagrant and puppet demo dev environment"

You will probably want to ignore your ".vagrant" file by adding a ".gitignore" file with the content ".vagrant" then:

git add .gitignore

git commit -m "ignore .vagrant file"

Synced Folders

This is a good time to examine the synced folders of Vagrant. This is a feature that allows a folder on your host machine to be shared with your Vagrant managed VM. This is useful in that it allows you to leverage your preferred development tools on your host machine, it also allows you to ensure that the data on your VM is backed up using your host machines back-up processes.

By default your working directory on the host machine will be mapped to /vagrant on the guest.

Configuring with Puppet

Now that we have our VM we need to configure it. We'll use Puppet for this.

Let's put an Apache web server on this VM using Puppet.

Enable Puppet in Vagrant

First you need to tell Vagrant to use Puppet for provisioing. Open your Vagrantfile in your editor and search for the "Puppet" section. This comments here describe how Vagrant and Puppet work together and provides an example manifest. Add the following lines after these comments.

config.vm.provision :puppet

This configuration uses the default settings for Puppet, like most things in Vagrant you can change these settings, see the Vagrant documentation (and the comments in your Vagrantfile) for more details.

Create a Puppet manifest

Once enabled Vagrant looks for Puppet manifests in the "manifests" folder so in your "puppetDemo" folder create the manifests folder and create a file called "site.pp" with the following content:

mkdir manifests

Create default.pp file in manifests folder:

exec {"apt-get update":

path => "/usr/bin",

}

package {"apache2":

ensure => present,

require => Exec["apt-get update"],

}

service { "apache2":

ensure => "running",

require => Package["apache2"],

}

file {"/var/www/demo":

ensure => "link",

target => "/vagrant/www",

require => Package["apache2"],

notify => Service["apache2"],

}

Remember you can do this with your preferred editor on the host machine and, thanks to the synced folder, it will automatically be made available on your VM.

This configuration does performs the following steps:

  • Execute 'apt-get update'
  • Ensure the latest version of the Apache web server
  • Ensure the web server is running
  • Map link /var/www to /vagrant/www (so that www files can be edited on the host and will survive the destruction and recreation of the VM)

Now put this manifest into version control:

git add manifests/site.pp
git commit -m "Initial Puppet manifest to install and configure the Apache web server"

We need to recreate the VM to make this configuration change take effect.

vagrant reload --provsision

Create your web application

It's beyond the scope of this tutorial to build a full blown web app. We'll just create a "hello world" web page to demonstrate that things are working. You'll need to create the "www" directory and, using your favorite editor, create an "index.html" file with the following contents. This should be placed in a ‘www’ folder in your working directory.

<html>

  <head><title>Hello World</title></head>

  <body>

    <h1>Hello Vagrant and Puppet World</h1>

  </body>

</html>

Now visit the virtual machine http://[IP Address from Conole output]/demo.

Share your development environment

Don't forget to put this file under version control. Once it is there you can tell your colleagues to update from git and reprovision their VM and they will be in exactly the same state as you. Say good by to “works on my machine” errors.

Next steps

Now that you are able to share your development environment easily it is time to think about moving your app to production. Since our VM was provisioned by Puppet it is easy to leverage Puppet in creating your production VM.


6 thoughts on “Building a Development Environment With Vagrant and Puppet

  1. Pingback: Why You Want to Develop in Production-Like Conditions, and How to Do it with Vagrant - Olivier Travers

  2. I just wanted to point out some discrepancies in this article:

    1. You described using “Ubuntu 12.04 LTS version”, but the init string states, “vagrant init cirex/ubuntu-13.10″.
    2. You may want to mention that by enabling Hyper-V in Windows 8, other providers may no longer work as described here: https://docs.vagrantup.com/v2/hyperv/index.html
    3. The default file name for the manifest is “default.pp”, and not “site.pp”. You article inconsistently mentions both.

Leave a Reply