Vagrant and Puppet, Part 3: Introduce Puppet to Vagrant


After finishing Part 2 we now have a working virtual machine ready to use. The only problem now is there is barely anything on it to use, it is just a basic Ubuntu install.

At this point you normally go through the manual process of adding repositories, installing packages, setting up services, etc. Then if you wanted to reuse this machine you’d have to go through the process again. This is what Puppet fixes.

Puppet allows you to write a manifest of your virtual machine’s desired configuration and have it happen automatically. Let’s get started.

Step 1: Directories and Files

Puppet Directory StructureCreate a new folder in your project and name it puppet.

Create a new folder named manifests and one named modules inside the puppet folder.

Inside manifests create a new text file named init.pp. This will be our main Puppet manifest. A manifest has the Puppet blocks that provision the machine.

We’ll go into Puppet modules in another article.

Step 2: Tell Vagrant About Puppet

Open your Vagrantfile and add the follow lines below the static IP configuration line.

This tells Vagrant that we are using Puppet and let’s it know where the manifests, modules, and main manifest file are.

Step 3: Tell Puppet to Update the Package List

If you vagrant up now you nothing is different than before we added the Puppet config. We still need to tell Puppet to do something.

The first thing we want to do on a new Linux install is update the package list. Open init.pp in an editor and enter the following code.

This is a basic Puppet block, in this case an execution block specified by exec.

Inside the block we give it a name update-package-list. The next line tells Puppet what command to execute, in this case sudo apt-get update.

Now run vagrant provision from the command-line. This checks for and applies changes to an already running Vagrant box.

If everything is correct you should see the following line in the Vagrant output.

notice: /Stage[main]//Exec[update-package-list]/returns: executed successfully

We now have Vagrant and Puppet talking to each other and we are ready to further automate the provisioning of our virtual machine.

In the next article we will use Puppet to install and setup the Nginx web server by creating a module.

Leave a Reply