In this example, we're going to creating two vagrant machines with a single vagrantfile. Machines will include apache web server and will be accessible from the host machine. They both will have their own instances in Virtual Box with different names.


Configuration


I'm assuming that you already have installed vagrant and Oracle VM software. I also assume that the ubuntu/trusty64 box is already added to your filesystem with vagrant box add ubuntu/trusty64 command. If you're not sure, you can confirm it with ls -l ~/.vagrant.d/boxes/ command.


Create a new project folder


mkdir dual-machine
$ cd dual-machine/

Create a bootstrap file


Do it when you're in dual-machine folder. Name the file as web-server.sh.


#!/usr/bin/env bash

echo -e "-- Setting global variables\n"
APACHE_CONFIG=/etc/apache2/apache2.conf
DOCUMENT_ROOT=/var/www/html
LOCALHOST=localhost

echo -e "-- Updating packages list\n"
apt-get update -y -qq

echo -e "-- Installing Apache web server\n"
apt-get install -y apache2 > /dev/null 2>&1

echo -e "-- Adding ServerName to Apache config\n"
grep -q "ServerName ${LOCALHOST}" "${APACHE_CONFIG}" || echo "ServerName ${LOCALHOST}" >> "${APACHE_CONFIG}"

echo -e "-- Restarting Apache web server\n"
service apache2 restart

echo -e "-- Creating a dummy index.html file\n"
cat > ${DOCUMENT_ROOT}/index.html <<EOD
<html>
<head>
<title>${HOSTNAME}</title>
</head>
<body>
<h1>${HOSTNAME}</h1>
<p>Hi sir, I am going to serve you today!</p>
</body>
</html>
EOD

Create vagrant file


Do it when you're in dual-machine folder. Name the file as Vagrantfile.


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

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

# Configs for web server 1
config.vm.define :webserver1 do |webserver1_config|
# Configs for virtual machine entry in Oracle VM VirtualBox
webserver1_config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant Web Server 1 - Ubuntu 14.04"
end
# Terminal name
webserver1_config.vm.hostname = "webserver1"
# IP address to access from host machine
webserver1_config.vm.network "private_network", ip: "192.168.50.10"
# Script to run while setting up the machine
webserver1_config.vm.provision :shell, path: "web-server.sh"
# Shared folder between host and virtual machine with "faster" nfs option and writing permissions enabled
webserver1_config.vm.synced_folder "webserver1", "/var/www/html", create: true, nfs: true, mount_options: ["actimeo=2"]
end

# Configs for web server 2
config.vm.define :webserver2 do |webserver2_config|
# Configs for virtual machine entry in Oracle VM VirtualBox
webserver2_config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant Web Server 2 - Ubuntu 14.04"
end
# Terminal name
webserver2_config.vm.hostname = "webserver2"
# IP address to access from host machine
webserver2_config.vm.network "private_network", ip: "192.168.50.20"
# Script to run while setting up the machine
webserver2_config.vm.provision :shell, path: "web-server.sh"
# Shared folder between host and virtual machine with "faster" nfs option and writing permissions enabled
webserver2_config.vm.synced_folder "webserver2", "/var/www/html", create: true, nfs: true, mount_options: ["actimeo=2"]
end
end

Run vagrant box


If you ever change your "web-server.sh" file, you'll need to run vagrant reload --provision command instead. In this process, you'll be prompted to enter admin password which is your OS login password. If you want to bypass it you can look into nfs page.


$ vagrant up --provision
Bringing machine 'webserver1' up with 'virtualbox' provider...
Bringing machine 'webserver2' up with 'virtualbox' provider...
==> webserver1: ......
==> webserver1: ......
==> webserver1: ......
==> webserver1: -- Setting global variables
==> webserver1: -- Updating packages list
==> webserver1: -- Installing Apache web server
==> webserver1: -- Adding ServerName to Apache config
==> webserver1: -- Restarting Apache web server
==> webserver1: * Restarting web server apache2
==> webserver1: ...done.
==> webserver1: -- Creating a dummy index.html file
==> webserver2: ......
==> webserver2: ......
==> webserver2: ......
==> webserver2: -- Setting global variables
==> webserver2: -- Updating packages list
==> webserver2: -- Installing Apache web server
==> webserver2: -- Adding ServerName to Apache config
==> webserver2: -- Restarting Apache web server
==> webserver2: * Restarting web server apache2
==> webserver2: ...done.
==> webserver2: -- Creating a dummy index.html file

Access the machines


# Server 1
$ vagrant ssh webserver1
vagrant@webserver1:~$

# Server 2
$ vagrant ssh webserver2
vagrant@webserver2:~$

Access servers from host browser


Just go to http://192.168.50.10/ or http://192.168.50.20/ for respective web servers.