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 http://www.vagrantbox.es/ usually have It installed.

These are my rules to install Guest additions in CentOS.

# vm_create.yml
# Using virtualbox_version: '5.0.10'

---

  # https://wiki.centos.org/HowTos/Virtualization/VirtualBox/CentOSguest
- name: Install Guest addition dependencies
  sudo: yes
  yum: name={{ item }}
  with_items:
    - dkms
    - kernel-devel

- name: Download Guest additions
  sudo: yes
  get_url: |
    url=http://download.virtualbox.org/virtualbox/{{ virtualbox_version }}/VBoxGuestAdditions_{{ virtualbox_version }}.iso
    dest=/tmp
    mode=0440

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

- name: Install Guest additions
  sudo: yes
  command: /mnt/VBoxLinuxAdditions.run

- 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/id_rsa.pub'

- name: Vagrant user creation
  user: |
    name=vagrant
    createhome=yes

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

- name: Vagrant in sudoers
  lineinfile: |
    dest=/etc/sudoers
    state=present
    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 package.box --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:

127.0.0.1:2222

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.

# ...
config.vm.box = "myvagrantbox"

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

Javier Aguirre

Read more posts by this author.

comments powered by Disqus