Is there any way that I can set some ENV vars from host to guest Vagrant OS when booting it up? From my guest OS I need to connect to a database and I want to pass the database URL from host to guest as well as other parameters.

I tried with TEST="foo" vagrant up but TEST is nowhere in my env vars on guest os. Any ideas?

有帮助吗?

解决方案

Your way of altering `vagrant up´ won't work. The correct way of doing this is during provisioning. Depending on the provisioner you are using, there are different ways.

Here is an example how to echo your host PATH env on your guest during provisioning:

config.vm.provision "shell", inline: "echo " + ENV['PATH']

As you can see, inside your Vagrantfile you can use the ENV variable to access the host environment variables. You can use this to pass variables to the provisioner of your choice and processing it as needed.

As I'm most used to chef, here is how I would do it:

  1. Pass all ENV variables needed through Vagrantfile into some chef attributes
  2. Create a template which iterates over the values provided and writes export statements
  3. Use chef to put this template into /etc/profile.d/ under some name that fits the needs

其他提示

@Romeo: See my related answer here: https://stackoverflow.com/a/42250314/6864226

config.vm.provision :shell, path: "scripts/bootstrap.sh", env: {"MYVAR" => ENV['ABC']}

@Sgoettschkes: Thanks for the pointer in the right direction

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top