Shell environment variables in vagrant files are o

2019-07-11 18:19发布

I'm running Vagrant 1.8.5, which supports environment variables in the Vagrantfile at provisioning time using a format like this

config.vm.provision :shell, path: "bootstrap.sh", env: {"MYSQL_DB_USERNAME"=>"django", "MYSQL_DB_PASSWORD"=>"supersecretpasswordwasreplaced"}

However, I would like these environment variables to also be available when I do up without provisioning, or when I SSH into the server to do jobs. The obvious ways I can find are

  • to have them built into the box (seems like overkill) or
  • repeat them in my config.ssh.forward_env so that they are available to both the provisioner and SSH.

Is there a better way?

2条回答
Explosion°爆炸
2楼-- · 2019-07-11 18:33

I am not sure about the best ultimate way - I've seen this question popping from time to time and discussion.

repeat them in my config.ssh.forward_env so that they are available to both the provisioner and SSH.

well, it could sound like a good solution but there's some drawback, I've discussed in this question already

Personally when I need to set env variable, I would add them in the .profile file:

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    echo "export MYSQL_DB_USERNAME = django" > /home/vagrant/.profile
    echo "export MYSQL_DB_PASSWORD = supersecretpasswordwasreplaced" > /home/vagrant/.profile
  SHELL

They will be available when you login.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-11 18:44

At first glance, this answer seems similar to the top voted answer. However, please look carefully at the redirection operator on the second echo

config.vm.provision "shell", env: {"DOCKERHUBID"=>ENV['DOCKERHUBID'], "DOCKERHUBPASS"=>ENV['DOCKERHUBPASS']}, inline: <<-SHELL
echo "export DOCKERHUBID=$DOCKERHUBID" > /home/vagrant/.profile
echo "export DOCKERHUBPASS=$DOCKERHUBPASS" >> /home/vagrant/.profile
SHELL
查看更多
登录 后发表回答