Vagrant is a tool that allows the user to spawn, launch, halt and manage multiple Virtual Machines easily. It has been described as “a Virtualbox CLI on steroids”. Here’s a list of commands that would help any newcomer to Vagrant to be productive quickly. This is not meant to be exhaustive. For (a lot) more, check out the official doc
Boxes
Boxes are images Vagrant uses to create VMs. You can look for boxes online or pass them around on a network.
To add a box
You can find some boxes on Vagrantbox.es
$ vagrant box add mynewbox http://...box
Or a box that you have locally
$ vagrant box add mynewbox /path/to/.../...box
To list the existing boxes
$ vagrant box list
Vagrantfiles
A Vagrantfile describes the network your building. When executing a command, vagrant will look for a file named “Vagrantfile” in the current directory. You can learn how to write complex Vagrantfiles in the official documentation. Here’s the simplest example
Init file
$ vagrant init $ ls Vagrantfile
This command has created a minimal Vagrantfile. Modify it so that it includes your selected box
$ grep -Ev '^ *(#|$)' Vagrantfile Vagrant::Config.run do |config| config.vm.box = "mynewbox" end
This configuration defines one machine based on mynewbox (it will be called “default”). It will use default values for everything else.
Status machine
$ vagrant status Current VM states: default not created The environment has not yet been created. Run `vagrant up` to create the environment.
Other statuses include:
- poweroff
- running
Status is particularly useful when you have several machines in a Vagrantfile.
Install and run
$ vagrant up $ vagrant status Current VM states: default running
This command will start the VM. If the VM hasn’t been created or has been destroyed, it will first install the OS on the machine.
Connect to the machine
$ vagrant ssh
ssh is activated by default.
- Username: vagrant
- Password: vagrant
- sudo: enabled
File sharing
By default, Vagrant automatically maps the current directory to the /vagrant directory in the guest machine. Files are instantly synchronized.
Halt a machine
$ vagrant halt
Use this command to put a machine in the ‘poweroff’ status. It will boot into the exact same state next time you run ‘vagrant up’
Destroy a machine
$ vagrant destroy
This command will completely destroy the machine.
Multiple machines
Vagrantfiles
It is possible to define more than one machine in a Vagrantfile. Like this:
Vagrant::Config.run do |env_config| env_config.vm.define "swift1" do |config| config.vm.box = "ub1204" config.vm.network :hostonly, "192.168.1.10" end env_config.vm.define "swift2" do |config| config.vm.box = "ub1204" config.vm.network :hostonly, "192.168.2.10" end env_config.vm.define "rgw" do |config| config.vm.box = "ceph" config.vm.network :hostonly, "192.168.251.100" end end
vagrant commands
When you have more than one machine defined in your Vagrantfile, certain commands like “vagrant ssh” are ambiguous. Two things to keep in mind:
Append one or more machine names
$ vagrant up swift1 swift2 keystone
No machine name will mean all (when applicable)
“vagrant up” will execute for every machine defined, but “vagrant ssh” will throw an error.
Networking
10.0.2.15
By default, vagrant will create an interface eth0 in each machine and assign it IP 10.0.2.15. It will use this interface for communication with vagrant. I find it easier to avoid using this interface for hosting my services.
Add an interface
Here’s how we can define an aditional interface for a machine:
$ grep -Ev '^ *(#|$)' Vagrantfile Vagrant::Config.run do |config| config.vm.box = "base" config.vm.network :hostonly, "192.168.1.10" end
This has defined a host-only interface. This is taken from the official Virtualbox documentation:
Host-only networking
This can be used to create a network containing the host and a set of virtual machines, without the need for the host’s physical network interface. Instead, a virtual network interface (similar to a loopback interface) is created on the host, providing connectivity among virtual machines and the host.