Last Updated: February 25, 2016
·
2.543K
· rcarmo

Painless Vagrant Provisioning with HereDocs and bash

Even though Puppet is rather nice, I got fed up with the verbosity and need to context-switch between Ruby, Puppet and other languages for simple Vagrant box setups and decided to start using the shell provisioner.

Here's a small snippet (for Debian boxes) that will check if you've done an APT update recently and set up a Python development environment:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.box = "raring64"

    # config.vm.network :forwarded_port, guest: 80, host: 8080
    config.vm.provision :shell, :inline => <<END
# Check if we need to perform a weekly pkgcache update
touch -d '-1 week' /tmp/.limit
if [ /tmp/.limit -nt /var/cache/apt/pkgcache.bin ]; then
    sudo apt-get -y update
fi
rm /tmp/.limit

if [ ! -e /usr/bin/easy_install ]; then
    sudo apt-get -y install python-setuptools
fi

if [ ! -e /usr/local/bin/fab ]; then
    sudo apt-get -y install build-essential python-dev 
    sudo easy_install fabric
fi

if [ -e /vagrant/fabfile ]; then
    cd /vagrant
    fab provision
fi
END
end

I've set the above to run Fabric at the end since I use it for production deployments and it's quite handy for me, but the basics of updating and installing packages are all there.