Using Ansible and Vagrant to Launch Ubuntu 16.04

I'm just getting started with Vagrant. So far I'm very impressed.

I'm also just getting started with Ansible. Also, very impressed.

Vagrant has this handy feature called provisioning that will set up a box just the way you want it when it first starts up. And it includes Ansible as one of the ways to provision a box. Let me tell ya, I keep being impressed.

This works great for the Ubuntu 14.04 box (ubuntu/trusty64 on Atlas) that I was provisioning. Getting a trusty64 box up and running is as easy as:

  1. vagrant up
  2. Ready to go!

Then I tried to do the same thing on an Ubuntu 16.04 box (ubuntu/xenial64 on Atlas). But the provisioning script would always fail because Python – a requirement for Ansible – is not installed on the xenial64 box. So I ended up having to manually install Python before provisioning the box. So many more annoying little steps!

  1. vagrant up. Wait for it to tell me provisioning failed. I know, alright!
  2. vagrant ssh
  3. sudo apt-get install python. Confirm yes this is what I want to do. Wait a while.
  4. exit
  5. vagrant provision
  6. Ready to go!

It turns out, there's a solution! Vagrant lets you specify multiple provision commands in one Vagrantfile, so Vagrant can be told to install Python first and then use the Ansible script.

Vagrant.configure("2") do |config|
  # All of the other stuff
 
  # First, install python
  config.vm.provision "shell" do |s|
    s.inline = "apt-get install -y python"
  end
 
  # Then run the ansible script
  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.playbook = "/home/bryan/..."
  end
end

That's handy! Now Vagrant will handle booting and provisiong a xenial64 box as nicely as a trusty64 box.

  1. vagrant up
  2. Ready to go!