So, sometimes you just need to try and build a multihost cluster for development or testing reasons. What happens if you want to have multiple multihost environments for testing? How can we achieve this with Vagrant? Vagrant does not provide any specific functionality to define multiple environments in the same Vagrantfile. It does however allow multimachines to be defined in the same Vagrantfile.

Vagrant

So the first step is getting Vagrant and configuring a basic vagrant configuration. The idea is the create a Vagrantfile in your base directory which will define all the various hosts that you will want to use. The next step is using the ruby ENV variable which allows Vagrant to use parameters per vagrant run. So the idea is to allow the vagrant up to target a different set of hosts as required. What we will do in this article is configure two different multihost environments that can be used for example clusters.

So the first part of the Vagrantfile will look as follows

hosts_a = [{name: 'test_1'}, {name: 'test_2'}]
hosts_b = [{name: 'test_3'}, {name: 'test_4'}]
if ENV['VAGRANT_HOSTS'].eql? 'A'
  host = hosts_a
elsif ENV['VAGRANT_HOSTS'].eql? 'B'
  host = hosts_b
else
  puts 'Please define the VAGRANT_HOST variable'
end

vagrant(2).do |config|
  hosts.each do |h|
    
  end
end

And the environments can be started separatly by running the following command VAGRANT_HOSTS=A vagrant up or VAGRANT_HOSTS=B vagrant up. This makes it possible to specialize vagrant provisioning based on the requirements of the environments.

Multiple Vagrantfiles

Why not use multiple vagrantfiles instead? It depends on what exactly you are trying to achieve. If the environments are different then it makes much more sense to just have different Vagrantfiles. But, if you need multiple environments for clusters, such as building a zookeeper and kafka cluster you may want different configurations based on the cluster, or you may want to target the hosts in the cluster separately. The reason is that the more differences between development and production, the less valuable the vagrant environment becomes for problem solving. It is also why docker might be a better option in order to reduce the differences between environments.