Skip to content
Home » Quickly Provision Three VMs with Vagrant

Quickly Provision Three VMs with Vagrant

Vagrant is a popular tool for developers to easily manage virtual machines. It provides a convenient way to create, configure and manage virtual environments. In this tutorial, we will walk through the process of creating a Vagrantfile to spin up three virtual machines in a private network. The virtual machines will use the following boxes:

  • generic/rocky9
  • generic/ubuntu2010
  • generic/arch

Prerequisites

Setting up a Private Network

In Vagrant, a private network allows virtual machines to communicate with each other while being isolated from the host machine and other networks. To create a private network, we can use the config.vm.network configuration in the Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "generic/rocky9"
  config.vm.network "private_network", ip: "192.168.33.10"
end

In this example, we set the box to generic/rocky9 and specify a private network with an IP address of 192.168.33.10.

Adding Multiple VMs

To add multiple virtual machines to the same private network, we can use the config.vm.define block to specify the name of each virtual machine and its configuration.

Vagrant.configure("2") do |config|
  config.vm.define "rocky9" do |rocky9|
    rocky9.vm.box = "generic/rocky9"
    rocky9.vm.network "private_network", ip: "192.168.33.10"
  end

  config.vm.define "ubuntu2010" do |ubuntu2010|
    ubuntu2010.vm.box = "generic/ubuntu2010"
    ubuntu2010.vm.network "private_network", ip: "192.168.33.11"
  end

  config.vm.define "arch" do |arch|
    arch.vm.box = "generic/arch"
    arch.vm.network "private_network", ip: "192.168.33.12"
  end
end

In this example, we define three virtual machines named rocky9, ubuntu2010 and arch. Each virtual machine has a unique IP address within the same private network.

Provisioning the VMs

Vagrant also provides a way to automate the configuration and setup of virtual machines through provisioning. This can be done with shell scripts, Ansible playbooks, Chef cookbooks, and more. For this tutorial, we will use a simple shell script.

Vagrant.configure("2") do |config|
  config.vm.define "rocky9" do |rocky9|
    rocky9.vm.box = "generic/rocky9"
    rocky9.vm.network "private_network", ip: "192.168.33.10"
    rocky9.vm.provision "shell", inline: <<-SHELL
      echo "Updating package manager for Rocky9"
      sudo dnf update -y
      sudo dnf upgrade -y
    SHELL
  end

  config.vm.define "ubuntu2010" do |ubuntu2010|
    ubuntu2010.vm.box = "generic/ubuntu2010"
    ubuntu2010.vm.network "private_network", ip: "192.168.33.11"
    ubuntu2010.vm.provision "shell", inline: <<-SHELL
      echo "Updating package manager for Ubuntu2010"
      sudo apt update -y
      sudo apt upgrade -y
    SHELL
  end

  config.vm.define "arch" do |arch|
    arch.vm.box = "generic/arch"
    arch.vm.network "private_network", ip: "192.168.33.12"
    arch.vm.provision "shell", inline: <<-SHELL
      echo "Updating package manager for Arch"
      sudo pacman -Syu
    SHELL
  end
end

In this example, we added a provisioning script for each virtual machine that updates and upgrades the package manager for each operating system.

5 Tips for Creating a Vagrantfile

  1. Start simple and add complexity as needed.
  2. Use version control to keep track of changes to your Vagrantfile.
  3. Test your Vagrantfile on different hosts and providers to ensure compatibility.
  4. Use provisioning scripts to automate the setup and configuration of your virtual machines.
  5. Take advantage of Vagrant’s built-in features, such as synced folders and port forwarding, to make development easier.

Summary

In this tutorial, we covered the process of creating a Vagrantfile to spin up three virtual machines in a private network. We used boxes generic/rocky9, generic/ubuntu2010 and generic/arch and provided a basic provisioning script for each virtual machine. We also provided 5 tips for creating a Vagrantfile.

For further reading, consider exploring advanced Vagrant features such as synced folders and port forwarding. Additionally, consider looking into provisioning tools such as Puppet, Ansible and Chef to automate the setup and configuration of your virtual machines.

Challenge

Try to spin up the virtual machines from the example Vagrantfile and verify that each virtual machine has the correct IP address and provisioning message.

Leave a Reply

Your email address will not be published. Required fields are marked *

one × 5 =