Setting ENV vars from host to guest in Vagrant

173 views
Skip to first unread message

Kunal Pariani

unread,
Apr 6, 2016, 7:53:06 PM4/6/16
to Vagrant
I need to do copy over an env var from the host (my Mac) to the vagrant VM. Any ideas on how to do this ? I tried couple of options after googling but none seem to work.

Alvaro Miranda Aguilera

unread,
Apr 7, 2016, 12:29:23 AM4/7/16
to vagra...@googlegroups.com
hello,

have a look at this.

Please let me know if helps, or its something missed.

Thanks
Alvaro.

On Thu, Apr 7, 2016 at 11:53 AM, Kunal Pariani <kuna...@gmail.com> wrote:
I need to do copy over an env var from the host (my Mac) to the vagrant VM. Any ideas on how to do this ? I tried couple of options after googling but none seem to work.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/4192f836-9d55-49d3-84eb-96fe629cddaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alvaro Miranda Aguilera

unread,
Apr 7, 2016, 12:35:10 AM4/7/16
to vagra...@googlegroups.com

Kunal Pariani

unread,
Apr 7, 2016, 4:06:03 PM4/7/16
to vagra...@googlegroups.com
I see the following error while trying this
There are errors in the configuration of this machine. Please fix
the following errors and try again:

shell provisioner:
* The following settings shouldn't exist: env

Do i need something additional here ? I installed the vagrant-env plugin too but that didn't work either

Thanks

You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/4PqYtQvTHqU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/C4CDA613-D320-4538-B42B-80DED91EE558%40gmail.com.

Alvaro Miranda Aguilera

unread,
Apr 7, 2016, 4:10:23 PM4/7/16
to vagra...@googlegroups.com
it should work without any plugin.

Whats the output of

vagrant version
vagrant plugin list



Kunal Pariani

unread,
Apr 7, 2016, 4:52:32 PM4/7/16
to Vagrant
Ok that error went away after i upgraded Vagrant to latest version 1.8.1
Although i still don't see the env var getting set inside the vagrant VM

$ vagrant provision==> default: Running provisioner: shell...
    default: Running: inline script
==> default: stdin: is not a tty
==> default: var HOST_IP=10.2.2.70
==> default: Running provisioner: shell...
    default: Running: /var/folders/5m/48h7dsrx39x2vl_xqmlddtch0000gn/T/vagrant-shell20160407-5063-rurl2r.sh
==> default: stdin: is not a tty
==> default: HOST_IP=10.2.2.70
==> default: $1=arg1
-----------------------------------------
$ vagrant ssh
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-85-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Thu Apr  7 20:45:34 UTC 2016

  System load:  0.0               Users logged in:        0
  Usage of /:   5.8% of 39.34GB   IP address for eth0:    10.0.2.15
  Memory usage: 33%               IP address for eth1:    10.2.2.70
  Swap usage:   0%                IP address for docker0: 172.17.0.1
  Processes:    84

  Graph this data and manage this system at:

  Get cloud support with Ubuntu Advantage Cloud Guest:

120 packages can be updated.
57 updates are security updates.


Last login: Thu Apr  7 20:47:07 2016 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$ echo $HOST_IP

vagrant@vagrant-ubuntu-trusty-64:~$ sudo su
root@vagrant-ubuntu-trusty-64:/home/vagrant#
--------------------------------------
$ cat provision.sh
#!/bin/bash

if [ ${HOST_IP} ]; then
echo "HOST_IP=${HOST_IP}"
        export HOST_IP=${HOST_IP}
else
echo "var HOST_IP not set"
fi

if [ ${1} ]; then
echo "\$1=${1}"
fi

Alvaro Miranda Aguilera

unread,
Apr 7, 2016, 7:04:42 PM4/7/16
to vagra...@googlegroups.com

On Fri, Apr 8, 2016 at 8:52 AM, Kunal Pariani <kuna...@gmail.com> wrote:
==> default: HOST_IP=10.2.2.70

Hello.


this line:
==> default: HOST_IP=10.2.2.70

Means the variable you are passing if being handled by the script.

From there, you need to update the provision script to configure it in the way you want.

Vagrant is doing his share, is upto configure the guest to consume that var.

You could replace this:
export HOST_IP=${HOST_IP}

with

echo "export HOST_IP=${HOST_IP}" | tee -a ~/.bash_profile

Now,

a piece of information, if you are using virtualbox, the host always will be available as 10.0.2.2




Alvaro.

Kunal Pariani

unread,
Apr 7, 2016, 9:01:34 PM4/7/16
to Vagrant
Thanks but i did update the script & still don't see the env var HOST_IP being set

Kunals-MacBook-Pro-2:consul-demo kunalpariani$ vagrant provision
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: stdin: is not a tty
==> default: var HOST_IP=10.2.2.70
==> default: Running provisioner: shell...
    default: Running: /var/folders/5m/48h7dsrx39x2vl_xqmlddtch0000gn/T/vagrant-shell20160407-77458-1i6snef.sh
==> default: stdin: is not a tty
==> default: HOST_IP=10.2.2.70
==> default: export HOST_IP=10.2.2.70
==> default: $1=arg1
==> default: Running provisioner: ansible...
-----------------------------
$ vagrant ssh
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-85-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Apr  8 00:52:27 UTC 2016

  System load:  0.48              Users logged in:        0
  Usage of /:   5.8% of 39.34GB   IP address for eth0:    10.0.2.15
  Memory usage: 28%               IP address for eth1:    10.2.2.70
  Swap usage:   0%                IP address for docker0: 172.17.0.1
  Processes:    85

  Graph this data and manage this system at:

  Get cloud support with Ubuntu Advantage Cloud Guest:

120 packages can be updated.
57 updates are security updates.


Last login: Fri Apr  8 00:53:38 2016 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$
vagrant@vagrant-ubuntu-trusty-64:~$ echo $HOST_IP

vagrant@vagrant-ubuntu-trusty-64:~$ vi ~/.bash
.bash_history  .bash_logout   .bashrc

No .bash_profile here
----------------
$ cat provision.sh
#!/bin/bash

if [ ${HOST_IP} ]; then
echo "HOST_IP=${HOST_IP}"
echo "export HOST_IP=${HOST_IP}" | tee -a ~/.bash_profile
else
echo "var HOST_IP not set"
fi

if [ ${1} ]; then
echo "\$1=${1}"
fi

Alvaro Miranda Aguilera

unread,
Apr 10, 2016, 6:02:45 PM4/10/16
to Vagrant
Hello,

Thats because by default the scripts run with privileged mode, so run as root.

if you want to setup variables as vagrant, then you need to the script with 

, privileged: "false"

Reply all
Reply to author
Forward
0 new messages