Vagrant create multiple VMs loop

In this tutorial, How do I use Vagrant create multiple VMs? Multiple Vagrant Virtual Machine on Vagrantfile. How to create Multiple VMs with multiple IP loop. Vagrant the essential for DevOps Roles.

Vagrant create multiple VMs loops

For example, I creating two servers with Vagrantfile as below

Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.vm.provider :virtualbox do |vb|
  vb.memory = 256
  vb.cpus = 1
end
# Application server 1.
config.vm.define "app1" do |app1|
  app1.vm.hostname = "app1.dev"
  app1.vm.box = "centos/7"
  app1.vm.network :private_network, ip: "192.168.3.4"
end
# Database server.
config.vm.define "db" do |db|
  db.vm.hostname = "db.dev"
  db.vm.box = "centos/7"
  db.vm.network :private_network, ip: "192.168.3.5"
end

Now, How to Vagrant create N Virtual Machine with different IPs in a loop.

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

config.ssh.insert_key = false
config.vm.provider :virtualbox do |vb|
  vb.memory = 256
  vb.cpus = 1
end

#Disabling the default /vagrant share
config.vm.synced_folder ".", "/vagrant", disabled: true
MACHINE = ["app1.dev","db.dev"]
N = 1

(0..N).each do |i|
config.vm.define "server#{i}" do |node|
  node.vm.hostname = MACHINE[i]
  node.vm.box = "centos/7"
  node.vm.network :private_network, ip: "192.168.3.#{10+i}"
end
end

Note: Change N and MACHINE variable is right for you

To run the first vagrant up a Virtual Machine, to starts vagrant environment

[huupv@huupv example01]$ vagrant up

The result, Vagrant create multiple VMs loops

Vagrant create multiple vms loop

Conclusion

Thought the article, you can use Vagrant create multiple VMs loops as above. I hope will this your helpful.

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

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