Vagrant: examples

From wikinotes

Summary

You can store your vagrant VM definition anywhere you would like on your system. The VM's actual stored location depends on the provider, virtualbox stores it's VMs as per normal in ~/vbox/<machine>.

Whenever you run vagrant up, your VM config is read, and your VM is configured accordingly. Changes to the VM are persistent (unlike docker).

By default, the root of your vagrant directory is shared with your Vagrant VM. New files created in this directory on your HOST are accessible inside the VM at /. You may configure other mountpoints.

Vagrant's main purpose is to very quickly create disposable VMs for

  • consistent development environment for entire dev team
  • writing/testing puppet/chef setup-scripts
  • cross platform development
  • system administration tests


Multi-OS

You can test networking with VMs of multiple OS's at once.

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

  config.vm.define "bsd" do |bsd|
    bsd.vm.synced_folder ".", "/vagrant"
    bsd.vm.box      = "freebsd/FreeBSD-11.0-CURRENT"
    bsd.vm.base_mac = "ea40a29b1002"
    bsd.vm.hostname = 'bsd_host'
    bsd.vm.network    "private_network", ip: "192.168.50.1"

    bsd.vm.provider :virtualbox do |vb|
      vb.gui = true
      vb.name = 'my_vm_name'  # name within virtualbox
    end
  end

  config.vm.define "arch" do |arch|
    arch.vm.box = "terrywang/archlinux"
    arch.vm.base_mac = "ea40a29b1002"
    arch.vm.hostname = "arch_host"
    arch.vm.network    "private_network", ip: "192.168.50.1"
    arch.vm.provision :shell, inline "ip link set enp0s8 up", :run=>'always'
  end
end

Archlinux

config.vm.define "arch" do |arch|
  arch.vm.box = "terrywang/archlinux"
  arch.vm.base_mac = "ea40a29b1002"
  arch.vm.hostname = "arch_host"
  arch.vm.network    "private_network", ip: "192.168.50.1"
  arch.vm.provision :shell, inline "ip link set enp0s8 up", :run=>'always'
end

FreeBSD

config.vm.define "bsd" do |bsd|
  bsd.vm.synced_folder ".", "/vagrant"
  bsd.vm.box      = "freebsd/FreeBSD-12.1-STABLE"  # 4yrs
  bsd.vm.base_mac = "ea40a29b1002"
  bsd.vm.hostname = 'bsd_host'
  bsd.vm.network    "private_network", ip: "172.28.128.150"   # NFS mount requires private ip '127.x.x.x, 10.x.x.x'
  bsd.vm.synced_folder ".", "/vagrant", type: "nfs"           # NFS instead of virtualbox-mount

  bsd.vm.provider :virtualbox do |vb|
    vb.gui = true
  end
end

NFS must be started on host.

sudo pacman -S nfs-utils
sudo systemctl enable nfs-server
sudo systemctl enable rpcbind

Windows

Vagrant.configure("2") do |config|
  config.vm.define "machine" do |machine|
    machine.vm.provider :virtualbox do |vb|
      vb.gui = true
    end
    config.vm.box = "Microsoft/EdgeOnWindows10"
    config.vm.synced_folder "/home/will/progs", "/progs"
  end
end

MacOS

Vagrant.configure("2") do |config|
  config.vm.box = "jhcook/macos-sierra"
end