• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Create multiple virtual machine with one Vagrantfile

June 7, 2020 by Sharad Chhetri Leave a Comment

For doing POC and testing sometimes we need multiple Virtual Machines very quickly. So here in this post we will create multiple virtual machine with one Vagrantfile by using Vagrant. We will cover some examples from coding side in Vagrantfile, it will help you in your real time scenarios as well.

As we know Vagrant is written in Ruby language, so we will use some Ruby code in Vagrantfile.
We use the Multi VMs for various technology POC like system Cluster,High Availability, automation tool (like chef,puupet and ansible) etc. etc.

Example 1: With basic Vagrantfile
Example 2: Using loop in Vagrantfile
Example 2: Nested loop-conditional statement in Vagrantfile

Syntax:
Block_Name.vm.define : To define the new VM, use this code. You will see more examples and will understand how to define this.

Create Multi VM With basic Vagrantfile

In this scenario,we will create three VM from single Vagrantfile. We will create VMs for Web Server, MySQL Server and FTP server. It will just create the 3 Virtual Machines with different Operating System.

mkdir -p ~/vagrant_box/example1 && cd ~/vagrant_box/example1 
# create file
vi Vagrantfile
#paste the given below content in Vagrantfile
Vagrant.configure("2") do |config|
  #config.vm.box = "base"

  config.vm.define "web" do |web|
    web.vm.provider "virtualbox" do |vb_web|
      vb_web.memory = 1024
      vb_web.cpus = 2
    end

    web.vm.box = "centos/8"
    web.vm.hostname = "web01"
    web.vm.network "private_network", ip: "192.168.33.10"

  end

  config.vm.define "mysql" do |mysql|
    mysql.vm.box = "debian/buster64"
    mysql.vm.hostname = "mysql01"
    mysql.vm.network "private_network", ip: "192.168.33.11"

  end

  config.vm.define "ftp" do |ftp|
    ftp.vm.box = "freebsd/FreeBSD-12.1-STABLE"
    ftp.vm.hostname = "ftp01"
    ftp.vm.network "private_network", ip: "192.168.33.12"
  end

end
# now create multi VM
vagrant up

Create Multi VM using loop in Vagrantfile

Here, also we will create 3 Virtual Machines. We have defined the array vm_name and then used the ruby loop to create all these 3 Virtual Machines. One thing to notice that all these VMs will have same Operating System.

mkdir -p ~/vagrant_box/example2 && cd ~/vagrant_box/example2 
# create file
vi Vagrantfile
Vagrant.configure("2") do |config|
  vm_name=['web01','mysql01','ftp01']
  vm_name.each do |i|
      config.vm.define "#{i}" do |node|
      node.vm.box = "centos/8"
    end
  end
end
# now create multi VM
vagrant up

This is another example of loop but here the VM name will be like node-1, node-2 and node-3 .

mkdir -p ~/vagrant_box/example3 && cd ~/vagrant_box/example3 
# create file
vi Vagrantfile
(1..3).each do |i|
  config.vm.define "node-#{i}" do |node|
    node.vm.box = "centos/8"
  end
end
# now create multi VM
vagrant up

Create Multi VM using nested loop and conditional statement in Vagrantfile

Because we can use ruby code in Vagrant, we add some Nested loop and condition in this example.
Here, if vm_name is ftp01 OR web01 it will create Virtual Machine with CentOS 8 operating system else create Virtual Machine with CentOS 7 Operating System.

mkdir -p ~/vagrant_box/example4 && cd ~/vagrant_box/example4 
# create file
vi Vagrantfile
Vagrant.configure("2") do |config|
  vm_name=['web01','mysql01','ftp01']
  vm_name.each do |i|
    if "#{i}" == 'ftp01' OR "#{i}" == 'web01' then
      config.vm.define "#{i}" do |node|
        node.vm.box = "centos/8"
      end
    else
      config.vm.define "#{i}" do |node|
        node.vm.box = "centos/7"
      end
    end
  end
end
# now create multi VM
vagrant up

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Create single node Virtual Machine with Vagrant
  2. Xen Virtual Machine Migration (In Hyper-VM)
  3. Virtual Machine inaccessible status on VirtualBox 4.3
  4. How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4
  5. hash:/etc/postfix/virtual is unavailable. open database /etc/postfix/virtual.db: No such file or directory
  6. Linux enable or disable multiple swap space
  7. How to configure multiple mysql instance in Ubuntu
  8. How to create Jenkins user by command line and GUI
  9. grep command to find multiple strings or keyword from file
  10. How to configure ethernet in CentOS 6 after installing in Virtual Box

Filed Under: Devops, Linux Tagged With: devops, vagrant

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

Head command in Linux / Unix

How to list users above or below particular user id

create a system account below uid 500 on RHEL/CentOS/Scientific Linux

How to show user account password expiration detail on Linux

Learn Linux Date Command With Examples

Learn tar command and know about its precaution

How To Find Absolute Full Path Of Command On Linux / Unix : which command

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

30 useful Linux terminal keyboard shortcuts

Replace keyword with its filename without extension : bash script

How to create a file with cat command

Yum Error database disk image is malformed

How to convert rpm file into deb file

postgres database backup script using database user password inside

How to encode and decode the strings with base64

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy