Vagrant automation: create two machines with debian bulleye image with virtualbox provider

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#3ed72b',
      'primaryTextColor': '#000',
      'primaryBorderColor': '#000',
      'lineColor': '#fff',
      'secondaryColor': '#e6f01b',
      'tertiaryColor': '#fff'
    }
  }
}%%


flowchart LR
   id1[/vagrant/]
   id2[/Vagrantfile /]
   id3[ define machine image Debian]
   id4[/provision/]
   id5[/virtualbox provider/]
   id6[/startup/]
   id7[/shutdown /]
   id8[/destroy/]
   id2-->id3
   id3-->id1
   id1-->id5
   id5-->id4
   id4-->id6
   id4-->id7
   id7-->id8

Overview:

  • install vagrant and virtualbox
  • create new folder and Vagrantfile to provisining machines
  • vagrant provisioning machines
    • start
    • list
    • shutdown
    • destroy

creating new folder and Vagrantfile

create new folder for your project

mkdir project-vagrantboxes

create within your new projectfolder new file Vagrantfile

cd project-vagrantboxes
touch Vagrantfile

now open the new file Vagrantfile with your prefered console editor like vim, nano what else, i use vim

Vagrantfile config file to provision two machines with debian bullseye image

# vim Vagrantfile

Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder "shared/", "/home/vagrant/shared", create: true
config.vm.provision "shell", inline: "echo machines don; ip a"
config.vm.box = "debian/bullseye64"

config.vm.define "m01" do |m01|
m01.vm.hostname = "m01"
m01.vm.network "private_network", ip: "192.168.60.101"
end

config.vm.define "m02", primary: true do |m02|
m02.vm.hostname = "m02"
m02.vm.network "private_network", ip: "192.168.60.102"
end

end

info: validate your Vagrantfile with: vagrant validate

provisioning and startin your machines

vagrant up

list your running machines with virtualbox provider

VBoxManage list runningvms

smilar output:

BoxManage list runningvms
"vagrant_m01_1702138997583_2142" {6924c7f1-531a-4371-b891-5c1476f00829}
"vagrant_m02_1702139034175_69860" {95c67b3f-d0ec-4ea9-8584-00d2d370feab}

check status of machines

vagrant status m01
vagrant status m02

# // or automated
for node in m01 m02; do vagrang status $node;done

now enjoy your machines

vagrant ssh m01   # or m02

shutdown your machines

vagrant halt m01
vagrant halt m02

# // or automated
for node in m01 m02; do vagrant halt $node;done

destroy machines

vagrant destroy m01
vagrant destory m02

# // or automated
for node in m01 m02; do vagrant destroy $node;done

demo