22

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?

2 Answers 2

17

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 {}
3
  • 1
    Can you explain each piped section of the code? So others can adapt it to their use case... Would be awesome. :)
    – Jack_Hu
    Commented Aug 30, 2018 at 15:47
  • I can actually answer this. First run grep config.vm.define Vagrantfile to see each of your config.vm.defines return each their own line. You can then run grep config.vm.define Vagrantfile | awk -F'"' '{print $2}' to see that awk strips out everything before and after the character \". finally xargs takes each line, now naming each machine and passes it to vagrant up in parrallel Commented Mar 24, 2022 at 19:35
  • I used this a lot, and made it a little easier to repeat by just copypasta of this command to an executable file in a local user PATH'd directory. Very nice! Thank you. Commented Mar 12 at 0:32
8

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.
4
  • 3
    +1 for keeping the site tidy.
    – jww
    Commented Dec 9, 2019 at 15:06
  • Clever awk program! It's worth noting the -P argument can be used to increase the parallelism by using a higher number. Commented Feb 11, 2020 at 16:36
  • I tested this - it wont work if you have to download a new/updated box image. Both threads will attempt to do this at once, and one will fail. It would be good if we could run a single thread dependency to check if the box image is present first, and if not to aquire it. Otherwise, code using this method wont be easily shared with others. It's a great help for virtual box users though! Commented May 17, 2020 at 8:26
  • It would be grateful if you would share your suggested code-fix :)
    – weshouman
    Commented May 19, 2020 at 3:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.