Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karnzx/ba65d3a24eb390c3277d61fdfd39b4f8 to your computer and use it in GitHub Desktop.
Save karnzx/ba65d3a24eb390c3277d61fdfd39b4f8 to your computer and use it in GitHub Desktop.
auto provision 2 Debian VMs on virtualbox using vagrant

Simple Vagrantfile for create multiple vm with virtualbox provider

what is this Vagrantfile do

Create 2 Linux Debian 12 bookworm VMs

name IP
vm-1 192.168.56.101
vm-2 192.168.56.102

prerequisite

only vagrant, virtualbox installed

For WSL2 require plugin: virtualbox_WSL2 and export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"

Vagrantfile

VM name, image, quantity and IP prefix can dynamically change by edit variables below.

# -*- mode: ruby -*-
# vim: set filetype=ruby :

IMAGE_NAME = "debian/bookworm64"
VM_NAME = "vm"
VM_QUANTITY = 2
IP_PREFIX = "192.168.56" # will be concatenate with .101, .102...VM_QUANTITY+100

Vagrant.configure("2") do |config|
	config.vm.synced_folder ".", "/vagrant", disabled: true
	config.vm.box = IMAGE_NAME

	config.vm.provision "shell", inline: <<-SHELL
		set -o verbose
		apt-get update
		apt-get install -y vim curl git
	SHELL

	config.vm.provider :virtualbox do |vb|
		vb.cpus = 1
		vb.memory = 2048
	end

	(1..VM_QUANTITY).each do |i|
		config.vm.define "#{VM_NAME}-#{i}" do |v|
			v.vm.hostname = "#{VM_NAME}-#{i}"
			v.vm.network "private_network", ip: "#{IP_PREFIX}.#{i + 100}"

			# naming the virtualmachine
			v.vm.provider :virtualbox do |vb|
				vb.name = "#{VM_NAME}-#{i}"
			end
		end
	end
end

create by run command

vagrant up

access vm using vargrant ssh VM_NAME-NUMBER

vagrant ssh vm-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment