2019-04-04 18:58:30 +02:00
|
|
|
Vagrant Guide
|
|
|
|
=============
|
2013-11-23 20:07:51 +01:00
|
|
|
|
|
|
|
.. _vagrant_intro:
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
````````````
|
|
|
|
|
2018-08-13 21:54:14 +02:00
|
|
|
`Vagrant <https://www.vagrantup.com/>`_ is a tool to manage virtual machine
|
2014-12-25 01:37:47 +01:00
|
|
|
environments, and allows you to configure and use reproducible work
|
|
|
|
environments on top of various 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
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
This guide will describe how to use Vagrant 1.7+ and Ansible together.
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-05-03 17:59:50 +02:00
|
|
|
If you're not familiar with Vagrant, you should visit `the documentation
|
2018-08-13 21:54:14 +02:00
|
|
|
<https://www.vagrantup.com/docs/>`_.
|
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.
|
2018-03-14 20:44:21 +01:00
|
|
|
Running from a Git checkout is fine. Follow the :ref:`installation_guide`
|
2013-11-23 23:59:35 +01:00
|
|
|
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
|
2014-12-25 01:37:47 +01:00
|
|
|
documentation, but here is a quick example that includes a section to use the
|
|
|
|
Ansible provisioner to manage a single machine:
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
.. code-block:: ruby
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2019-09-30 23:47:57 +02:00
|
|
|
# This guide is optimized for Vagrant 1.8 and above.
|
|
|
|
# Older versions of Vagrant put less info in the inventory they generate.
|
|
|
|
Vagrant.require_version ">= 1.8.0"
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
Vagrant.configure(2) do |config|
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2019-09-30 23:47:57 +02:00
|
|
|
config.vm.box = "ubuntu/bionic64"
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
config.vm.provision "ansible" do |ansible|
|
|
|
|
ansible.verbose = "v"
|
|
|
|
ansible.playbook = "playbook.yml"
|
2013-11-23 20:07:51 +01:00
|
|
|
end
|
2014-12-25 01:37:47 +01:00
|
|
|
end
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2013-11-23 23:59:35 +01:00
|
|
|
Notice the ``config.vm.provision`` section that refers to an Ansible playbook
|
2014-12-25 01:37:47 +01:00
|
|
|
called ``playbook.yml`` in the same directory as the ``Vagrantfile``. Vagrant
|
|
|
|
runs the provisioner once the virtual machine has booted and is ready for SSH
|
2013-11-23 20:10:43 +01:00
|
|
|
access.
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
There are a lot of Ansible options you can configure in your ``Vagrantfile``.
|
|
|
|
Visit the `Ansible Provisioner documentation
|
2018-08-13 21:54:14 +02:00
|
|
|
<https://www.vagrantup.com/docs/provisioning/ansible.html>`_ for more
|
2014-12-25 01:37:47 +01:00
|
|
|
information.
|
|
|
|
|
2013-11-23 20:07:51 +01:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ vagrant up
|
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
This will start the VM, and run the provisioning playbook (on the first VM
|
|
|
|
startup).
|
2013-11-23 23:59:35 +01:00
|
|
|
|
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
|
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
This will re-run the playbook against the existing VM.
|
|
|
|
|
|
|
|
Note that having the ``ansible.verbose`` option enabled will instruct Vagrant
|
|
|
|
to show the full ``ansible-playbook`` command used behind the scene, as
|
|
|
|
illustrated by this example:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2019-09-30 23:47:57 +02:00
|
|
|
$ PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit="default" --inventory-file=/home/someone/coding-in-a-project/.vagrant/provisioners/ansible/inventory -v playbook.yml
|
2014-12-25 01:37:47 +01:00
|
|
|
|
|
|
|
This information can be quite useful to debug integration issues and can also
|
|
|
|
be used to manually execute Ansible from a shell, as explained in the next
|
|
|
|
section.
|
2013-11-23 23:59:35 +01:00
|
|
|
|
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
|
2014-12-25 01:37:47 +01:00
|
|
|
faster than kicking ``vagrant provision`` and pretty easy to do.
|
2013-11-23 20:07:51 +01:00
|
|
|
|
2014-12-25 01:37:47 +01:00
|
|
|
With our ``Vagrantfile`` example, Vagrant automatically creates an Ansible
|
|
|
|
inventory file in ``.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory``.
|
|
|
|
This inventory is configured according to the SSH tunnel that Vagrant
|
|
|
|
automatically creates. A typical automatically-created inventory file for a
|
|
|
|
single machine environment may look something like this:
|
2013-11-23 20:07:51 +01:00
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
# Generated by Vagrant
|
|
|
|
|
2019-09-30 23:47:57 +02:00
|
|
|
default ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/home/someone/coding-in-a-project/.vagrant/machines/default/virtualbox/private_key'
|
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
|
2014-12-25 01:37:47 +01:00
|
|
|
``ansible`` or ``ansible-playbook`` commands the correct arguments, at least
|
2019-09-30 23:47:57 +02:00
|
|
|
for the *inventory*.
|
2013-11-23 20:07:51 +01:00
|
|
|
|
|
|
|
.. code-block:: bash
|
2015-02-02 13:30:17 +01:00
|
|
|
|
2019-09-30 23:47:57 +02:00
|
|
|
$ ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml
|
2014-12-25 01:37:47 +01:00
|
|
|
|
|
|
|
Advanced Usages
|
|
|
|
```````````````
|
|
|
|
|
|
|
|
The "Tips and Tricks" chapter of the `Ansible Provisioner documentation
|
2018-08-13 21:54:14 +02:00
|
|
|
<https://www.vagrantup.com/docs/provisioning/ansible.html>`_ provides detailed information about more advanced Ansible features like:
|
2014-12-25 01:37:47 +01:00
|
|
|
|
2019-09-26 16:18:29 +02:00
|
|
|
- how to execute a playbook in parallel within a multi-machine environment
|
2014-12-25 01:37:47 +01:00
|
|
|
- how to integrate a local ``ansible.cfg`` configuration file
|
2013-11-23 20:07:51 +01:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2018-07-21 15:48:47 +02:00
|
|
|
`Vagrant Home <https://www.vagrantup.com/>`_
|
2014-12-25 01:37:47 +01:00
|
|
|
The Vagrant homepage with downloads
|
2018-08-13 21:54:14 +02:00
|
|
|
`Vagrant Documentation <https://www.vagrantup.com/docs/>`_
|
2014-12-25 01:37:47 +01:00
|
|
|
Vagrant Documentation
|
2018-08-13 21:54:14 +02:00
|
|
|
`Ansible Provisioner <https://www.vagrantup.com/docs/provisioning/ansible.html>`_
|
2014-12-25 01:37:47 +01:00
|
|
|
The Vagrant documentation for the Ansible provisioner
|
2018-08-13 21:54:14 +02:00
|
|
|
`Vagrant Issue Tracker <https://github.com/hashicorp/vagrant/issues?q=is%3Aopen+is%3Aissue+label%3Aprovisioners%2Fansible>`_
|
2014-12-25 01:37:47 +01:00
|
|
|
The open issues for the Ansible provisioner in the Vagrant project
|
2018-03-14 20:44:21 +01:00
|
|
|
:ref:`working_with_playbooks`
|
2014-12-25 01:37:47 +01:00
|
|
|
An introduction to playbooks
|