Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant Multi-Machine Parallel Start

Tags:

vagrant

I'm trying to provision a master-master MySQL pair and they can only be configured correctly if both of them are up.

Vagrant.configure("2") do |config|
  web.vm.box = "centos/7"

  config.vm.define "primary" do |primary|
    .......
  end

  config.vm.define "secondary" do |secondary|
    .......
  end
end

I've run thru this multiple times and Vagrant only starts the second vm after the first one is up.

Is there any way to force Vagrant to start up two VM's at the same time?

like image 390
Ken J Avatar asked Aug 22 '17 17:08

Ken J


2 Answers

vagrant up supports parallel VM start with the key --parallel:

--[no-]parallel Enable or disable parallelism if provider supports it

Default vagrant provider VirtualBox doesn't support it, but you can start your VMs simultaneously using xargs, which supports parallel command execution with the key -P <max-procs> (example provided exactly for your Vagrantfile):

grep config.vm.define Vagrantfile | awk -F'"' '{print $2}' | xargs -P2 -I {} vagrant up {}
like image 123
nickgryg Avatar answered Nov 05 '22 10:11

nickgryg


Vagrantfile could have the boxes in variables, thus statically grepping the Vagrantfile won't succeed in all scenarios.

Following is another approach that captures the names created by vagrant status

vagrant status | \
awk '
BEGIN{ tog=0; }
/^$/{ tog=!tog; }
/./ { if(tog){print $1} }
' | \
xargs -P2 -I {} vagrant up {}

Command breakdown

  • vagrant status gets a report of the machines that are managed by the Vagrantfile, printed as a machine-per-line between 2 empty lines.
  • awk command captures all the machine-lines between the 2 empty lines and prints the first word of each machine-line (which are the machine names).
  • xargs command changes stream into arguments that could be utilized in parallel using the -P option.
like image 6
weshouman Avatar answered Nov 05 '22 08:11

weshouman