Do you and your team ever need to all work in the exact same development environment? Is your new cutting edge web app dependant on a complex server configuration that takes you hours to setup? Do you find yourself constantly repeating yourself with server setups, no matter how easy? Or are you just curious about new technologies that can benefit your development process?

In this blog post I will show you what Vagrant and Ansible are, why you might want to use them and how to use them effectively.

What are Vagrant and Ansible?

Vagrant

­Vagrant is a tool used for creating lightweight, reproducible and portable development environments. Vagrant uses VirtualBox to host its virtual machines because VirtualBox is free, available on every major platform and is already the standard for using virtual machines.

Vagrant have a very wide variety of default server configurations or “boxes” available on  Atlas, including Ubuntu, Laravel and Debian. These “boxes” are used as pre-packaged operating systems to be used in your virtual machine. This means you don’t need to go through the process of setting up your operating system as it is already pre-configured with the default settings.

Ansible

Ansible is a tool used to provision servers, or virtual machines. Ansible uses something called playbooks to know how and what to provision servers with. This can go from making a simple web server from an Ubuntu virtual machine to a fully custom web tool on a deployed server.

If you have ever configured and installed a web server from scratch before you probably know while it’s not a hard task it can take around 30-40 minutes per server. If you need to do multiple servers everyday this can be a real chore. That’s where Ansible steps in, you write your commands in to your playbook using YAML, define your servers and their SSH keys in your inventory file, type “ansible-playbook [playbook_name].yml” and you’re done. This will go through each command for every server and execute. This can take setting up a single server down from 30-40 minutes to 2-3 minutes. Plus, it’s all automated so you can go and grab a cuppa while Ansible works its magic.

I think that Vault is one of the best Ansible features. It allows the user to save all of their sensitive information to a single file, and then securely encrypt that file while still being able to use it in playbooks. You can have multiple vault files in your playbooks, this a great way of compartmentalising your sensitive information. One drawback though is all the vault files used within one playbook must all have the same password to unlock, this removes a potential for an added layer of protection, making it a little easier for people to get all your sensitive information, rather than one file at a time.

 

What are the advantages?

What can you gain from using Vagrant and Ansible?

Being able to replicate your proposed production environment can be invaluable, there is nothing worse than having just spent the last few weeks making a beautiful website to find out that it doesn’t work on your production server, no clue why it isn’t working and your final launch deadline an hour away. It’s at that point you wish God would literally strike you down with a lightning bolt. Vagrant and Ansible remove this problem, from day one you can be working on an exact replica of your production server so no unforeseen problems creep in. Using Vagrant and Ansible also means your whole team can be working on their own exact duplicate of the environment on their machine as well.

Also if something changes in your environment such as a library or even something as major as the operating system you just need to change the code in your Vagrant file, push/share that file, then anyone else that is using it needs to check that code out and everyone will be on the same environment once again, easy, with no code repetition at all.

 

How we use Vagrant and Ansible

We use the Vagrant and Ansible combination on projects that benefit most from replicating the production environment.

One of the more intricate projects we are working on is a multi-tiered server architecture. This involves hosting different parts of a server on different servers and linking them all together. Normally when making web applications you will host everything on a single server but for large projects that will have a lot of users and require extra security having a multi-tier architecture can be beneficial. Using Vagrant and Ansible we can replicate having this sort of system on our work machines, meaning we don’t need to actually pay to host these servers while we are in development. This also gives us the added advantage of having more freedom to play around with the servers as if we break one on our local machine we can simply rebuild in a single command and not have it affect anyone in the process.

Another way we use Ansible is to provision our basic cloud webservers. As we use Digital Ocean as our server provider we have the option of when we are creating a server to have a completely blank server. This is a good idea if you want to know exactly what your server is running and have it setup optimally for how we want to work, this can take some time to do though. This is where Ansible comes in, all you need to do is: define your new server, make sure python is installed on it and run your Ansible script to install your Apache/Nginx server, PHP, MySQL and any other web technologies we might want. This makes setting up new server incredibly easy and makes sure they are also all setup in the same way keeping every consistent.

 

Alternatives

XAMPP and MAMP are both very similar products that setup a localhost on your machine, both use Apache, MySQL and PHP. The main difference is that MAMP has a pro version that has more settings and allows for more customization. Both will create a folder in the root of your hard drive with a htdocs folder inside where you can make your webapps. This is great if you don’t need to replicate your production environment and just need a basic webserver to dev on, but when it comes to needing to replicate a complex webserver these just simply aren’t the tools for the job. Our developers do use XAMPP for some local dev when sites don’t need a dedicated server. When web developing it certainly isn’t the case of one of the other, there is nothing stopping you from using both regularly, and choosing the best one for each project.

 

Conclusion

Vagrant and Ansible are powerful tools to have at your disposal and they can really benefit your development workflow. Since both tools are completely free why not test them out so you can consider them for your next project?