Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Vagrant Practice and Challenges

Practice Vagrant with hands-on challenges across multi-machine setups, providers, plugins, team workflows and troubleshooting, with solutions and a self-check script.

You learn Vagrant by building and breaking environments. This lesson gives you challenges grouped by topic, hints, and complete solutions to compare against. Try each one on your own computer first; the solutions are read-only Vagrantfiles because Vagrant does not run in the browser. A Ruby stand-in at the end lets you check one common kind of mistake automatically.

How to practice

  1. Make a scratch folder per challenge (mkdir challenge1 && cd challenge1).
  2. Write the Vagrantfile, run vagrant validate, then vagrant up.
  3. Prove it works with a command (curl, ping, vagrant ssh -c).
  4. Finish with vagrant destroy -f and run vagrant up again to confirm it is reproducible.

Challenge 1: two machines that can see each other

Create app and db with private IPs 192.168.56.10 and 192.168.56.11. From app, ping db must work by name.

Hint: names resolve only if you write /etc/hosts yourself or use a plugin.

Ruby
HOSTS = { "app" => "192.168.56.10", "db" => "192.168.56.11" }

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  HOSTS.each do |name, ip|
    config.vm.define name do |m|
      m.vm.hostname = name
      m.vm.network "private_network", ip: ip
      m.vm.provision "shell", inline: <<~SH
        sed -i '/# lab/,$d' /etc/hosts
        printf '# lab\n#{HOSTS.map { |n, a| "#{a} #{n}" }.join('\n')}\n' >> /etc/hosts
      SH
    end
  end
end

Check: vagrant ssh app -c "ping -c 1 db".

Challenge 2: an optional machine

Add a third machine tools that is created only when you ask for it by name, and make app the default for vagrant ssh.

Ruby
config.vm.define "app", primary: true do |m| ... end
config.vm.define "tools", autostart: false do |m| ... end

Check: vagrant up creates app and db only; vagrant up tools adds the third; vagrant ssh enters app.

Challenge 3: resources from the environment

Let the user choose the memory with VM_MEMORY=3072 vagrant up, defaulting to 1024. Use half the host CPUs, at least 1.

Ruby
require "etc"
memory = ENV.fetch("VM_MEMORY", "1024").to_i
cpus = [Etc.nprocessors / 2, 1].max

config.vm.provider "virtualbox" do |vb|
  vb.memory = memory
  vb.cpus = cpus
end

Check: VBoxManage showvminfo <name> --machinereadable | grep memory.

Challenge 4: one Vagrantfile, two providers

Write a Vagrantfile that works with VirtualBox and Hyper-V (or libvirt), each with its own memory setting, from one resource constant.

Ruby
MEMORY = 2048

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2204"
  config.vm.provider("virtualbox") { |p| p.memory = MEMORY }
  config.vm.provider("hyperv")     { |p| p.memory = MEMORY }
  config.vm.provider("libvirt")    { |p| p.memory = MEMORY }
end

Check: only the active provider's block applies (providers overview).

Challenge 5: fail fast

Make the Vagrantfile stop with a clear message when Vagrant is older than 2.3 and when the vagrant-hostmanager plugin is missing.

Ruby
Vagrant.require_version ">= 2.3.0"

unless Vagrant.has_plugin?("vagrant-hostmanager")
  raise "Install the plugin first: vagrant plugin install vagrant-hostmanager"
end

Bonus: replace the raise with automatic installation using config.vagrant.plugins (plugins).

Challenge 6: backup before destroy

Add a trigger that dumps a database to your project folder before vagrant destroy, and does not stop the destroy if the machine is already off.

Ruby
config.trigger.before :destroy do |t|
  t.name = "Dump database"
  t.run_remote = { inline: "mysqldump -uroot app > /vagrant/backup.sql" }
  t.on_error = :continue
end

Check: ls backup.sql after vagrant destroy -f (triggers).

Challenge 7: pin everything

Pin the box to a specific version, set the Vagrant range with ~>, and turn off update checks for daily use.

Ruby
Vagrant.require_version "~> 2.4"
config.vm.box = "ubuntu/jammy64"
config.vm.box_version = "20240115.0.0"
config.vm.box_check_update = false

Check with vagrant box list that the pinned version is installed (version constraints).

Challenge 8: find the bug

A teammate's Vagrantfile has three problems. Find them without running it.

Ruby
Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
    web.vm.network "private_network", ip: "192.168.56.10"
    web.vm.network "forwarded_port", guest: 80, host: 8080
  end

  config.vm.define "db" do |db|
    db.vm.network "private_network", ip: "192.168.56.10"
    db.vm.provision "shell", inline: "mysql -uroot -pTopSecret -e 'create database app'"
  end

  config.vm.box = "ubuntu/jammy64"
end

Solution:

  1. Both machines use 192.168.56.10: duplicate IP.
  2. The password TopSecret is hard-coded in a command line.
  3. config.vm.box is set after the define blocks, so the machines do not inherit it (they have no box at all).

Also noteworthy: create database app fails on a second run; use IF NOT EXISTS.

Challenge 9: diagnose the failure

For each symptom, name the most likely cause and the first command you run.

Symptom Likely cause First step
Timed out while waiting for the machine to boot Virtualization disabled or conflict, too little memory Set vb.gui = true and look at the console
Vagrant cannot forward the specified ports The host port is already in use lsof -i :8080 or netstat -ano
unknown configuration section 'hostmanager' Plugin not installed vagrant plugin list
/bin/bash^M: bad interpreter CRLF line endings Add .gitattributes with eol=lf
Shared folder is empty or fails to mount Guest Additions mismatch Install vagrant-vbguest or switch to rsync

See debugging and common errors for details.

Challenge 10: capstone

Build, from scratch, a repository that contains:

  • a Vagrantfile defining lb, two web machines and one db from a YAML file;
  • idempotent shell provisioners per role;
  • a README.md with prerequisites, the commands to start, test and reset;
  • .gitignore and .gitattributes appropriate for a team;
  • a pinned box version and a required Vagrant range;
  • a trigger that backs up the database before destroy.

Then hand it to a friend, with no help, and see whether vagrant up works first time. If it does not, the README or the Vagrantfile is missing something. Compare with the three-tier project.

A stand-in: self-check for a Vagrantfile

This Ruby program applies a few of the checks from the challenges to Vagrantfile text, the way you might in a Git hook. Feed it a good file and a bad one.

Ruby Vagrantfile self-check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Where to go next

  • Add Ansible or Chef as a provisioner and reuse the same code on real servers.
  • Build your base box with Packer (packaging boxes).
  • Try the Docker provider for fast single-service machines (Docker provider).
  • Contribute: write a tiny plugin for a task your team repeats (plugins).

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. In challenge 8, why do the machines have no box?

    Machine blocks inherit shared settings at the time they are defined, so shared settings must come first.
  2. 2. Which is the best proof that your environment is reproducible?

    Rebuilding from nothing, with no manual steps, is the real test.
  3. 3. What does a fast failure at the top of a Vagrantfile (require_version, plugin check) save your teammates?

    Clear early errors beat obscure failures deep in a run.

Summary

  • Practice by building, proving each result with a command, and rebuilding from scratch.
  • Typical mistakes: duplicate IPs, secrets in commands, settings declared after define, non-idempotent scripts, missing plugins.
  • A good Vagrant repository is data-driven, pinned, documented and backed up by triggers.
  • Automate the checks you keep repeating, in a script or a hook.

Back to the track: Vagrant