Skip to content

Commit

Permalink
Change vagrant-multi-machine to use the Vai provisioner.
Browse files Browse the repository at this point in the history
This one is much more capable then the official Ansible provisioner.
  • Loading branch information
htgoebel committed Feb 27, 2015
1 parent c28a00f commit e84c950
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 57 deletions.
6 changes: 1 addition & 5 deletions vagrant-multi-machine/.debops.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
[ansible defaults]
private_key_file = ~/.vagrant.d/insecure_private_key
remote_user = vagrant
host_key_checking = False

#
80 changes: 57 additions & 23 deletions vagrant-multi-machine/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ Using Debops with a multi-machine Vagrantfile
=====================================================

This project is an example for using Debops with a multi-machine
Vagrantfile. It uses the Vagrant Ansible provider only to generate the
inventory-file. Provision is then actually done using `debops` as
Vagrantfile. It uses the Vagrant provisioner plugin Vai_ to generate
the inventory-file. Provision is then actually done using `debops` as
usual.

The Vagrant Ansible provisioner is not used at all, because Vai does a
much better job for our needs. (A former version of this example used
the Vagrant Ansible provisioner, but have been much more complicated, see
below.)


Requirements
==============
Expand All @@ -20,37 +25,40 @@ Requirements
Quick Start
===========

* Install the Vagrant provisioning plugin Vai_::

vagrant plugin install vai

* Fire up Vagrant: ``vagrant up``

This will create two virtual machine `web` and `db` and run a dummy
playbook on it. This playbook will do nothing, but is required to
make Vagrant generate the inventory file.
This will create two virtual machine `web` and `db` and generate the
inventory file in ``ansible/inventory``.

In addition, the Vagrantfile creates a symlink from the
auto-generated inventory-file by Vagrant to your
``ansible/inventory``.
* Run::

* Run ``debops ./simpletest.yml``
ANSIBLE_SSH_ARGS="-o UserKnownHostsFile=/dev/null" debops ./simpletest.yml

This will run a simple playbook testing if files have been found and
variables have been set up as correctly as expected. You should get
"okay" for all tasks.

Now you can use debops as usual. It will automatically include the
host definitions auto-generated by vagrant. If for some reason debops
resp. Ansible does not find the inventory, you may safely run ``vagrant
provision`` to regenerate it.
Please note: Using this ``ANSIBLE_SSH_ARGS=...`` is optional. But it
avoids cluttering your known_hosts with keys of your ever-changing
vagrant VMs. *Absolutely do not use this for your production servers!*

Now you can use debops as usual (mind ``ANSIBLE_SSH_ARGS=...`` :-). It
will automatically include the host definitions auto-generated by
vagrant. If for some reason debops resp. Ansible does not find the
inventory, you may safely run ``vagrant provision`` to regenerate it.


How it works
==============

In addtion to the symlink to the generated inventory file, this
example makes use of both :file:`.debops.cfg` and the
``ansible_config_hook``. The former will define the hook and the hook
will add some requried configuration to ``ansible.cfg``. Required
options are the ssh-user, the ssh-key-file and (optionally) swicthing
of ssh host-key-checking.
The `Vai` provisioner generates a inventory file which is setting up
host, port, private key file and the remote user as required by
Vagrant. Ansible resp. debops will read the information an know how to
connect to the Vagrant VM.



Expand All @@ -64,8 +72,12 @@ In short:

* Set up your host- and group-vars in ``ansible/inventory`` as usual.

* Define your groups in ``ansible/inventory/groups`` (the actual name
of the file does not matter).
* Define your groups in the Vagrantfile and/or in an inventory-file
(e.g. ``ansible/inventory/groups``, the actual name of the file does
not matter).

In this example we do both: ``secondGroup`` is defined using the
`Vagrantfile` and ``firstGroup`` is defined in an inventory-file.

* Add more machines to the ``Vagrantfile`` as you need.

Expand All @@ -74,8 +86,27 @@ In short:



Reasoning for using this way
===============================
Why using `Vai` instead of the `Ansible` provisioner
=====================================================

A former version of this example used the official Vagrant Ansible
provisioner. But this has be much more complicated, inflexible and
insecure.

The Vagrant Ansible provider was only used to generate the
inventory-file. But this inventory-file did not contain all required
information, so the remote user and the private key file have had been
defined in :file:`.debops.cfg`.

Additionally the Vagrant file was required to contain code for
creating a dummy playbook (which the Ansible provisioner requires) and
sym-linking the inventory-file into the inventory.

So as `Vai` came up, we decided to switch to it.


Reasoning for why we formerly did it that way
----------------------------------------------

We tried a lot off different setups, but the only one working
reasonable is to *not* provision from within the Vagrantfile, but use
Expand Down Expand Up @@ -140,6 +171,9 @@ Please note that when using a hand-crafted inventory. you will have to
take care of the actual configuration of the machines changing. E.g.
IP-ports may change if other machines are running, too.


.. _Vai: https://github.com/MatthewMi11er/vai

..
Local Variables:
mode: rst
Expand Down
34 changes: 8 additions & 26 deletions vagrant-multi-machine/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,7 @@ HOSTS = {
"db" => [NETWORK+"11", RAM, GUI, BOX],
}

ANSIBLE_INVENTORY = 'ansible/inventory'

# ---- Custom commands run on the main host ----

# Create a dummy playbook to both get the auto-generated inventory and
# keep `ansible-playbook` happy.
ANSIBLE_DUMMY_PLAYBOOK = ".vagrant/provisioners/ansible/dummy.yml"
require 'pathname'
pb = Pathname.new(ANSIBLE_DUMMY_PLAYBOOK)
pb.parent.mkpath()
pb.open('w') do |f1|
f1.puts '---'
f1.puts '- hosts: "!all"'
end

# Symlink auto-generated inventory-file to inventory
aiv = Pathname.new(ANSIBLE_INVENTORY)
viv = Pathname.new('.vagrant/provisioners/ansible/inventory')
aiv.mkpath()
name = 'vagrant_ansible_inventory'
target = aiv+name
target.make_symlink((viv+name).relative_path_from(aiv)) unless target.symlink?()
ANSIBLE_INVENTORY_DIR = 'ansible/inventory'

# ---- Vagrant configuration ----

Expand All @@ -66,10 +45,13 @@ Vagrant.configure(2) do |config|
end
end # HOSTS-each

config.vm.provision "ansible" do |ansible|
ansible.host_key_checking = "false"
ansible.playbook = ANSIBLE_DUMMY_PLAYBOOK
ansible.raw_arguments = ['--syntax-check']
config.vm.provision "vai" do |ansible|
ansible.inventory_dir=ANSIBLE_INVENTORY_DIR
# optional: add a group listing all vagrant machines
ansible.groups = {
'secondGroup' => [ "db" ],
# '_provided_by_vagrant_'=> HOSTS.keys,
}
end

end
3 changes: 0 additions & 3 deletions vagrant-multi-machine/ansible/inventory/groups
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[firstGroup]
web

[secondGroup]
db

0 comments on commit e84c950

Please sign in to comment.