How To Launch Multiple VM’s With Single Vagrantfile

Often times you might want to launch more than one VM of a related application with different configurations. In this case, you don’t have to create multiple vagrant configurations. You can use a single Vagrantfile with multiple configurations as shown below. The best part is you can start VM’s individually rather than starting all VM’s at once.

An example configuration which launches three VM’s is shown below.

Vagrant.configure("2") do |config|

config.vm.provision "shell", inline: "sudo yum update -y"
  
  config.vm.define "master" do |master|
    master.vm.box = "bento/centos-7.1"
    master.vm.network "private_network", ip: "192.168.5.2"
    master.vm.hostname = "master"
    master.vm.provider "virtualbox" do |vb|
            vb.memory = "2048"
            vb.cpus = "2"
    end  
  end

  config.vm.define "node1" do |node1|
    node1.vm.box = "bento/centos-7.1"
    node1.vm.network "private_network", ip: "192.168.5.3"
    node1.vm.hostname = "node1"
    node1.vm.provider "virtualbox" do |vb|
            vb.memory = "2048"
            vb.cpus = "2"
    end
  end

  config.vm.define "node2" do |node2|
    node2.vm.box = "bento/centos-7.1"
    node2.vm.network "private_network", ip: "192.168.5.4"
    node2.vm.hostname = "node2"
    node2.vm.provider "virtualbox" do |vb|
            vb.memory = "2048"
            vb.cpus = "2"
    end   
  end
end

To start the VM’s, open the terminal and browse to the location which contains the Vagrantfile. execute the following command to bring up the VM’s.

vagrant up

To ssh, you can use the individual VM names mentioned in the configuration.

For example,

vagrant ssh master

 

Other Interesting Blogs

Leave a Comment

0 Shares
Share via
Copy link