John Bachir
John Bachir

Reputation: 22711

How can I have Vagrant's provision fail if a subcommand fails?

If one of the lines in config.vm.provision "shell" fails, Vagrant keeps going forward with the rest of the script. How can I make any error cause the provision process to fail?

Upvotes: 4

Views: 1398

Answers (2)

russau
russau

Reputation: 9088

I want exactly this. Looks like it's not possible so I've settled for a panicky banner instead. The child scripts contain set -e to exit if anything fails.

Vagrantfile:

config.vm.provision "shell", path: "setup-files/parent.sh"`

parent.sh:

#!/bin/bash
/home/vagrant/setup-files/child1.sh &&
/home/vagrant/setup-files/child2.sh 

if [ $? -ne 0 ]; then # If: last exit code is non-zero
    echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    echo An error occurred running build scripts
    echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fi

Upvotes: 0

Frederic Henri
Frederic Henri

Reputation: 53703

I dont think vagrant provides for an option on the shell provisioning but it can be managed within your script itself by using The Set Builtin

#!/bin/bash    
set -e
.... rest of your commands - first command which fails will break the script and exits ...

Upvotes: 1

Related Questions