Modular vagrant file
As the number of VMโs required increased in my Vagrant file it started growing very large. One approach to simplify this would be having a directory with all the config files .rb, explicitly call them. This also saves additional steps of cd to directories, running vagrant up.
vagrant/
โโโ Vagrantfile
โโโ vagrant.d/
โ โโโ 00-provider-libvirt.rb
โ โโโ 10-resources-small.rb
โ โโโ 10-resources-medium.rb
โ โโโ 10-resources-large.rb
โ โโโ 20-disks.rb
โ โโโ 30-networks.rb
โ โโโ vm-chef-server.rb
โ โโโ vm-k3s.rb
โ โโโ vm-eve-ng.rb
config.vm.provider :libvirt do |lv|
lv.driver = "kvm"
lv.cpu_mode = "host-passthrough"
lv.nested = true
lv.graphics_type = "none"
lv.video_type = "virtio"
lv.disk_bus = "virtio"
lv.nic_model_type = "virtio"
lv.memorybacking :access, :mode => "shared"
end{
cpus: 1,
ram: 1024
}{
cpus: 2,
ram: 2048
}{
cpus: 4,
ram: 8192
}{
small: 10,
medium: 20,
large: 40
}config.vm.define "eve-ng" do |vm|
vm.vm.hostname = "eve-ng"
vm.vm.box = "generic/ubuntu2204"
profile = LARGE
disk = DISKS[:large]
vm.vm.provider :libvirt do |lv|
lv.cpus = profile[:cpus]
lv.memory = profile[:ram]
lv.storage :file, size: "#{disk}G"
end
endconfig.vm.define "k3s" do |vm|
vm.vm.hostname = "k3s"
vm.vm.box = "almalinux/8"
profile = MEDIUM
disk = DISKS[:large]
vm.vm.provider :libvirt do |lv|
lv.cpus = profile[:cpus]
lv.memory = profile[:ram]
lv.storage :file, size: "#{disk}G"
end
endconfig.vm.define "chef-server" do |vm|
vm.vm.hostname = "chef-server"
vm.vm.box = "almalinux/8"
profile = MEDIUM
disk = DISKS[:large]
vm.vm.provider :libvirt do |lv|
lv.cpus = profile[:cpus]
lv.memory = profile[:ram]
lv.storage :file, size: "#{disk}G"
end
endCaveats: remember to keep hostnames unique in the vagrant files.
Related Articles
Modular vagrant file
How to structure a modular Vagrantfile using separate .rb config files for resources, disks, and networks.
How We Set Up Our KVM Hypervisor: From Bare Metal to Production-Ready VM Host
Detailed walkthrough of building a dedicated KVM/libvirt hypervisor with XFS tuning, hugepages, 10GbE tuning, and automation.
Building a Predictable KVM Infrastructure: From Chaos to Control
How to engineer a predictable KVM-based infrastructure focusing on repeatability, observability, and operational safety.