eZ Community » Blogs » Betsy Gamrat » VirtualBox virtual machines with...

By

Betsy Gamrat

VirtualBox virtual machines with Vagrant and Ansible for website development

Tuesday 22 September 2015 4:09:13 pm

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Vagrant and Ansible are tools to efficiently provision virtual machines (also called VMs or simply boxes).

This blog post will begin with a short discussion of why one would invest the time to use these tools, then cover the required software, an overview of how Vagrant works with VirtualBox and the use of Ansible to provision a VM.

Why should you use VMs for website development?

The traditional ways that developers work on websites are on remote development machines or locally, directly on their main operating system. There are many advantages to using VMs for development, including the following:

  • The entire development team can have the same server and configuration, without purchasing additional hardware
  • Their local VMs can be more representative of the production servers
  • Bring up the VM when you need to work with it and shut it down when you're done. This is especially helpful if you need different versions of software such as PHP for different projects.

Vagrant and Ansible help to automate the provisioning of the virtual machines. Vagrant handles the starting and stopping of the machines as well as some configuration, and Ansible breaks down the details of the machines into easy-to-read configuration files and installs and configures the software within the VMs.

General approach

We usually keep the site code on the host operating system and share these files from the host operating system to the virtual machines. This way, the VMs can be loaded with only the software required to run the site, and each team member can use their favourite local tools for code editing, version control, and more under the OS they use most often.

Risks

This scheme is not without risk and complexity. A VM is a great tool for your collection, but like any tool, you will need to take some time to learn how to use it.

  • When Vagrant, Ansible, and VirtualBox are running well together, they help development to run more efficiently and can improve the quality of your work. However, when things go wrong they can distract from actual web development. They represent additional tools that you need to maintain and troubleshoot, and you need to train and support your development team to use them properly.
  • Host operating system: Be sure your host OS supports the tools you're planning to use. As I mentioned, this blog post focusses on Ansible. Ansible is not officially supported on Windows as the host. That means if you only have a Windows machine to work with, you'll need to consider using a Linux OS as the controller. (There are some tricks to get it to work on Windows.)
  • Performance: Remember the intent of these VMs is to support development. They will not run as fast as standalone servers. If that is an issue, you will likely need to invest some time in improving the performance.
  • An implicit assumption is made that only one instance of the VM will be running on the host at any given time. If you will be using more than one instance of the VM, you'll need to take that into account as you set it up.

Getting started

The first step is to install the required software: VagrantAnsible, and VirtualBox. We will focus only on VirtualBox in this post, but you can also use VMWare or one of several other providers.

You will likely need some VirtualBox extensions and Vagrant plugins as well. Take the time to read the documentation carefully.

Then, you will need a starting point for your virtual server, typically called a "base box". For your first VM, it is easiest to use an existing box. There are many boxes up at HashiCorp's Atlas and at Vagrantbox.es.

Once you've chosen your box, these commands should bring it to life:

$ vagrant box add name-of-box url-of-box
$ vagrant init name-of-box
$ vagrant up

Provisioning, customizing, and accessing the VM

Once the box is up and running, you can start adding software to it using Ansible. Plan to spend a lot of time learning Ansible. It is well worth the investment. You'll use Ansible to load the system software, create databases, configure the server, create users, set file ownership and permissions, set up services, and much more -- basically to configure the virtual machine to include everything you need. Once you have the Ansible scripts set up you will be able to re-use them with different base boxes / VMs and also run them against remote servers (which is a topic for another day!).

The easiest way to SSH into the box is:

$ vagrant ssh

You may update your /etc/hosts file to map the VM box IP address to an easy to remember name for SSH and browser access. Once the box is running and serving pages, you can start working on the site.

Development workflow

Using a VM as described here doesn't significantly change normal development workflows when it comes to editing code. The host machine and VM share the application files through the path configured under Vagrant. You can edit the files using your code editor of choice on the host, or make minor adjustments with a text editor on the VM. You can also keep version control tools on the host machine. In other words, all of the code modifications made on the host machine automatically show up on the virtual machine.

All going well, you get the convenience of local development and an environment that mimics the server(s)!

Proudly Developed with from