Using Vagrant and Ansible

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

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

    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.

    The first step once you’ve installed Vagrant is to create a and 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:

    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.

    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:

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

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

      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.

      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:

      If you want to run Ansible manually, you will want to make sure to pass 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):

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

      • Vagrant Home
      • The Vagrant homepage with downloads
      • Vagrant Documentation
      • The Vagrant documentation for the Ansible provisioner
      • Vagrant Issue Tracker
      • The open issues for the Ansible provisioner in the Vagrant project
      • An introduction to playbooks