Notes: Vagrant box with provisioning

DISCLAIMER: this is not meant to be coherent post. Sorry. Just notes for future reference.

First of all - simple vagrantfile with some of my standard choices.

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

Vagrant.configure(2) do |config|
    config.vm.box = "centos/7"
    config.vm.synced_folder ".", "/vagrant", type: "rsync"
    config.vm.hostname = "builder-#{rand(100...999)}"
    config.vm.provider "libvirt" do |vb|
        vb.memory = 2048
        vb.cpus = 4
    end
    config.vm.provision "shell", inline: "sudo yum install -y epel-release"
    config.vm.provision "shell", inline: "sudo yum update -y"

For the sake of simplicity I am using the inline version of shell provisioner. Plus, you can clearly see (without opening any other file) what is going to happen when you run vagrant up.

When there is more work to do I like to use Ansible as provisioner. This makes it easier for more complex setups, but there is always option for using a script file (also inline!) for the shell provisioner.

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

$script = <<-SCRIPT
echo -n "Update the box..."
sudo yum install -y epel-release &> /dev/null
sudo yum update -y &>/dev/null
echo "Done!"
SCRIPT

Vagrant.configure(2) do |config|
    config.vm.box = "centos/7"
    config.vm.synced_folder ".", "/vagrant", type: "rsync"
    config.vm.hostname = "builder-#{rand(100...999)}"
    config.vm.provider "libvirt" do |vb|
        vb.memory = 2048
        vb.cpus = 4
    end
    config.vm.provision "shell", inline: $script

We still have everything in one place, which might be desirable when there's little need for mental context switching between files. There's also the possibility for using the external script. This is useful when porting something existing (like some configuration management that's not yet in Ansible) for development under Vagrant.

    config.vm.provision "shell", path: script.sh

Bonus points for ability to use URI here (e.g. raw file in GitHub).

WARNING! This article has been written a long time ago. It may no longer represent the current views of the author!


Consider commenting via e-mail

Newest in technical