Riccardo Messineo
Riccardo Messineo

Reputation: 336

Vagrant, launching command in ssh with environment variables

I have a vagrantbox with a LAMP stack and YiiFramework. I need to run some commands that rely on environment variables that I set provisioning the box in the .bashrc (.profile has the same behavior though). The problem is that I want to launch them using ssh, but everything I tried is not working. For example:

vagrant ssh -- -t '[path]/./yiic command action'

Tells me that I didn't set the env variables. Even:

vagrant ssh -- -t 'printenv MYSQL_HOST'

Has this output:

rmessineo:~ rmessineo$ switebox ssh -- -t 'printenv MYSQL_HOST'
Connection to 127.0.0.1 closed.
rmessineo:~ rmessineo$ 

But of course if I login in and to the same:

rmessineo:~ rmessineo$ vagrant ssh

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Feb 24 11:02:05 2016 from 10.0.2.2

vagrant@debian-jessie:~$ printenv MYSQL_HOST
127.0.0.1
vagrant@debian-jessie:~$ 

Any ideas?

Upvotes: 0

Views: 1141

Answers (2)

Frederic Henri
Frederic Henri

Reputation: 53703

I added the following in my .profile file in the VM

export MY_VAR="hello fred"

Then I am able to get the value of the new variable by running

fhenri@machine:~/project/examples/vagrant/ubuntu$ vagrant ssh -- -t 'source ~/.profile && printenv MY_VAR'
hello fred
Connection to 192.168.6.120 closed.

Upvotes: 1

BMW
BMW

Reputation: 45223

Suppose the vagrant instance starts with port 2222, then you can run the command:

ssh -p 2222 [email protected] 'printenv MYSQL_HOST'

password is vagrant, then you should get the result.

Update the port depend the real vagrant instance.

Upvotes: 0

Related Questions