Testing Ansibile Playbooks with Vagrant

I’ve been interested in Ansible for a long time now, and thanks to my coworker’s expertise, I’ve been able to get my feet wet. My main barrier has been finding a way to run playbooks.

  • For my first attempt at learning Ansible, I tried running it locally using `ansible_connection=local`. But I found that running it on a system where changes persisted made it hard to trace what was going on. The final straw was trying to locate guides and resources about how to use Ansible was so difficult. You need documentation for how to read the documentation.
  • For my second attempt at learning Ansible, I tried running Vagrant in Ubuntu 14.04, but for some reason, it would not even install.
  • For my third attempt at learning Ansible, I tried running it against a Docker container, but the lack of ssh and pid 1 made re-creating the full experience too difficult. I found that I could get 80% of the way there using tricks from http://phusion.github.io/baseimage-docker/ and https://docs.docker.com/examples/running_ssh_service/
  • For my fourth attempt at learning Ansible, I tried getting Vagrant up and running on my Windows machine, and it worked! It also started working on my Ubuntu machine too.

Then I started following reading: docs.ansible.com/guide_vagrant.html, but modified it for my own purposes. I got this working in Windows first, but everything here works exactly the same in Linux and OSX too.

I made a few minor modifications I’ll go over now. I changed the base box to be similar to what we use in production and to one that wasn’t two years old. In my case, trusty64:

$ vagrant init ubuntu/trusty64

Then, I adjusted the Vagrantfile to use a shell provisioner. Here’s a mine (with comments removed):

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.hostname = "ansible-test"
  config.vm.box_check_update = false
  config.vm.network "public_network"
  config.vm.provision :shell, :privileged => false, :path => 'vagrant-install.sh'

And this is contents of vagrant-install.sh referenced above:

echo `whoami`
sudo apt-get update -qq
sudo apt-get install -y avahi-daemon

Using avahi/zeroconf gives me a hostname on my local network so I can use a predictable and human friendly hostname.

Let’s get started!

$ vagrant up

And now let’s run our playbook, jenkins.yml

$ ansible-playbook --private-key=~/.vagrant.d/insecure_private_key -u vagrant jenkins.yml

To get it to connect to ansible-test.local, I hacked my  /etc/ansible/hosts file according to these instructions: docs.ansible.com/intro_inventory.html#hosts-and-groups

So how do I feel about Ansible? It’s meh. I still hate it, but I hate it less than Chef and Puppet. I hate them all so much.

Let the hate flow through you


Leave a Reply

Your email address will not be published. Required fields are marked *