Tag Archives: vagrant

Allow per-developer Vagrantfile customisations

If you are sharing a Vagrant file across a team, for example, for configuring a local development environment you’ll know that sometimes individual developers want their own VM tweaks. For example, they may have a machine with more memory that they’d like to dedicate to the VM. In order to support this, you can add a config file for custom changes:

Example Vagrantfile showing custom memory and CPU cap:

# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'

if File.exists?('vagrant.yml')
  settings = YAML.load_file 'vagrant.yml'

memory_use    = defined?(settings['memory']) ? settings['memory'] : 1024
cpu_execution = defined?(settings['cpucap']) ? settings['cpucap'] : 50

Vagrant.configure(2) do |config|
  config.vm.box = "puphpet/centos65-x64"
  config.vm.network "private_network", ip: ""

  config.vm.provider "virtualbox" do |vb|
    vb.memory = memory_use
    vb.customize ["modifyvm", :id, "--cpuexecutioncap", cpu_execution]

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provision/playbook.yml"
    ansible.become = true
    ansible.raw_arguments = Shellwords.shellsplit(ENV['ANSIBLE_ARGS']) if ENV['ANSIBLE_ARGS']
    ansible.compatibility_mode = "1.8"

In this example, we’ve specified two configurable settings — memory_use and cpu_execution. Values will be read from the settings file, if it exists, otherwise default values are set (lines 9-10). Memory is specified in megabytes, and CPU execution is the maximum allowed percentage of the hosts CPU usage.

Example vagrant.yml file:

memory: 4096
cpucap: 75

Don’t forget to add the vagrant.yml file to your .*ignore file to make sure it isn’t committed to your source repo.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me: