Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop vagrant provisioning in multi-machine environments to switch back and forth between machines?

I have a multi-machine Vagrantfile setting up a 5 node environment.

I've been looking around to see what levels of control you have over the order of provisioning, but it's pretty limited:

https://docs.vagrantup.com/v2/multi-machine/

I want to provision 5 nodes, then go back to the first node, and run other provisioning steps there.

What I mean is that you have a Vagrantfile like this:

Vagrant.configure('2') do |config|
   config.vm.provision some stuff

   config.vm.define 'node1' do |node1|
      node1.vm.provision some more stuff
   end

   config.vm.define 'node2' do |node2|
      node2.vm.provision some other stuff
   end

   ... node3 node4 node 5 ...
end

But after vagrant has finished starting and provisioning the all the machines up to node5, I then want to run another provisioner on node1. Does anyone know how to do this? Maybe some ruby hackery?

like image 665
philbert Avatar asked Jul 31 '14 15:07

philbert


People also ask

What is a multi-machine environment in Vagrant?

Vagrant is able to define and control multiple guest machines per Vagrantfile. This is known as a "multi-machine" environment. These machines are generally able to work together or are somehow associated with each other.

How do I use multiple machines in Vagrant?

Using the multi-machine feature of Vagrant, these environments can be modeled in the context of a single Vagrant environment without losing any of the benefits of Vagrant. Multiple machines are defined within the same project Vagrantfile using the config.vm.define method call.

Can we use vagrant create multiple VMs loops?

Thought the article, you can use Vagrant create multiple VMs loops as above. I hope will this your helpful. Thank you for reading the DevopsRoles page! My name is Huu.

Why does my vagrant file have different settings for different machines?

That is because the ordering is outside-in, in the order of the file. If you want to apply a slightly different configuration to multiple machines, see this tip. The moment more than one machine is defined within a Vagrantfile, the usage of the various vagrant commands changes slightly.


1 Answers

If what you want is to have that other provisioner to run automagically right after all the machines have been vagrant uped, unfortunately there's no way to do that as far as I know, Vagrant will allways run all the provisioners specified (unless you tell it to run just a subset of them).

The only way you might be able to emulate would be having different kinds of provisioners for each machine and selectively running them as needed. So, for example, you'd vagrant up --provision --provision-with=shell and then run a vagrant provision --provision-with chef_solo to have the shell provisioners run first and the chef_solo provisioning afterwards

But, if you want to manually fire up a provisioner after all the machines have been brought up you can just use the vagrant provision command to accomplish that.

like image 74
fgrehm Avatar answered Sep 28 '22 20:09

fgrehm