Vagrant - Creating Multi VMs
Vagrant is able to define and control multiple guest machines per Vagrantfile. This is known as a "multi-machine" environment. In this post, I'll show you how to deploy multiple VMS using a single vagrant file. You can read more about it in the official vagrant documentation.

Let's start with the basics, copy the code from the vagrant site into notepad:
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "echo Hello" #will run commands on all machines in this file
config.vm.define "web" do |web|#VM No'1
web.vm.box = "apache"
end
config.vm.define "db" do |db|#VM No'2
db.vm.box = "mysql"
end
end
Let's start with basic changes such as VM names, Memory, and CPUs:
Vagrant.configure("2") do |config|
#config.vm.provision "shell", inline: "echo Hello" #will run commands on all machines in this file
config.vm.define "web01" do |web01|#VM No'1
web01.vm.box = "ubunto/bionic64" #Setting machine type
web01.vm.network "private_network", ip: "192.168.33.22"#Set static IP
web01.vm.provider "virtualbox" do |vb| #Set Memory and CPUs
vb.memory = "1024"
vb.cpus = 2
end
end
config.vm.define "db01" do |db01|#VM No'2
db01.vm.box = "geerlingguy/centos7"#Setting machine type
db01.vm.network "private_network", ip: "192.168.33.23"#Set static IP
db01.vm.provider "virtualbox" do |vb| #Set Memory and CPUs
vb.memory = "1024"
vb.cpus = 2
end
end
end
Adding provisioning code to a specific machine (we can add specific code per machine):
Vagrant.configure("2") do |config|
#config.vm.provision "shell", inline: "echo Hello" #will run commands on all machines in this file
config.vm.define "web01" do |web01|#VM No'1
web01.vm.box = "ubunto/bionic64" #Setting machine type
web01.vm.network "private_network", ip: "192.168.33.22"#Set static IP
web01.vm.provider "virtualbox" do |vb| #Set Memory and CPUs
vb.memory = "1024"
vb.cpus = 2
end
end
config.vm.define "db01" do |db01|#VM No'2
db01.vm.box = "geerlingguy/centos7"#Setting machine type
db01.vm.network "private_network", ip: "192.168.33.23"#Set static IP
db01.vm.provider "virtualbox" do |vb| #Set Memory and CPUs
vb.memory = "1024"
vb.cpus = 2
end
config.vm.provision "shell", inline: <<-SHELL
yum install httpd wget unzip -y
systemctl start httpd
systemctl enable httpd
cd /tmp/
wget https://www.tooplate.com/zip-templates/2119_gymso_fitness.zip
unzip -o 2119_gymso_fitness.zip
cp -r 2119_gymso_fitness/* /var/www/html/
systemctl restart httpd
SHELL
end
end
Now its time to test it:


Normally, we would use the vagrant up command to start a VM in a specific directory, but now that we have multiple VMs, we must also specify the machine's name:
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant/multivms
$ vagrant ssh db01
[vagrant@localhost ~]$ exit
logout
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant/multivms
$ vagrant ssh web01
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 4.15.0-180-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun Jun 12 16:15:46 UTC 2022
System load: 0.0 Processes: 98
Usage of /: 2.9% of 38.71GB Users logged in: 0
Memory usage: 13% IP address for enp0s3: 10.0.2.15
Swap usage: 0% IP address for enp0s8: 192.168.33.22
0 updates can be applied immediately.
New release '20.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.