Ken J
Ken J

Reputation: 4562

Vagrant Multi-Machine Parallel Start

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?

Upvotes: 23

Views: 8813

Answers (2)

weshouman
weshouman

Reputation: 933

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.

Upvotes: 8

nickgryg
nickgryg

Reputation: 28593

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 {}

Upvotes: 18

Related Questions