Multiple Vagrant VMs in One Vagrantfile

Share It!

With the help of Vagrant, you can create a virtual machine as per requirement. You can also modify the physical properties of this virtual machine such as RAM, CPUs, etc. You can Establish network interfaces so that you can access your virtual machine from your own computer, another device on the same network, or even from another virtual machine.

Multiple Vagrant VMs

See Also:
1. How to Install Vagrant on CentOS 7
2. How configure X11 Forwarding in CentOS/RHEL 6/7
3. How to Set Up a Cookieless Domain in Linux

In this article, we will help you to create multiple Virtual machines using single Vagrantfile.

Base Config:

First we are going to explain the base configuration of of Vagrantfile. In this we are CentOS 7 to built new VM machine.

Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
  config.vm.box = "centos/7"
  config.vm.hostname = 'web.techoism.local'
  config.vm.network "private_network", ip: "172.20.10.13"
  config.vm.network "forwarded_port", guest: 80, host: 81
   
  config.vm.provider "virtualbox" do |v|
     v.memory = 2048
     v.cpus = 2
   end
end
end

config.vm.define: Define VM name
config.vm.box: Every Vagrant development environment requires a box. You can search for boxes at official site.
config.vm.hostname: Create your machine hostname
config.vm.network “private_network”: Create a private network, which allows host-only access to the machine using a specific IP.
config.vm.network “forwarded_port”: Create a forwarded port mapping which allows access to a specific port
v.memory: Customize the amount of memory on the VM
v.cpus: Customize the amount of CPU on the VM

Built Multiple VM:

Vagrant helps us to create two or more VM using single Vagrantfile. While creating the Vagrantfile make sure that you are using a different hostname, IP address and other settings so there aren’t conflicts.

Vagrant.configure("2") do |config|
  config.vm.define "Dev" do |Dev|
    config.vm.box = "centos/7"
    config.vm.hostname = 'web.techoism.local'
    config.vm.network :private_network, ip: "172.20.10.15"

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

  config.vm.define "web" do |web|
    config.vm.box = "centos/7"
    config.vm.hostname = 'qa.techoism.local'
    config.vm.network :private_network, ip: "172.20.10.16"

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

The above Vagrantfile define two VM’s. One VM is for development and another VM is for the web.

Now run below command to built the VM.

# vagrant up

The cool thing is that Vagrant automatically runs most commands on all of the VMs in the Vagrantfile. Below command will tell the status of a vagrant machine.

# vagrant status

Access Vagrant Machine:

Run below command to access the Virtual Machine.

# vagrant ssh

Other Commands:

     box             manages boxes: installation, removal, etc.
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version

Enjoy it!

No Responses

Leave a Reply

Your email address will not be published.