in a Vagrantfile, I define a local variable. How can pass it to a bash script as a parameter? $var, #var, #{var}?

144 Views Asked by At

In a Vagrantfile, I want to run scripts.
But some part of them shouldn't run in provision mode.

I've sat a local variable update = "true" (or false) depending of the value of the Vagrant command.

A puts shows me that it is valued by "false", by example,
but then, I'm unable to send its content to a bash script, for parameter.

I've tried $update, #update, #{update}, update without success
on the line bash common/300_Kafka.sh "3.4.0" "2.12" $update

Part of the Vagrantfile:

if ARGV[0] == "provision"
  update = "true"
else
  update = "false"
end

puts "Vagrant box. Démarrage en mode mise à jour : " + update

config.vm.provision "file", source: "../common", destination: "common"

config.vm.provision "shell", privileged: false, inline: <<-SHELL
  bash common/050_Certificats.sh
  bash common/070_Utilitaires.sh
  bash common/100_JDK.sh "17"
  bash common/150_Postgresql-Postgis.sh "15" "3" "postgres"
  bash common/200_NodeJS-Angular.sh "16"
  bash common/250_ELK.sh "7.x"
  bash common/300_Kafka.sh "3.4.0" "2.12" $update
SHELL

Message on console:

Vagrant box. Démarrage en mode mise à jour : false

default: Indiquez si Kafka est en installation initiale (false) ou update (true).  n'est pas accepté.

The bash script called, testing the values:

#!/bin/bash
# $1: Version de Kafka [3.4.0]
# $2: Version de Scala associée [2.12]
# $3: Mode update (true) ou installation initiale (false)
echo "300 : Installation de Kafka $1 basé sur Scala $2"
export DEBIAN_FRONTEND=noninteractive

if [ -z "$1" ]; then
  echo "Précisez la version de Kafka à installer. [Recommandée : 3.4.0]" >&2;
  exit 1;
fi

if [ -z "$2" ]; then
  echo "Précisez la version de Scala liée. [Recommandée : 2.12]" >&2;
  exit 1;
fi

if [ "$3" != "true" ] && [ "$3" != "false" ]; then
  echo "Indiquez si Kafka est en installation initiale (false) ou update (true). $3 n'est pas accepté." >&2;
  exit 1;
fi

update=$3

if [ "$update" == "false" ]; then
   cd /tmp && wget -q "https://downloads.apache.org/kafka/$1/kafka_$2-$1.tgz"
   sudo tar -xf "kafka_$2-$1.tgz" -C /opt
   echo "export KAFKA_HOME=/opt/kafka_$2-$1" >> ~/.profile
else
   echo "En mode update, Kafka ne se réinstallera pas."
fi
2

There are 2 best solutions below

3
Marc Le Bihan On

My way of sending parameters to shell script isn't correct.
It looks working but doesn't perfectly, inside of an inline <<-SHELL.

I found myself successful with that, instead:

if ARGV[0] == "provision"
  update = "true"
else
  update = "false"
end

puts "Vagrant box. Démarrage en mode mise à jour : " + update

config.vm.provision "file", source: "../common", destination: "common"

config.vm.provision "shell", privileged: false, path: "../common/050_Certificats.sh"
config.vm.provision "shell", privileged: false, path: "../common/070_Utilitaires.sh"
config.vm.provision "shell", privileged: false, path: "../common/100_JDK.sh", args: ["17"]
config.vm.provision "shell", privileged: false, path: "../common/150_Postgresql-Postgis.sh", args: ["15", "3", "postgres"]
config.vm.provision "shell", privileged: false, path: "../common/200_NodeJS-Angular.sh", args: ["16"]
config.vm.provision "shell", privileged: false, path: "../common/250_ELK.sh", args: ["7.x"]
config.vm.provision "shell", privileged: false, path: "../common/300_Kafka.sh", args: ["3.4.0", "2.12", "#{update}"]
3
spickermann On

I would try

bash common/300_Kafka.sh "3.4.0" "2.12" "#{update}"

because from the error message it seems like it explicitly expects the string "true" or "false".