Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable to a shell script provisioner in vagrant

I'm using a simple shell script to provision software for a vagrant setup as seen here.

But can't figure out a way to take the command line arguments passed in to vagrant and send them along to an external shell script. Google reveals that this was added as a feature but I can't find any documentation covering it or examples out there.

like image 280
user1391445 Avatar asked Mar 17 '13 14:03

user1391445


Video Answer


2 Answers

You're correct. The way to pass arguments is with the :args parameter.

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

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

$ bootstrap.sh 'first arg' second 

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

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

like image 94
Johann Avatar answered Sep 29 '22 00:09

Johann


Indeed, it doesn't work with variables! The correct snytax is :

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

and then, in the shell setup.sh:

echo "### $1 - $2"  > ### 192.168.50.4 - my_server 
like image 38
Ivan Avatar answered Sep 29 '22 01:09

Ivan