rubyshellvagrantvagrantfilevagrant-provision

Vagrantfile: Assign result of inline shell script to variable


I would like to assign the exit code of an inline shell script in a Vagrantfile to a variable:

FOO = config.vm.provision "shell", 
env: {
  "FILE" => "/tmp/hello"
},
inline: <<-SHELL
  [ -f "$FILE" ]; echo $?
SHELL

and then use it later in my Vagrantfile, like:

if FOO != 0
  ...

The way I tried it obviously doesn't work. Does anyone here know

  1. if this is possible and
  2. if yes, how?

Solution

  • I ended up using an approach like this one.

    Something I discovered along the way is that all Ruby conditionals in the Vagrantfile are evaluated in the very beginning, although the inline Shell scripts inside these conditionals (if there are any) are not evaluated at the same time. This lead to some strange behavior, which I could avoid by doing like I said above.