Run Definition of Arbitrary Scripts with Vagrant Commands

What and why Vagrant

Vagrant is computer software for creating and configuring virtual development environments. It can be seen as a wrapper around virtualization software such as VirtualBox, KVM, VMware and around configuration management software such as Ansible, Chef, Salt or Puppet.

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

Vagrant Official Website www.vagrantup.com

After install Vagrant and VirtualBox, we can create a Vagrant development environment such as Ubuntu 14.04 LTS 64bit by this command vagrant init ubuntu/trusty64, will be generate a Vagrantfile in current directory.

If you wanna ro moving VirtualBox and Vagrant to another directory, first move ~/.vagrant.d to the another directory,

$ sudo mv ~/.vagrant.d /Volumes/SAMSUNG/.vagrant.d

Set VAGRANT_HOME to new path in ~/.bash_profile

$ export VAGRANT_HOME="/Volumes/SAMSUNG/.vagrant.d"

Open the VirtualBox, in Preferences set its Default Machine Folder to new path, then quit VirtualBox, move VirtualBox VMs folder to the new directory. Reopen VirtualBox. will see VMs are listed as inaccessible. Remove them from the list. For each VM in your VirtualBox VMs folder on the external drive, browse to its folder in Finder and double-click the .vbox file to restore it to the VirtualBox Manager. Finally, move any existing Vagrant directories you've made with vagrant init (these are the directories with a Vagrantfile in each) to the external drive. Since these directories only store metadata you could leave them on your main drive, but it's nice to keep everything together so you could fairly easily plug the whole drive into another machine and start your VMs from there.

The Vagrantfile

The primary configuration location for any Vagrant development environment is a file called Vagrantfile which you need to place in your project’s folder.

The configuration syntax of this Vagrantfile is Ruby, but you do not need to be a Ruby programmer or have any knowledge of the programming language to write this configuration file. You'll mostly do basic variable assignment in the configuration.

Every configuration option you will need you can place inside this file.

About provisioning

The primary purpose of Vagrant is to have a base virtual machine and to give you the framework for creating automatic software installations and configurations in the virtual machine.

By letting Vagrant handle the provisioning of software, it also gives you the flexibility in configuration and more importantly, makes this process repeatable and automatic.

Vagrant doesn't care how you provision the virtual machine, it offers multiple options ranging from basic shell scripts to software automation managers such as Puppet, Chef or Ansible. You can even configure it to use multiple provisioners at the same time.

Of course there's always the possibility to vagrant ssh into the base virtual machine and install your required software manually, but that defeats the purpose of Vagrant and all the flexibility it offers when provisioning a box.

Shell Script Provisioning

The easiest way to provision a base box is to use basic shell script commands which then run inside the virtual machine. This also removes the need for learning or installing Puppet, Ansible and similar tools – even though they can sometimes be more effective provisioners. We’ll deal with them in future articles.

We need to define the provisioning type, which in our case is called shell. Let's write that inside this block in the configuration file

config.vm.provision "shell" do |s|
    s.path "provision/setup.sh"
end

Vagrant has two types of shell provisioning, inline and external. With inline you can write shell commands in the Vagrantfile itself, but let’s focus on external provisioning, which simply means to load and run a shell script from a file (relative to Vagrantfile) or even from a URL.

In our case we want to load provision/setup.sh file, let's create it and write the following in this file

#!/bin/bash
echo "Provisioning virtual machine..."

Now, run vagrant up and it will output Provisioning virtual machine... on the screen. Note that Vagrant will provision the virtual machine only once on the first run, any subsequent provisioning must be executed with the --provision flag either vagrant up --provision or vagrant reload --provision. The provisioning will re-run also if you destroy the VM and rebuild it with vagrant destroy and vagrant up.

Update and Upgrade

echo "Update and Upgrade..."
sudo apt-get update && sudo apt-get upgrade

Vagrantfile

Vagrant.configure(2) do |config|
    # Specifying the base box
    config.vm.box = "ubuntu/trusty64"

    # Shell provisioning
    config.vm.provision "shell" do |s|
        s.path = "provision/setup.sh"
    end
end

provision/setup.sh

#!/bin/bash
echo "Provisioning virtual machine..."
echo "Update and Upgrade..."
sudo apt-get update && sudo apt-get upgrade

Running vagrant up to set up virtual machine. This article reference Moving VirtualBox and Vagrant to an external drive and Vagrantfile Explained: Setting Up and Provisioning with Shell.

0.00 avg. rating (0% score) - 0 votes