Configuring Vagrant with JSON Instead of Ruby

thumb image-post

TL;DR - The Vagrantfile that's found in every Vagrant environment is nothing more than a Ruby script. Ruby can parse JSON (shocking, I know). For an example of how you can abstract your Vagrant environment's configuration into a standalone JSON file, see this GitHub repository.

I'm a big fan of Vagrant. One aspect of the platform that I don't particularly care for, though, is its reliance on Ruby for configuring virtual machines. Setting aside any personal preferences for or against Ruby, one thing is certain... Ruby is not as immediately understandable as something like JSON - and when it comes to configuration - that's exactly what I want... immediate "understandability," for lack of a better word.

For that reason, I've taken to setting up my Vagrant environments with a Vagrantfile that retrieves its settings from a separate JSON file, an example of which you can see below:

    "servers": {
        "my-server": {
            "guest_ip": "",
            "guest_hostname": "",
            "box": "trusty-webdev",
            "box_url": "",
            "memory": 2048,
            "cpus": 1,
            "paths": {
                "~/repos/project": "/opt/site"
            "timezone": "America/Chicago",
            "open_url": "",
            "scripts": [

With or without any Ruby experience, you can easily grok what's going on here.

Parsing JSON with Ruby

The Vagrantfile that comes with every Vagrant environment is nothing more than a Ruby script. An example of how you can parse a JSON file with Ruby is shown below:

require 'json'

if File.exists?(File.expand_path "./config.json")  
    settings = JSON.parse( "./config.json"))

Full Example

A more in-depth example that includes this, along with several other Vagrant hacks can be found on GitHub: