Virtual Day
Building the ecosystem for the Cloud Operating Model with Azure, Cisco, F5 and GitLab Register

Getting Started

Provision a Virtual Machine

You have a virtual machine running a basic copy of Ubuntu. You can edit files locally and have them synced into the virtual machine. In this tutorial you will serve those files using a webserver.

You could just SSH into the guest machine and install a webserver, but you would have to repeat this process every time you shut down and brought this environment back up. Vagrant can instead automatically install software when you vagrant up so that the guest machine can be repeatably created and ready-to-use.

»Create an HTML directory

Create a directory where Apache will look for your content.

$ mkdir html

Next create a file called index.html in the new directory, and populate it with the content for the index page.

<!DOCTYPE html>
    <h1>Getting started with Vagrant!</h1>

»Write a provisioning script

We will setup Apache using a shell script for this tutorial. Create the following shell script and save it as in the same directory as your Vagrantfile.

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www

This script will download and start Apache, and crate a symlink between your synced files directory and the location where Apache will look for content to serve.

»Configure Vagrant

Next, configure Vagrant to run this shell script when setting up the machine, by editing the Vagrantfile. It which should now look like this.

Vagrant.configure("2") do |config| = "hashicorp/bionic64"
  config.vm.provision :shell, path: ""

The provision line tells Vagrant to use the shell provisioner to setup the machine, with the file. The file path is relative to the location of the project root (where the Vagrantfile is).

»Provision the webserver

If your machine were not already up, vagrant up would create your machine and Vagrant would automatically provision it. But, because the guest machine is already running from a previous step, You need to reload your machine.

$ vagrant reload --provision
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
## ... Output truncated ...

This will quickly restart your virtual machine, skipping the initial import step. The provision flag on the reload command instructs Vagrant to run the provisioners, since usually Vagrant will only do this on the first vagrant up.

After Vagrant completes running, the web server will be up and running. You cannot see the website from your own browser (yet), but you can verify that the provisioning works by loading a file from within the machine.

SSH into the guest machine.

$ vagrant ssh

Now get the HTML file that was created during provisioning.

vagrant@vagrant:~$ wget -qO-
<!DOCTYPE html>
    <h1>Getting started with Vagrant!</h1>

This works because in the shell script above you installed Apache and setup the default DocumentRoot of Apache to point to your /vagrant directory, which is the default synced folder setup by Vagrant.

Continue to the next tutorial where you will forward a port on your guest machine so that you can access the website that the guest machine is serving.

Logout of your SSH session.

vagrant@vagrant:~$ logout
Connection to closed.

»Next Steps

For complex provisioning scripts, it may be more efficient to package a custom Vagrant box with those packages pre-installed instead of building them each time. Learn more in the packaging custom boxes documentation.

You have successfully provisioned your first virtual machine with Vagrant. Read on to learn about networking.