zen

I'm a big fan of the DevOps attitude of "cattle" versus "pets": machines should be built in a repeatable, automated and consistent way. If there's something wrong, don't be afraid to replace a sick "cow" instead of trying to revive your "pet".

This Zen mindset also helps when preparing for demos, trainings and workshops: Usually I need a number of machines, and what better way than create them by using automation ? For that I'm using the tools Ansible, Packer, Vagrant and VirtualBox - they are all Open Source and can be used on a number of platforms (e.g. Windows, Linux and Mac OS X).

Ansible is a tool for managing systems and deploying applications, licensed under the GNU General Public License version 3 (my personal favorite).

Vagrant is a tool for managing virtual machines and is licensed under the MIT license.

VirtualBox is a virtualization environment for local use, licensed under the GNU General Public License version 2.

Packer creates a machine image by installing an operating system to a multitude of local and cloud platforms, for example VMWare, VirtualBox as well as Docker, Amazon EC2 and DigitalOcean. Packer is licensed under the Mozilla Public License Version 2.0.

How to solve the bootstrap problem and use Ansible to provision machines if you haven't got Ansible installed (yet) ?

This post contains the two (!) simple steps on how to set up a Vagrant Box containing an Ansible server, packaged as a Vagrant box. This allows you to use Ansible for provisioning all of your (future) (VirtualBox) installations.

Prerequisites

  • Make sure that the following tools are installed, up-to-date and can be executed from the command line.

    Vagrant : https://www.vagrantup.com (to configure and spin up virtual machines)

    VirtualBox : https://www.virtualbox.org/wiki/Downloads (the virtualization environment)

  • Start your favorite shell (e.g. zsh or Bash), and verify that the tools are working:

    vagrant version && VBoxManage --version && echo "Hey Ho Let's Go"
    
  • Clone the repository containing the necessary Ansible and Vagrant files:

    git clone https://github.com/PeterMosmans/easy-provisioning && pushd
    easy-provisioning
    

1a: Build and provision Ansible on Debian

Before you start, if you want to add your own user account and public SSH key to the Ansible server, create the file

ansible/group_vars/personal.yml
- there is an example called ansible/group_vars/personal-example.yml in the repository (see for more information the corresponding Ansible Galaxy role, bootstrap).

The following one-liner will spin up a Debian box named

bootstrap
, install Ansible on it, and provision it for use with VirtualBox. It will also compact the box, and shut it down:

pushd vagrant/ansible-server && VAGRANT_VAGRANTFILE=Vagrantfile.bootstrap
vagrant up

What happens ? By using the

VAGRANT_VAGRANTFILE
environment variable, an alternative Vagrantfile is specified,
Vagrantfile.bootstrap
. This file contains parameters that will download a stock Debian installation, installs the pip version of Ansible, installs the Ansible Galaxy roles specified in
ansible/requirements.yml
, runs the playbook
ansible/ansible-server.yml
, and runs a script to compact the box (that was installed by one of the Ansible roles).

1b: Package the Ansible server

Use the following one-liner command to package the Ansible server as a new Vagrantbox, and add it to the local catalog, ready to be used:
VAGRANT_VAGRANTFILE=Vagrantfile.bootstrap vagrant package bootstrap --output
ansible-server.box && vagrant box add ansible-server.box --name
ansible-server

That's all there is to it! Now, you can spin up the Ansible server box anytime using the command

vagrant up

in the

vagrant/ansible-server
directory. The directory
/ansible
in the repository is mapped on the VirtualBox to
/etc/ansible
, so the data persists across Vagrant 'reboots'.

Optionally you can shut down and remove the bootstrap box (as it's already imported) using
VAGRANT_VAGRANTFILE=Vagrantfile.bootstrap vagrant destroy && rm
ansible-server.box

Enjoy the Zen and power of Ansible...


Comments

comments powered by Disqus