Tuesday, 10 March 2020

Multiple Vms Vagrantfile

Multiple Vms Vagrantfile

Multiple Vms Vagrantfile

2020-03-10T17:50:49Z



Introduction

A Vagrantfile for multiple VMS.

Prepare Environment

mkdir Vagrant_VMS
cd Vagrant_VMS
vagrant init

Populate Vagrantfile with following contents.

all_hosts = [
    {
        vagrant_hostname: "amachine",
        full_hostname: "amachine.virtual.machine",
        vmbox: "fedora/31-cloud-base",
        vmbox_version: "31.20191023.0",
        ip: "10.0.0.10",
        memory: 2048,
        cpus: 1
    },
#    {
#        vagrant_hostname: "anotherMachine",
#        full_hostname: "another.virtual.machine",
#        vmbox: "fedora/31-cloud-base",
#        vmbox_version: "31.20191023.0",
#        ip: "10.0.0.12",
#        memory: 2048,
#        cpus: 1
#    },
]

# individual machine names must be mentioned is below command line in
# order to bring machines. (due to autostart: false)
# vagrant up amachine anotherMachine
Vagrant.configure("2") do |config|
    #config.vm.box = "fedora/31-cloud-base"
    #config.vm.box_version = "31.20191023.0"

    all_hosts.each do |host|
        config.vm.define host[:vagrant_hostname], autostart: false do |this_host|
            this_host.vm.network :private_network, ip: host[:ip]
            this_host.vm.hostname = host[:full_hostname]
            this_host.vm.box = host[:vmbox]
            this_host.vm.box_version = host[:vmbox_version]

            this_host.vm.provider "virtualbox" do |m|
                m.memory = host[:memory]
                m.cpus = host[:cpus]
            end
        end
    end
    config.vm.synced_folder ".", "/vagrant", type: "nfs"
end

Start VMs

vagrant up amachine

Note: You need to mention vm name in vagrant up command due to presense of autostart: false in above Vagrantfile.

No comments:

Post a Comment