bellackn
bellackn

Reputation: 2184

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?

Upvotes: 1

Views: 1472

Answers (2)

fernandezcuesta
fernandezcuesta

Reputation: 2448

After landing here I realized you can actually assign the output of a command exit code as in ruby:

FOO = `mycommand`
BAR = $?.exitstatus

Then use it in Vagrantfile as any other variable.

Upvotes: 0

bellackn
bellackn

Reputation: 2184

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.

Upvotes: 0

Related Questions