2013-11-23 20:07:51 +01:00
Using Vagrant and Ansible
=========================
.. _vagrant_intro:
Introduction
`` ` ` ` ` ` ` ` ` ``
2013-11-23 20:10:43 +01:00
Vagrant is a tool to manage virtual machine environments, and allows you to
2014-03-31 01:45:55 +02:00
configure and use reproducible work environments on top of various
2013-11-23 20:10:43 +01:00
virtualization and cloud platforms. It also has integration with Ansible as a
provisioner for these virtual machines, and the two tools work together well.
2013-11-23 20:07:51 +01:00
This guide will describe how to use Vagrant and Ansible together.
2014-05-03 17:59:50 +02:00
If you're not familiar with Vagrant, you should visit `the documentation
2013-11-23 20:10:43 +01:00
<http://docs.vagrantup.com/v2/> `_.
2013-11-23 20:07:51 +01:00
2013-11-23 23:59:35 +01:00
This guide assumes that you already have Ansible installed and working.
Running from a Git checkout is fine. Follow the :doc: `intro_installation`
guide for more information.
2013-11-23 20:07:51 +01:00
.. _vagrant_setup:
Vagrant Setup
`` ` ` ` ` ` ` ` ` ` ``
2013-11-23 23:59:35 +01:00
The first step once you've installed Vagrant is to create a `` Vagrantfile ``
and customize it to suit your needs. This is covered in detail in the Vagrant
2013-11-23 20:10:43 +01:00
documentation, but here is a quick example:
2013-11-23 20:07:51 +01:00
.. code-block :: bash
$ mkdir vagrant-test
$ cd vagrant-test
$ vagrant init precise32 http://files.vagrantup.com/precise32.box
2013-11-23 20:10:43 +01:00
This will create a file called Vagrantfile that you can edit to suit your
needs. The default Vagrantfile has a lot of comments. Here is a simplified
example that includes a section to use the Ansible provisioner:
2013-11-23 20:07:51 +01:00
.. code-block :: ruby
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise32"
config.vm.box_url = "http://files.vagrantup.com/precise32.box"
config.vm.network :public_network
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
2013-11-23 20:10:43 +01:00
The Vagrantfile has a lot of options, but these are the most important ones.
2013-11-23 23:59:35 +01:00
Notice the `` config.vm.provision `` section that refers to an Ansible playbook
called `` playbook.yml `` in the same directory as the Vagrantfile. Vagrant runs
2013-11-23 20:10:43 +01:00
the provisioner once the virtual machine has booted and is ready for SSH
access.
2013-11-23 20:07:51 +01:00
.. code-block :: bash
$ vagrant up
This will start the VM and run the provisioning playbook.
2013-11-23 23:59:35 +01:00
There are a lot of Ansible options you can configure in your Vagrantfile. Some
particularly useful options are `` ansible.extra_vars `` , `` ansible.sudo `` and
`` ansible.sudo_user `` , and `` ansible.host_key_checking `` which you can disable
to avoid SSH connection problems to new virtual machines.
2013-11-23 20:10:43 +01:00
Visit the `Ansible Provisioner documentation
<http://docs.vagrantup.com/v2/provisioning/ansible.html> `_ for more
information.
2013-11-23 20:07:51 +01:00
2013-11-23 23:59:35 +01:00
To re-run a playbook on an existing VM, just run:
.. code-block :: bash
$ vagrant provision
This will re-run the playbook.
2013-11-23 20:07:51 +01:00
.. _running_ansible:
Running Ansible Manually
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-11-23 23:59:35 +01:00
Sometimes you may want to run Ansible manually against the machines. This is
pretty easy to do.
2013-11-23 20:07:51 +01:00
2013-11-23 20:10:43 +01:00
Vagrant automatically creates an inventory file for each Vagrant machine in
2014-05-07 11:24:28 +02:00
the same directory located under `` .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory `` .
It configures the inventory file according to the SSH tunnel that Vagrant
2013-11-23 23:59:35 +01:00
automatically creates, and executes `` ansible-playbook `` with the correct
2013-11-23 20:10:43 +01:00
username and SSH key options to allow access. A typical automatically-created
inventory file may look something like this:
2013-11-23 20:07:51 +01:00
.. code-block :: none
# Generated by Vagrant
2015-09-10 16:11:47 +02:00
machine ansible_host=127.0.0.1 ansible_port=2222
2013-11-23 20:07:51 +01:00
2015-10-09 03:01:09 +02:00
.. note ::
2015-10-12 14:01:24 +02:00
Ansible 2.0 has deprecated the “ssh” from `` ansible_ssh_user `` , `` ansible_ssh_host `` , and `` ansible_ssh_port `` to become `` ansible_user `` , `` ansible_host `` , and `` ansible_port `` . If you are using a version of Ansible prior to 2.0, you should continue using the older style variables (`` ansible_ssh_* `` ). These shorter variables are ignored, without warning, in older versions of Ansible.
2015-10-09 03:01:09 +02:00
2013-11-23 20:10:43 +01:00
If you want to run Ansible manually, you will want to make sure to pass
2013-11-23 23:59:35 +01:00
`` ansible `` or `` ansible-playbook `` commands the correct arguments for the
2015-02-02 13:30:17 +01:00
username (usually `` vagrant `` ) and the SSH key (since Vagrant 1.7.0, this will be something like
`` .vagrant/machines/[machine name]/[provider]/private_key `` ), and the autogenerated inventory file.
2013-11-23 20:07:51 +01:00
Here is an example:
.. code-block :: bash
2015-02-02 13:30:17 +01:00
$ ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=.vagrant/machines/default/virtualbox/private_key -u vagrant playbook.yml
Note: Vagrant versions prior to 1.7.0 will use the private key located at `` ~/.vagrant.d/insecure_private_key. ``
2013-11-23 20:07:51 +01:00
.. seealso ::
`Vagrant Home <http://www.vagrantup.com/> `_
The Vagrant homepage with downloads
`Vagrant Documentation <http://docs.vagrantup.com/v2/> `_
Vagrant Documentation
`Ansible Provisioner <http://docs.vagrantup.com/v2/provisioning/ansible.html> `_
The Vagrant documentation for the Ansible provisioner
:doc: `playbooks`
An introduction to playbooks