Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 51Vagrant · IaC and automation

Install and set up Vagrant (local lab VMs)

Vagrant builds virtual machines on your own computer from one small file, the Vagrantfile. One command starts a full lab — for example a RHEL-like server, two web servers and a database — and another command deletes it. It is the classic way to practise Linux, Ansible, Kubernetes and the RHCSA tasks, and to test changes before they reach real servers. This guide installs Vagrant with VirtualBox or libvirt (KVM) on RHEL and Ubuntu, writes a multi-machine lab with Ansible provisioning, and covers boxes, snapshots and the common errors.

  • 9 Lessen
  • 9 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

51.1

Know it before you install

A box is a ready base image (for example bento/rockylinux-9). A provider runs the VMs (VirtualBox, libvirt/KVM, VMware, Hyper-V). Provisioners set up the VM after the first boot (shell, Ansible). Vagrant gives each VM SSH access, a synced folder and a private network.

ItemValue
Packagevagrant (HashiCorp repository)
Project fileVagrantfile (Ruby syntax)
Boxes~/.vagrant.d/boxes/
Project state.vagrant/ in the project folder (do not commit)
ProvidersVirtualBox (virtualbox), KVM (vagrant-libvirt plugin)
NeedsCPU virtualization (VT-x/AMD-V) on, 8+ GB RAM for small labs

Notitie

Vagrant uses the BSL license since 2.3.8. Many teams also use cloud VMs or containers for labs — Vagrant shines when you need real VMs offline.

51.2

Install on RHEL, Rocky, Alma and Fedora

Vagrant from the HashiCorp repo, and libvirt/KVM as provider (VirtualBox is not in RHEL):

dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf -y install vagrant
dnf -y install @virtualization libvirt-devel gcc make
systemctl enable --now libvirtd
usermod -aG libvirt alice
vagrant plugin install vagrant-libvirt
vagrant --version

Log out and in again so the libvirt group works.

51.3

Install on Ubuntu and Debian

curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/hashicorp.list
apt-get update
apt-get install -y vagrant virtualbox
vagrant --version

For KVM on Ubuntu: apt-get install -y qemu-kvm libvirt-daemon-system libvirt-dev, add your user to libvirt, then vagrant plugin install vagrant-libvirt.

Check virtualization is on: grep -cE 'vmx|svm' /proc/cpuinfo must be more than 0.

51.4

First configuration: a multi-machine lab

One control node and two web servers on a private network, provisioned by Ansible:

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "bento/rockylinux-9"
  config.vm.box_check_update = false
  config.ssh.insert_key = true
 
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024
    vb.cpus = 1
    vb.linked_clone = true
  end
  config.vm.provider "libvirt" do |lv|
    lv.memory = 1024
    lv.cpus = 1
  end
 
  (1..2).each do |i|
    config.vm.define "web#{i}" do |web|
      web.vm.hostname = "web#{i}.lab"
      web.vm.network "private_network", ip: "192.168.56.1#{i}"
    end
  end
 
  config.vm.define "db" do |db|
    db.vm.hostname = "db.lab"
    db.vm.network "private_network", ip: "192.168.56.20"
    db.vm.provider("virtualbox") { |vb| vb.memory = 2048 }
    db.vm.provision "ansible" do |ansible|
      ansible.playbook = "site.yml"
      ansible.limit = "all"
      ansible.groups = { "web" => ["web1", "web2"], "db" => ["db"] }
    end
  end
end
SettingWhy
config.vm.boxThe base image; pin a version with config.vm.box_version for repeatable labs.
private_network + fixed IPsVMs talk to each other and to your host on a lab network.
memory, cpus per providerLabs fit in your RAM.
linked_clone (VirtualBox)Faster start, less disk.
Ansible provisioner on the last VMRuns once after all VMs are up, with limit = "all" and groups.
vagrant validate
vagrant up
vagrant up --provider=libvirt
vagrant status
web1   running (virtualbox)
web2   running (virtualbox)
db     running (virtualbox)

51.5

Daily use: the commands and the real uses

vagrant ssh web1
vagrant ssh-config >> ~/.ssh/config
vagrant provision db
vagrant reload web2 --provision
vagrant snapshot save db clean
vagrant snapshot restore db clean
vagrant halt
vagrant destroy -f
vagrant box list
vagrant box update
vagrant box prune
vagrant global-status --prune

Real uses at work:

  • RHCSA / Linux practice: break and fix a VM, then restore a snapshot.
  • Test Ansible roles and playbooks on clean VMs before production.
  • Small multi-node labs (Kubernetes with kubeadm, HAProxy + web servers, database replicas).

51.6

Make it safe

Security checklist:

  • Use boxes from trusted publishers (for example bento/…, official distro boxes); pin versions.
  • config.ssh.insert_key = true (default) — each VM gets its own key instead of the public insecure key.
  • Keep lab networks private (private_network); do not forward lab ports to 0.0.0.0 on shared networks.
  • No real passwords or production data in Vagrantfiles or synced folders; add .vagrant/ to .gitignore.
  • Delete labs you do not use (vagrant destroy, vagrant box prune) to free disk.

51.7

Update and remove

dnf -y upgrade vagrant
apt-get install --only-upgrade -y vagrant
vagrant plugin update
dnf -y remove vagrant
rm -rf ~/.vagrant.d

51.8

Fix common problems

ProblemCauseFix
VT-x is disabled in the BIOS / VM does not startVirtualization off.Turn on VT-x/AMD-V in BIOS/UEFI; on a cloud VM use a type with nested virtualization.
VirtualBox: The IP address configured for the host-only network is not within the allowed rangesVirtualBox allows only 192.168.56.0/21 by default.Use 192.168.56.x, or allow ranges in /etc/vbox/networks.conf.
VirtualBox kernel module will not loadSecure Boot or missing kernel headers.Install headers and vboxconfig; sign the module or turn off Secure Boot.
libvirt: Permission denied / Call to virConnectOpen failedYour user is not in libvirt.usermod -aG libvirt USER, log in again.
vagrant up hangs at "Waiting for SSH"Network or the box did not boot.vagrant up --debug; open the provider console; try another box version.
The box 'x' could not be foundWrong name or no box for your provider.Search Vagrant Cloud for a box with your provider (virtualbox/libvirt).
VirtualBox and KVM fightBoth hypervisors loaded.Use one; unload kvm modules when using VirtualBox (or use libvirt).

51.9

Next steps

1Where do you put the Ansible provisioner in a multi-machine Vagrantfile, and why?

On the last VM, with limit = "all", so it runs once after all VMs exist.

2How do you go back to a clean VM after a broken exercise?

Alle commando's in dit hoofdstuk