Vagrant: configuration

From wikinotes

Config Version

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

ssh

  • generally, login/password are both vagrant
  • vagrant inserts it's own key into ~/.ssh/authorized_keys at creation
  • you can obtain the configured ssh-port using vagrant port

GUI

If you are using Virtualbox, you can start it with a GUI:

config.vm.provider "virtualbox" do |v|
  v.gui = true
end


Vagrant can also run X11 apps in the usual hackish ways. Either by allowing forwarding of X11, or with X11vnc.

config.ssh.forward_x11 = true
https://coderwall.com/p/ozhfva/run-graphical-programs-within-vagrantboxes

Environment Variables

# access environment variables from the HOST using 'ENV'
config.vm.hostname = "dev-vm-" + ENV['HOSTNAME']

Networking

Port Forwarding

# Port Forwarding to host's 127.0.0.1
config.vm.network :forwarded_port, guest: 80, host: 4567

Multi-Machine Network

You can simulate a network comprising of multiple machines.

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"

  ## machine 'web'
  config.vm.define "web" do |web|
    web.vm.box = "apache"
  end

  ## machine 'mysql'
  config.vm.define "db" do |db|
    db.vm.box = "mysql"
  end
end

Mac Addr

# Occasionally, VM requires the MAC address of the network
# bridge interface in order to  have internet access
config.vm.base_mac = "ea40a29b1002"

Shared Folders

config.vm.synced_folder ".", "/vagrant"		## normal, default shared folder (using virtualbox)

Additional Mount Flags

## Additional Flags are comma separated after your
## mount.

config.vm.synced_folder ".", "/vagrant" \
     ,disabled: true		## disable share
     ,owner:    "root"
     ,group:    "root"

nfs mounts

Occasionally, you'll encounter a box that you cannot use virtualbox-sharing with (ex: FreeBSD10). No problem, you can use nfs shares.

# you can find your MAC with
ifconfig -a             ## search for the line next to 'ether'. Remove all the ':'s
## This is kind of your worst case scenario,
## virtualbox-shared folders work almost everywhere
config.vm.define "ansible" do |ansible|
    ansible.vm.synced_folder ".", "/vagrant", type: "nfs"				## mount of type nfs
    ansible.vm.network "private_network", ip: "172.28.128.150"		## nfs requires a private network ip
end

## On your host machine, you will need to have the following packages:
sudo pacman -S nfs-utils

sudo systemctl enable nfs-server
sudo systemctl enable rpcbind