If you are developing infrastructure-as-code using Puppet modules or Chef cookbooks then you must checkout Vagrant.
Vagrant has been around for a few years but is today, one of the DevOps favourite tools for automated provisioning of development environments or, for testing infrastructure-as-code, before it gets pushed to production environments.
In this example we’ll be focusing on using Puppet as the provisioner.
It uses puppet apply, saving the need of having to setup a puppetmaster and certificates. (If you prefer, you can provision with a puppetmaster).
Installation
Vagrant uses VirtualBox for provisioning VMs so firstly you need to install it:
1 |
sudo apt-get install virtualbox |
Then go to http://downloads.vagrantup.com to download and install the latest version of Vagrant.
1 2 |
wget http://files.vagrantup.com/packages/7ec0ee1d00a916f80b109a298bab08e391945243/vagrant_1.2.7_x86_64.deb sudo dpkg -i vagrant_1.2.7_x86_64.deb |
Adding a box
Next you need to import a “box” which is a ready to use base image of your favourite Linux distribution; copy the url of a box at http://www.vagrantbox.es or (my preference) use Canonical Ubuntu 12.04 LTS official box. You must also give the box a base name.
1 |
vagrant box add 'precise64' http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box |
Configuration
Next prepare an initial vagrant configuration:
1 2 3 |
mkdir ~/vagrant cd ~/vagrant vagrant init |
This creates a configuration file named Vagrantfile, open it with your favourite editor
1 2 3 4 5 6 7 8 |
Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.provision :puppet do |puppet| puppet.module_path = "/etc/puppet/modules" puppet.options = "--verbose" end end |
This is the minimal required to get vagrant to boot an Ubuntu 12.04 LTS VM and provision it with Puppet.
By default the provisioner will apply Puppet manifests declared inside manifests/default.pp So create manifests folder and default.pp manifest file
1 |
mkdir ~/vagrant/manifests/ |
In this example we’re going to install apache and setup a VirtualHost using the apache module from sourceforge which can be installed this way:
1 |
sudo puppet module install puppetlabs-apache |
Inside ~/vagrant/manifests/default.pp include the apache module
1 2 |
include apache apache::vhost { 'blog.bluemalkin.net': port => '80', docroot => '/var/www/blog'} |
Boot the VM
Now we’re ready to boot the VM using vagrant up
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
~/vagrant$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'precise'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- /vagrant [default] -- /tmp/vagrant-puppet/manifests [default] -- /tmp/vagrant-puppet/modules-0 [default] Running provisioner: puppet... Running Puppet with default.pp... |
The VM is created within a minute! Then you’ll start to see the verbose output of apache and the vhost being installed.
ssh into the box
1 |
vagrant ssh |
Perform some checks:
1 2 3 |
vagrant@vagrant-ubuntu-precise-64:~$ which apache2 /usr/sbin/apache2 vagrant@vagrant-ubuntu-precise-64:~$ cat /etc/apache2/sites-enabled/25-blog.bluemalkin.net.conf |
All good!
Other Vagrant commands
1 |
vagrant suspend |
Stops the execution of the VM and saves it’s state.
1 |
vagrant resume |
Boots up the VM after a suspend.
1 |
vagrant destroy |
Destroys the VM.
1 |
vagrant provision |
Re-executes the Puppet or Chef provisioner.
This is probably what you will be running the most when you’re developing modules/cookbooks.
1 |
vagrant reload |
Shutdowns then starts the VM. If you change your path to Puppet module or manifests you will need to do a reload or destroy/up those two paths are mounted into the box using NFS.
AWS provisioner
Vagrant can also be used to provision AWS instances instead of local virtualbox VMs.
Install the plugin:
1 |
sudo vagrant plugin install vagrant-aws |
Next you will need to create a dummy box, set your AWS credentials, choose an AMI etc…follow the steps explained in the plugin Quick Start at https://github.com/mitchellh/vagrant-aws
Conclusion
Vagrant is a great tool for quickly testing deployments. Vagrant files can be customised to do many things, checkout the official documentation at http://docs.vagrantup.com/v2/ to see what all the options are.
Thank you for sharing this list.
Oh my goodness! Incredible article dude! Many thanks.
Good thing about your post is that you put a lot of effort for explanation,it was a excellent post.
I have learn some just right stuff here. Certainly price bookmarking for revisiting.
I surprise how much attempt you set to make this type of magnificent informative website.