Difference between revisions of "Vagrant/Build always same VMs"

From Ever changing code

(Enable mDNS discovery)
(2 intermediate revisions by the same user not shown)
Line 685: Line 685:
== References ==
== References ==
*[ Vagrant GUI vms] stackoverflow
*[ Vagrant GUI vms] stackoverflow
= Windows=
<source lang=ruby>
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
 + = "gusztavvargadr/windows-server"
  config.vm.box_check_update = false
  config.vm.provider "virtualbox" do |vb|
    vb.gui = true      # Display the VirtualBox GUI when booting the machine
    vb.memory = "3072"  # Customize the amount of memory on the VM:
  # Plugins
  config.vbguest.auto_update = false
  config.vbguest.no_remote = true
Shared location
* enable Network Sharing
* Vagrant path is mapped to <code>\\VBOXSVR\vagrant</code>
= WIP DevOps workstation =
= WIP DevOps workstation =

Revision as of 18:51, 11 July 2019


Vagrant is configured on a per project basis. Each of these projects has its own Vagran file. The Vagrant file is a text file in which vagrant reads that sets up our environment. There is a description of what OS, how much RAM, and what software to be installed etc. You can version control this file.

Ruby gotchas

Vagrant configuration file is written in Ruby specification, therefore you need to remember

  • don't use dashes in object names, don't: = "ubuntu/xenial64"
  • don't use symbols (here underscore) in variable names, don't: (1..2).each do |minion_number|


Check your distro most likely Ubuntu version candidate

apt-cache vagrant

Download from

sudo dpkg -i vagrant_1.9.5_x86_64.deb
sudo apt-get update && sudo apt-get install -f   #to resolve missing dependencies if any

Install ruby is recommended as configuration within Vagrant file is written in Ruby language.

sudo apt-get install ruby
sudo gem install bundler
sudo gem update bundler    #if update needed

Version upgrade

In this example I was successful to update version 1.9.5 to 2.1.1 on top of old package.#

  1. Download new version
  2. Install using dpkg -i
  3. If you see errors you can attempt to try
vagrant plugin repair    #I used
vagrant plugin expunge --reinstall
vagrant plugin update    #Then updated plugin 'vagrant-proxyconf'

Images management

So, you know Vagrant comes with a preconfigured image repositories.

Add boxes

These are standard VMs from providers in Virtualbox, VMware or Hyper-V format taken from a given repository

vagrant box add hashicorp/precise64      #user: hashicorp boximage: precise64, this is preconfigured repository
vagrant box add ubuntu/xenial64
vagrant box add ubuntu/xenial64    --box-version 20170618.0.0 --provider virtualbox
vagrant box add bento/ubuntu-18.04 --box-version 201812.27.0  --provider hyperv

Box can be specified via URLs or local file paths, Virtualbox can only nest 32bit machines

vagrant box add --force ubuntu/14.04 <nowiki></nowiki>
vagrant box add --force <span style="color: green">ubuntu/14.04-i386</span> <nowiki></nowiki>
Manage boxes using
vagrant box list | add | remove

vagrant init - your first project

Configure Vagrantfile to use the box as your base system
Vagrant.configure("2") do |config|      = "ubuntu/bionic64"
 config.vm.hostname = "ubuntu" #hostname, requires reload

Create Vagrant project, by creating Vagrantfile in your current directory
vagrant init                    #initialises an project 
vagrant init ubuntu/xenial64    # initialises official Ubuntu 16.04 LTS (Xenial Xerus) Daily Build
vagrant init ubuntu/bionic64    #supports only Virtualbox provider
vagrant init bento/ubuntu-18.04 #supports variety of providers
Power up your Vagrant box
vagrant up

 Timed out while waiting for the machine to boot. This means that
 Vagrant was unable to communicate with the guest machine within
 the configured ("config.vm.boot_timeout" value) time period.

The error above is due to Virtualbox cannot run nested 64bit virtualbox VM. Spinning up a 64bit VM stops with an error that no 64bit CPU could be found.

Ssh to the box
piotr@vm-ubuntu64:~/git/vagrant$ vagrant ssh    #default password is "vagrant"
vagrant@vagrant-ubuntu-precise-32:~$ w
 13:08:35 up 15 min,  1 user,  load average: 0.06, 0.31, 0.54
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0         13:02    1.00s  4.63s  0.09s w
Shared directory between Vagrant VM and an hypervisor provider

Vagrant VM shares a directory mounted at /vagrant with the directory on the host containing your Vagrantfile. This can be manually mounted from within VM as long the shared directory is setup in GUI.

Eg. vm_name > Settings > Shared Folders > Name: vagrant | Path: /home/piotr/vm_name

sudo mount -t vboxsf -o uid=1000 vagrant /vagrant #firts arg 'vagrant' refers to GUI setiing


vagrant --debug up

Manage power states

  • vagrant suspend - saves the current running state of the machine and stop it
  • vagrant halt - gracefully shuts down the guest operating system and power down the guest machine
  • vagrant destroy - removes all traces of the guest machine from your system. It'll stop the guest machine, power it down, and remove all of the guest hard disks

Lookup path precedence for Vagrant project file

When you run any vagrant command, Vagrant climbs your directory tree starting first in the current directory you are in. Example:




Private network is network that is not accessible from Internet. Networking stanza is a part of the main |config| loop.

DHCP IP address assigned "private_network", type: "dhcp"

Static IP assigment "private_network", ip: ""
auto_config: false     #optional to disable auto-configure

Public network These networks can be accessible from outside of the host machine including Internet, are usually Bridged Networks.

Examples of dhcp and static IP assignment "public_network", type: "dhcp" "public_network", ip: ""

Default interface. The name need to match your system name otherwise Vagrant will prompt you to choose from available interfaces during vagrant up process. "public_network", bridge: 'eth1'

Port forwarding

Vagrant can forward any host(hypervisor) TCP port to guest vm specyfing in ~/git/vargant/Vagrant file :forwarded_port, guest: 80, host: 4567

Reload virtual machine vagrant reload and run from hypervisor web browser to test it.

Sync folders

First folder is on a host machine and the second is mapped on Vagrant guest vm.


Vagrant.configure("2") do |config|
    config.vm.sync_folder "../data/", "/vagrant-data", disabled: true

Modifying the Owner/Group

config.vm.sync_folder "../data/", "/vagrant-data", disabled: true,
   owner: "root", group: "root"

Vagrant providers

Vagrant can work with a wide variety of backend providers, such as VMware, AWS, and more without changing Vagrantfile. It's enough to specify the provider and Vagrant will do the rest:

vagrant up --provider=vmware_fusion
vagrant up --provider=aws


  • Enable Hyper-V
  • if you running Docker for Windows make sure is disabled as only one application can bound to Internal NAT vswitch, if you are using it
  • WSL and Windows Vagrant versions must match
  • the terminal you run WSL or PowerShell runs with elevated privileges

When running in WSL, make sure you have export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1",

  • you are in native Bash.exe not eg. ConEmu terminal with as it was proven not working at the time. You can change default provider by export VAGRANT_DEFAULT_PROVIDER=hyperv

Optional: Set the user-level environment variable in PowerShell:

[Environment]::SetEnvironmentVariable("VAGRANT_DEFAULT_PROVIDER", "hyperv", "User")

Copy insecure private key from to WSL ~/.vagr ant_key/private_key because Microsoft filesystem does not support Unix style file permissions, until WSL2 is released.

$ wget -O ~/.vagrant_key/private_key
# then set in Vagrantfile
config.ssh.private_key_path = "~/.vagrant_key/private_key"

When running on HyperV you need to choose a vswitch you will use. Vagrant will prompt you, select "Default Switch", w hich is eqvivalent of NAT Network. You need to creat your own vswitch if you want access to Internet.

Go to Hyper-V Manager, open Virtual Switch Manager..., create External switch, name: vagrant-external, press OK. Then

vagrant up --provider hyperv

    default: Please choose a switch to attach to your Hyper-V instance.
    default: If none of these are appropriate, please open the Hyper-V manager
    default: to create a new virtual switch.
    default: 1) DockerNAT
    default: 2) Default Switch
    default: 3) vagrant-external
    default: What switch would you like to use?3    #<-- select 3

Read more

Run Vagrant file

vagrant up --provider=hyperv



Shell provisioner

Vagrant can run from shared location script or from inline: Vagrant file shell provisioning commands.

Create provisioning script

vi ~/git/vagrant/     
#!/usr/bin/env bash
export http_proxy=<nowiki>http://username:password@proxyserver.local:8080</nowiki>
export https_proxy=$http_proxy 
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then 
  rm -rf /var/www
  ln -sf /vagrant /var/www  # sets Vagrant shared dir to Apache DocumentRoot

Configure Vagrant to run this shell script above when setting up our machine

vi ~/git/vagrant/Vagrantfile   
Vagrant.configure("2") do |config| = ubuntu/14.04-i386
   config.vm.provision: shell, path: ""

Another example of using shell provisioner, separating a script out

$script = <<SCRIPT
echo    " touch /home/vagrant/test_\\`date +%s\\`.txt " > /home/vagrant/newfile
chmod +x        /home/vagrant/newfile
echo "* * * * * /home/vagrant/newfile" > mycron
crontab mycron

Vagrant.configure("2") do |config| = "ubuntu/xenial64"
  config.vm.provision "shell", inline: $script , privileged: false

Bring the environment up

vagrant up                   #runs provisioning only once
vagrant reload --provision   #reloads VM skipping import and runs provisioning
vagrant ssh                  #ssh to VM
wget -qO-          #test Apache is running on VM

Provisioners - shell, ansible, ansible_local and more

This section is about using Ansible with Vagrant,

*ansible, where Ansible is executed on the Vagrant host
*ansible_local, where Ansible is executed on the Vagrant guest

Ansible provisioner

Specify Ansible as a provisioner in Vagrant file

# Run Ansible from the Vagrant Host
 config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"

Chef_solo provisioner

Create recipe, the following dirctory structure is required, eg. recipe name is: vagrant_la

├── cookbooks
│   └── vagrant_la
│       └── recipes
│           └── default.rb


vi cookbooks/vagrant_la/recipes/default.rb
execute "apt-get update"
package "apache2"
execute "rm -rf /var/www"
link "var/www" do
        to "/vagrant"

In Vagrant file add following

config.vm.provision "chef_solo" do |chef|
        chef.add_recipe "vagrant_la"

Run vagrant up

Puppet manifest

Create Vagrant provisioning stanza

config.vm.define "web" do |web|
         web.vm.hostname = "web" = "apache" "private_network", type: "dhcp" "forwarded_port", guest: 80, host: 8080
         web.vm.provision "puppet" do |puppet|
                 puppet.manifests_path = "manifests"
                 puppet.manifest_file = "default.pp"

Create a required folder structure for puppet manifests

├── manifests
│   └── default.pp
└── Vagrantfile

Puppet manifest file

vi manifests/default.pp
exec { "apt-get update":
       command => "/usr/bin/apt-get update",
package { "apache2":
       require => Exec["apt-get update"],
file { "/var/www":
       ensure => link,
       target => "/vagrant",
       force => true,

Box images advanced

vagrant box list   #list all downloaded boxes

Default path of boxes image, it can be specified by environment variable VAGRANT_HOME

C:\Users\%username%\.vagrant.d\boxes  #Windows
~/.vagrant.d/boxes                    #Linux

Change default path via environment variable

export VAGRANT_HOME=my/new/path/goes/here/

Box format

When you un-tar the .box file it contains 4 files:

|--box-disk1.vmdk  #compressed virtual disk
|--box.ovf         #description of virtual hardware

Create box from current project (package box)

This allows to create a reusable box that contains all changes to the software we made

$ vagrant package web
==> web: Attempting graceful shutdown of VM...
==> web: Clearing any previously set forwarded ports...
==> web: Exporting VM...
==> web: Compressing package to: /home/piotr/vagrant/
$ vagrant box add web
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'web' (v0) for provider: 
   box: Unpacking necessary files from: file:///home/piotr/vagrant/

After this process you will find the box in ~/.vagrant.d/boxes location and you can also list them using vagrant box list.

Enable Vagrant to use proxy server for VMs

Install proxyconf plugin or use vagrant plugin list to verify if installed

vagrant plugin install vagrant-proxyconf

Configure your Vagrantfile, here particularly host runs CNTLM proxy

Vagrant.configure("2") do |config| 
    config.proxy.http = ""
    config.proxy.https = ""
    config.proxy.no_proxy = "localhost,"

Virtualbox Guest Additions - upgrade

Automatic sync using plugin

$ vagrant gem install vagrant-vbguest #for vagrant < 1.1.5
$ vagrant plugin install vagrant-vbguest #for vagrant 1.1.5+

Verify current version

vagrant vbguest --status

For Vagrant 1.1.5+, add to your Vagrant file, to

config.vbguest.auto_update = false
config.vbguest.no_remote = true

Then install the correct version matching your host installation

vagrant vbguest --do install 

Full command options

$ vagrant vbguest [vm-name] [--do start|rebuild|install] [--status] [-f|--force] \
                  [-b|--auto-reboot] [-R|--no-remote] [--iso VBoxGuestAdditions.iso] [--no-cleanup]

More you will find at vagrant-vbguest plugin project.

Manual upgrade

Find out what version you run

$ modinfo vboxguest


$ lsmod | grep -io vboxguest | xargs modinfo | grep -iw version
version:        4.3.36_Ubuntu


$ sudo /usr/sbin/VBoxService --version

Download the extension, you can explore here


wip... see references for the rest of steps, next step instal vagrant plugin vb...


List all Virtualbox SSH redirections

vboxmanage list vms | cut -d ' ' -f 2  > /tmp/vms.out && for vm in $(cat /tmp/vms.out); do vboxmanage showvminfo "$vm" | grep ssh; done
vboxmanage list vms | cut -d ' ' -f 1 | sed 's/"//g' > /tmp/vms.out && for vm in $(cat /tmp/vms.out); do echo $vm; vboxmanage showvminfo "$vm" | grep ssh; done
vboxmanage list vms \
  | cut -d ' ' -f 1 \
  | sed 's/"//g' > /tmp/vms.out \
  && for vm in $(cat /tmp/vms.out); do vboxmanage showvminfo "$vm" \
                                      | grep ssh \
                                      | tr --delete '\n'; echo " $vm"; done

sed 's/"//g'      #removes double quotes from whole string
tr --delete '\n'  #deletes EOL, so the next command output is appended to the previous line

HAProxy cluster, multi-node Vagrant config

git clone

This creates Ansible mgmt server, Load Balancer and Web nodes [1..2]. HAProxy will be configured via Ansible code.

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

Vagrant.configure("2") do |config|
 # create mgmt node
 config.vm.define :mgmt do |mgmt_config| = "ubuntu/trusty64"
     mgmt_config.vm.hostname = "mgmt" :private_network, ip: ""
     mgmt_config.vm.provider "virtualbox" do |vb|
       vb.memory = "256"
     mgmt_config.vm.provision :shell, path: ""

 # create load balancer
 config.vm.define :lb do |lb_config| = "ubuntu/trusty64"
     lb_config.vm.hostname = "lb" :private_network, ip: "" "forwarded_port", guest: 80, host: 8080
     lb_config.vm.provider "virtualbox" do |vb|
       vb.memory = "256"

 # create some web servers
  (1..2).each do |i|
    config.vm.define "web#{i}" do |node| = "ubuntu/trusty64"
        node.vm.hostname = "web#{i}" :private_network, ip: "{i}" "forwarded_port", guest: 80, host: "808#{i}"
        node.vm.provider "virtualbox" do |vb|
          vb.memory = "256"

Boot strap script

#!/usr/bin/env bash 
# install ansible (
apt-get -y install software-properties-common
apt-add-repository -y ppa:ansible/ansible
apt-get update
apt-get -y install ansible

# copy examples into /home/vagrant (from inside the mgmt node)
cp -a /vagrant/examples/* /home/vagrant
chown -R vagrant:vagrant /home/vagrant

# configure hosts file for our internal network defined by Vagrantfile
cat >> /etc/hosts <<EOL
# vagrant environment nodes  mgmt  lb  web1  web2  web3  web4  web5  web6  web7  web8  web9

Gitbash path - /c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe

Set bootstrap script for Proxy or No-proxy specific system

Vagrant status
Vagrant up
Vagrant ssh mgmt
ansible all --list-hosts
ssh-keyscan web1 web2 lb > ~/.ssh/known_hosts
ansible-playbook ssh-addkey.yml -u vagrant --ask-pass
ansible-playbook site.yml

Once set it up you can navigate on your laptop to:

http://localhost:8080/              #Website test
http://localhost:8080/haproxy?stats #HAProxy stats

Use to verify end server

curl -I http://localhost:8080

Curl -i X-Backend-Server

Generate web traffic

vagrant ssh lb
sudo apt-get install apache2-utils
ansible localhost -m apt -a "pkg=apache2-utils state=present" --become
ab -n 1000 -c 1

Enable mDNS discovery

By default, mDNS only and exclusively resolves host names ending with the .local top-level domain (TLD). This can cause problems if that domain includes hosts which do not implement mDNS but which can be found via a conventional unicast DNS server. Resolving such conflicts requires network-configuration changes that violate the zero-configuration goal. Install avahi system on all machines to facilitate service discovery on a local network via the mDNS/DNS-SD protocol suite.

config.vm.provision "shell", inline: <<-SHELL
  apt-get install -y avahi-daemon libnss-mdns


  • nss-mdns system which allows hostname lookup of *.local hostnames via mDNS in all system programs using nsswitch

Use host system DNS server resolver

config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]

Ubuntu with GUI

This article is going to describe to setup Vagrant Virtualbox VM with GUI, setting up xserver with xfce4 as desktop environment.


This is not working

locale-gen en_GB.utf8 #en_GB.UTF-8
     update-locale LANG=en_GB.UTF-8
     locale-gen --purge "en_GB.UTF-8"
     dpkg-reconfigure --frontend=noninteractive locales
     dpkg-reconfigure --frontend=noninteractive keyboard-configuration
     localedef -i en_GB -c -f UTF-8 en_GB.utf8
     sudo update-locale LANG=en_GB.UTF-8
     locale-gen --purge "en_GB.UTF-8"


This setup installs Ubuntu desktop and may require a restart to apply changes to like a taskbar with shortcuts.

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

Vagrant.configure("2") do |config|      = "ubuntu/bionic64" #bento/ubuntu-18.04, ubuntu/xenial64
  config.vm.hostname = "u18gui-1"

  # Manually check for updates `vagrant box outdated`
  config.vm.box_check_update = false

  # Vbguest plugin
  if Vagrant.has_plugin?("vagrant-vbguest")
    config.vbguest.auto_update = false
    config.vbguest.no_remote   = true

  # "forwarded_port", guest: 80, host: 8080, host_ip: ""
  # Public network, which generally matched to bridged network.
  # "public_network"

  # config.vm.synced_folder "hostDir", "/InVagrantMount/path" 
  # config.vm.synced_folder "../data", "/vagrant_data"

   config.vm.provider "virtualbox" do |vb|
     vb.gui    = true
     vb.memory = "3072"   = "u18gui-1_vagrant"
   config.vm.provision "shell", inline: <<-SHELL
     export DEBIAN_FRONTEND=noninteractive
     setxkbmap gb
     apt-get update && apt-get upgrade -yq
     apt-get install -yq ubuntu-desktop --no-install-recommends
     apt-get install -yq terminator tmux
     #only U16 xenial to fix Unity
     #apt-get install -yq unity-lens-files unity-lens-applications indicator-session --no-install-recommends 

Running up

vagrant plugin install vagrant-vbguest
vagrant up && vagrant vbguest --do install && vagrant reload


Get a basic Ubuntu image working, boot it up and vagrant ssh. Next, enable the VirtualBox display, which is off by default. Halt the VM and uncomment these lines in Vagrantfile:

config.vm.provider :virtualbox do |vb|
  vb.gui = true

Boot the VM and observe the new display window. Now you just need to install and start xfce4. Use vagrant ssh and:

sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11
#guest additions are already installed on most of the Vagrant boxes

Don't start the GUI as root because you really want to stay the vagrant user. To do this you need to permit anyone to start the GUI:

sudo vim /etc/X11/Xwrapper.config and edit it to allowed_users=anybody
sudo startxfce4&
sudo VBoxClient-all #optional

You should be landed in a xfce4 session.

(Optional) If VBoxClient-all script isn't installed or anything is missing, you can replace with the equivalent:

sudo VBoxClient --clipboard
sudo VBoxClient --draganddrop
sudo VBoxClient --display
sudo VBoxClient --checkhostversion
sudo VBoxClient --seamless



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

Vagrant.configure("2") do |config| = "gusztavvargadr/windows-server"
  config.vm.box_check_update = false
  config.vm.provider "virtualbox" do |vb|
     vb.gui = true       # Display the VirtualBox GUI when booting the machine
     vb.memory = "3072"  # Customize the amount of memory on the VM:
  # Plugins
  config.vbguest.auto_update = false
  config.vbguest.no_remote = true

Shared location

  • enable Network Sharing
  • Vagrant path is mapped to \\VBOXSVR\vagrant

WIP DevOps workstation

This to contain:

  • bashrc with git branch in ps1
  • bash autocomplete (...samename)
  • bash colored symlinks
  • bash_logout and .profile to eval ssh-agent and kill on exit
  • git install
  • ansible 1.9.4
  • java Oracle
  • clone tfenv and install terraform
  • vim install
  • vundle install
  • [done] python 2.7 OOB in 16.04
  • [done]python pip: awscli, boto, boto3, etc..