Vagrant is a tool used to create, configure, connect and stop virtual machines. It relies on providers of virtual machines and supports the most common (VirtualBox, VMWare, Hyper-v, etc.).

Fichier de configuration Vagrantfile

Vagrant uses a configuration file called `Vagranfile’ in which is described what needs to be created and configured.

The first lines of this file tell your system that the syntax used is Ruby.

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

Then we define which version of Vagrant will be used.

Vagrant.configure("2") do |config|

Box

The first configuration paramter is the Box , from which your future virtual machine will be created.

There is a rather well-stocked boxes catalogue: Vagrant Boxes .

config.vm.box = "generic/debian9"

Provider

Then we define which Provider we want to use and what options we want to pass to them. In the example below, we want to use VirtalBox, give the name `hugo-box’ to the virtual machine and assign it 2048Mb of memory.

config.vm.provider "virtualbox" do |vb|
  vb.name = "hugo-box"
  vb.memory = "2048"
end

Customization of the virtual machine

It is possible to configure a lot of elements and customize this virtual machine according to your needs.

Hostname

To set the hostname :

config.vm.define 'hugo-box' do |node|
  node.vm.hostname = 'hugo-box'
end

directory synchronization

To configure the synchronization of a folder on your workstation (in the /home/me/my_appli/) with a folder on the virtual machine (in the /home/vagrant/my_appli/), and thus work from the virtual machine on files in your workstation:

config.vm.synced_folder "/home/moi/mon_appli/", "/home/vagrant/mon_appli/"

Port forwarding

If you want to be able to access a VM resource from your workstation, you will need to set up port forwarding.

If your web page is accessible on the tcp/1313 port of your virtual machine, you can access it from your workstation (on the same port) by adding the following configuration to Vagrant:

config.vm.network "forwarded_port", guest: 1313, host: 1313

Pprovisioning

If the box that serves as the basis for the virtual machine needs to be completed or customized, you can ask Vagrant to perform actions after the virtual machine has started. This is what is called “Provisioning” in Vagrant, and he accepts a lot of “Provisioners”: Shell, Ansile, Chief, Pupper…

Shell

To ask Vagrant to execute a simple shell command:

config.vm.provision "shell", inline: "who", privileged: false

or more complex and defines as a $script:

config.vm.provision "shell", inline: $script, privileged: false

This script can be defined at the top of your Vagrantfile (outside of the Vagrant parameters):

$script = <<-SCRIPT
git config --global user.email "je@suis.moi"
git config --global user.name "Je-Suis Moi"
SCRIPT

You can also ask to run a file from your workstation:

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

Ansible

If you need to do some customization on the virtual machine, you can use a deployment tool like Ansible. I will not go into the details of the use of Ansible here, it will be the subject of another article.

Here’s how to ask Vagrant to run an Ansible scenario when launching the virtual machine.

  config.vm.provision "ansible" do |ansible|
    ansible.become = true
    ansible.playbook = "./vagrant_provision.yml"
  end

Creation and launch of the virtual machine

The essential parameters for the creation and launch of a virtual machine are the choice of an image and a supplier.

You can start its creation and configuration with the command below, to be launched when you are in the same directory as your Vagrantfile.

$ vagrant up

By default, you can connect to it with ssh via the command below:

$ vagrant ssh

You will then be logged in with as a vagrant user, configured by default on all virtual machines. This user has authentication based on a pair of keys. It is this pair of keys that allow you to connect to the virtual machine without having to enter a password.

You can prevent use by adding the line below to your Vagrantfile.

config.ssh.insert_key = false

Conclusion

Vagrant is a powerful and simple tool to use. I use it whenever I need to set up a development environment. This allows me to avoid having to install multiple frameworks, libraries and other tools (and all their different versions) on my workstation.

As an example, you will find the Vagrantfile and the vagrant_provision.yaml files I use to launch and test this blog on my workstation, in its GitLab repository .