Passing a variable to the shell script proviser in the wander

I am using a simple shell script to install the software for configuring vagrants, as shown here .

But cannot find a way to pass command line arguments passed to the tramp and send them to the outer shell of the script. Google shows that this was added as a function, but I cannot find any documentation covering it, or examples there.

+19
parameter-passing vagrant provisioning
Mar 17 '13 at 14:32
source share
5 answers

You're right. The argument passing method is a parameter :args .

 config.vm.provision :shell, :path => "bootstrap.sh", :args => "'first arg' second" 

Note that single quotes around first arg are only needed if you want to include spaces in the part of the passed argument. That is, the above code is equivalent to entering the following in the terminal:

 $ bootstrap.sh 'first arg' second 

Where inside the script, $ 1 refers to the string "first arg", and $ 2 refers to the string "second".

The v2 docs can be found here: http://docs.vagrantup.com/v2/provisioning/shell.html

+23
May 24 '13 at 20:15
source share

Indeed, it does not work with variables! Correct snytax:

 var1= "192.168.50.4" var2 = "my_server" config.vm.provision :shell, :path => 'setup.sh', :args => [var1, var2] 

and then in the setup.sh shell:

 echo "### $1 - $2" > ### 192.168.50.4 - my_server 
+6
May 08 '16 at 20:00
source share

Answering my own question based on some information I found in the old version of the docs page:

 config.vm.provision :shell, :path => "bootstrap.sh", :args => "'abc'" 

- @ user1391445

+1
Jun 16 '16 at 0:47
source share

Here is an alternative way to pass variables from the environment:

 config.vm.provision "shell" do |s| s.binary = true # Replace Windows line endings with Unix line endings. s.inline = %Q(/usr/bin/env \ TRACE=#{ENV['TRACE']} \ VERBOSE=#{ENV['VERBOSE']} \ FORCE=#{ENV['FORCE']} \ bash my_script.sh) end 

Usage example:

 TRACE=1 VERBOSE=1 vagrant up 
+1
Jun 16 '16 at 0:50
source share

To add explicit arguments, I used this successfully:

 config.vm.provision "shell", path: "provision.sh", :args => "--arg1 somearg --arg2 anotherarg" 
0
Sep 07 '17 at 10:23
source share



All Articles