Files
oam/knowledge base/vagrant.md
2023-07-09 21:35:14 +02:00

5.7 KiB

Vagrant

Table of contents

  1. TL;DR
  2. Usage
    1. Boxes management
  3. Install shells' autocompletion
  4. Customize a Box
  5. Use environment variables in the provisioning script
  6. Specify the disk size
  7. Reboot after provisioning
  8. Loop over VMs' definitions
  9. Further readings

TL;DR

# Initialize Vagrant.
vagrant init 'archlinux/archlinux'
vagrant init 'debian/testing64' --provider 'virtualbox'

# Start a VM.
vagrant up
vagrant up --provider 'libvirt'

# Connect to a started VM
vagrant ssh

# Print the SSH config snippet to connect to the VM.
vagrant ssh-config

# (re)Provision a VM.
vagrant provision
vagrant up --provision

# Add a Box.
vagrant add 'archlinux/archlinux'
vagrant add 'debian/testing64' --provider 'virtualbox'

# List downloaded Boxes.
vagrant box list

# List outdated Boxes.
vagrant box outdated

# Update a Box.
vagrant box update
vagrant box update --box 'generic/gentoo'

# Remove a Box from the local catalogue.
vagrant box remove 'archlinux/archlinux'

# Destroy a VM.
vagrant destroy
vagrant destroy --force

# Install shells' autocompletion.
vagrant autocomplete install --bash
vagrant autocomplete install --zsh

# Install a Plugin.
vagrant plugin install 'vagrant-disksize'

Usage

All commands need to be run from the VM's folder.

  1. Install Vagrant.

  2. Optionally, create a folder to keep all files in order and move into it:

    mkdir 'test-vm'
    cd $_
    
  3. Create a configuration:

    vagrant init 'archlinux/archlinux'
    
  4. Start the VM:

    vagrant up
    
    # Re-provision the VM after startup.
    vagrant up --provision
    
  5. Connect to the VM:

    vagrant ssh
    

Boxes management

vagrant box add 'archlinux/archlinux'
vagrant box add 'archlinux/archlinux' --provider virtualbox

vagrant box list

vagrant box update
vagrant box update --box 'generic/gentoo'

Install shells' autocompletion

$ vagrant autocomplete install --bash
Autocomplete installed at paths:
- /home/user/.bashrc

$ vagrant autocomplete install --zsh
Autocomplete installed at paths:
- /home/user/.zshrc

Customize a Box

Vagrant.configure("2") do |config|
  config.vm.box = "archlinux/archlinux"

  config.vm.provider "virtualbox" do |vb|
    # Vagrant can call any VBoxManage command prior to booting the machine.
    # Multiple customize directives will be executed in order.
    vb.customize ["modifyvm", :id, "--vram", "64"]
    vb.customize ["modifyvm", :id, "--graphicscontroller", "vmsvga"]
    vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]

    # Some settings have convenience shortcuts.
    vb.name = "xfce4 latest"
    vb.cpus = 2
    vb.memory = "2048"
    vb.default_nic_type = "82543GC"
    vb.gui = true

    # Skip the guest additions check.
    vb.check_guest_additions = false
  end

Use environment variables in the provisioning script

Add the variables as argument of the config.vm.provision key:

Vagrant.configure("2") do |config|
  config.vm.provision :shell do |shell|
    shell.env = {
      "STATIC" => "set-in-config",
      "FORWARDED" => ENV['HOST_VAR'],
    }
    shell.inline = <<-SHELL
      printenv STATIC FORWARDED
      sudo -u vagrant --preserve-env=STATIC,FORWARDED printenv STATIC FORWARDED
    SHELL
  end
end

Specify the disk size

Install the 'vagrant-disksize' plugin:

vagrant plugin install 'vagrant-disksize'

then set it up:

vagrant.configure('2') do |config|
    config.disksize.size = '50GB'
end

Reboot after provisioning

Add one of the following to the box's Vagrantfile:

config.vm.provision "shell", reboot: true

config.vm.provision :shell do |shell|
  shell.privileged = true
  shell.reboot = true
end

Loop over VMs' definitions

The inner portion of multi-machine definitions and provider overrides are lazy-loaded.
This means the value of variables used in it cannot change (like in a for cycle).

This works:

(1..3).each do |i|
  config.vm.define "node-#{i}" do |node|
    node.vm.provision "shell", inline: "hostname"
  end
end

This does not work:

for i in 1..3 do
  config.vm.define "node-#{i}" do |node|
    node.vm.provision "shell", inline: "hostname"
  end
end

Further readings