Using Vagrant and Ansible

Introduction

Vagrant is a tool to manage virtual machineenvironments, and allows you to configure and use reproducible workenvironments on top of various virtualization and cloud platforms.It also has integration with Ansible as a provisioner for these virtualmachines, and the two tools work together well.

This guide will describe how to use Vagrant 1.7+ and Ansible together.

If you’re not familiar with Vagrant, you should visit the documentation.

This guide assumes that you already have Ansible installed and working.Running from a Git checkout is fine. Follow the Installation Guideguide for more information.

Vagrant Setup

The first step once you’ve installed Vagrant is to create a Vagrantfileand customize it to suit your needs. This is covered in detail in the Vagrantdocumentation, but here is a quick example that includes a section to use theAnsible provisioner to manage a single machine:

  1. # This guide is optimized for Vagrant 1.7 and above.
  2. # Although versions 1.6.x should behave very similarly, it is recommended
  3. # to upgrade instead of disabling the requirement below.
  4. Vagrant.require_version ">= 1.7.0"
  5.  
  6. Vagrant.configure(2) do |config|
  7.  
  8. config.vm.box = "ubuntu/trusty64"
  9.  
  10. # Disable the new default behavior introduced in Vagrant 1.7, to
  11. # ensure that all Vagrant machines will use the same SSH key pair.
  12. # See https://github.com/hashicorp/vagrant/issues/5005
  13. config.ssh.insert_key = false
  14.  
  15. config.vm.provision "ansible" do |ansible|
  16. ansible.verbose = "v"
  17. ansible.playbook = "playbook.yml"
  18. end
  19. end

Notice the config.vm.provision section that refers to an Ansible playbookcalled playbook.yml in the same directory as the Vagrantfile. Vagrantruns the provisioner once the virtual machine has booted and is ready for SSHaccess.

There are a lot of Ansible options you can configure in your Vagrantfile.Visit the Ansible Provisioner documentation for moreinformation.

  1. $ vagrant up

This will start the VM, and run the provisioning playbook (on the first VMstartup).

To re-run a playbook on an existing VM, just run:

  1. $ vagrant provision

This will re-run the playbook against the existing VM.

Note that having the ansible.verbose option enabled will instruct Vagrantto show the full ansible-playbook command used behind the scene, asillustrated by this example:

  1. $ PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --private-key=/home/someone/.vagrant.d/insecure_private_key --user=vagrant --connection=ssh --limit='machine1' --inventory-file=/home/someone/coding-in-a-project/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

This information can be quite useful to debug integration issues and can alsobe used to manually execute Ansible from a shell, as explained in the nextsection.

Running Ansible Manually

Sometimes you may want to run Ansible manually against the machines. This isfaster than kicking vagrant provision and pretty easy to do.

With our Vagrantfile example, Vagrant automatically creates an Ansibleinventory file in .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.This inventory is configured according to the SSH tunnel that Vagrantautomatically creates. A typical automatically-created inventory file for asingle machine environment may look something like this:

  1. # Generated by Vagrant
  2.  
  3. default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

If you want to run Ansible manually, you will want to make sure to passansible or ansible-playbook commands the correct arguments, at leastfor the username, the SSH private key and the inventory.

Here is an example using the Vagrant global insecure key (config.ssh.insert_keymust be set to false in your Vagrantfile):

  1. $ ansible-playbook --private-key=~/.vagrant.d/insecure_private_key -u vagrant -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

Here is a second example using the random private key that Vagrant 1.7+automatically configures for each new VM (each key is stored in a path like.vagrant/machines/[machine name]/[provider]/private_key):

  1. $ ansible-playbook --private-key=.vagrant/machines/default/virtualbox/private_key -u vagrant -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

Advanced Usages

The “Tips and Tricks” chapter of the Ansible Provisioner documentation provides detailed information about more advanced Ansible features like:

  • how to parallely execute a playbook in a multi-machine environment
  • how to integrate a local ansible.cfg configuration file

See also