From OVA to a ready to develop Vagrant machine with Ansible

At my new project there are very well separated technical departments (as in any medium/big company). Instead of installing everything from scratch in a Vagrant base Ubuntu box as I usually do I received an OVA (Open Virtual Appliance) with CentOS and a ready to use Apache and Drupal installation.

Since I'm not gonna be in charge of the systems this time but I still need to install my own dependencies for development, I decided to convert the OVA into a Vagrant box using Ansible. Here's my solution.

To start we need import the OVA in Virtualbox.

We are gonna create Ansible tasks to make the process automatic. We need to follow Vagrant instructions to create the vagrant user. If you are gonna use Vagrant to share folders with config.vm.synced_folders, you might need to install the Guest additions in case the box doesn't have It already, in my case It didn't, but boxes at usually have It installed.

These are my rules to install Guest additions in CentOS.

# vm_create.yml
# Using virtualbox_version: '5.0.10'


- name: Install Guest addition dependencies
  sudo: yes
  yum: name={{ item }}
    - dkms
    - kernel-devel

- name: Download Guest additions
  sudo: yes
  get_url: |
    url={{ virtualbox_version }}/VBoxGuestAdditions_{{ virtualbox_version }}.iso

- name: Mount Guest additions
  sudo: yes
  mount: |
    src=/tmp/VBoxGuestAdditions_{{ virtualbox_version }}.iso

- name: Install Guest additions
  sudo: yes
  command: /mnt/

- name: Umount Guest additions
  sudo: yes
  command: umount /mnt

- name: Remove Guest additions
  sudo: yes
  command: rm VBoxGuestAdditions_{{ virtualbox_version }}.iso

After we could create the vagrant user (following in the vm_create.yml file).

# vm_create.yml
# Here vagrant_authorized_key_path: '/home/javaguirre/.ssh/'

- name: Vagrant user creation
  user: |

- name: Vagrant user authorized key
  authorized_key: |
    key="{{ lookup('file', vagrant_authorized_key_path) }}"

- name: Vagrant in sudoers
  lineinfile: |
    inserafter='^root    ALL=(ALL)       ALL'
    line='vagrant ALL=(ALL)       NOPASSWD:ALL'

We create the vagrant user with a home and add a ssh key to authorized_keys. We give the vagrant user sudo powers without the need of a password, very handy for development and only for development. :-)

After that the process is simple:

ansible-playbook --inventory-file=host vm_create.yml
vagrant package --base myvirtualmachine
vagrant box add --name myvagrantbox

Our host file would have the configuration to connect to our virtualbox machine, you can check the Ansible documentation for this, but It could be something like:

The first command deploys the Ansible rules from in vm_create.yml into the virtual machine myvirtualmachine which has to be running.

The second command creates a Vagrant box out of the virtual machine with the name myvagrantbox.

The third command adds the box we created on the previous command into your vagrant boxes locally. You can check It's added and ready to be used with the vagrant box list command, should be listed.

Then in your Vagrantfile you could just add this box.

# ... = "myvagrantbox"

We can start hacking on our new Vagrant environment. :-)

Javier Aguirre

Read more posts by this author.

comments powered by Disqus