如何在 Vagrant 配置期间使环境变量可用?
How can I make environment variables available during Vagrant provisioning?
我想在配置期间让 Vagrant 可以使用环境变量,这样我就可以 运行 一些依赖它们作为凭据的命令。具体来说 aws cli
和 pg_restore
.
例如,pg_restore
需要访问我在 .bash_profile
中设置的 $PGPASS 变量。我试过 运行ning source /home/vagrant/.bash_profile
导出所需的 AWS 环境变量,但稍后在我的配置块中 aws
命令失败,因为未设置环境变量。
.bash_profile
export AWS_ACCESS_KEY_ID='keyid'
export AWS_SECRET_ACCESS_KEY='secret'
Vagrantfile 配置块
config.vm.provision :shell, run: "always", inline: <<-SH.gsub(/^\s*/,"")
source /home/vagrant/.bash_profile
aws s3 cp s3://bucket/sql/file /tmp/file # fail, missing credentials
echo $(printenv | grep AWS_) # outputs blank line
SH
您 运行 作为 root
用户进行配置,而您的 bash 是针对 vagrant
用户的,因此请确保 运行 通过添加privileged: false
config.vm.provision :shell, privileged: false, run: "always", inline: <<-SH.gsub(/^\s*/,"")
source /home/vagrant/.bash_profile
aws s3 cp s3://bucket/sql/file /tmp/file # fail, missing credentials
echo $(printenv | grep AWS_) # outputs blank line
SH
我想在配置期间让 Vagrant 可以使用环境变量,这样我就可以 运行 一些依赖它们作为凭据的命令。具体来说 aws cli
和 pg_restore
.
例如,pg_restore
需要访问我在 .bash_profile
中设置的 $PGPASS 变量。我试过 运行ning source /home/vagrant/.bash_profile
导出所需的 AWS 环境变量,但稍后在我的配置块中 aws
命令失败,因为未设置环境变量。
.bash_profile
export AWS_ACCESS_KEY_ID='keyid'
export AWS_SECRET_ACCESS_KEY='secret'
Vagrantfile 配置块
config.vm.provision :shell, run: "always", inline: <<-SH.gsub(/^\s*/,"")
source /home/vagrant/.bash_profile
aws s3 cp s3://bucket/sql/file /tmp/file # fail, missing credentials
echo $(printenv | grep AWS_) # outputs blank line
SH
您 运行 作为 root
用户进行配置,而您的 bash 是针对 vagrant
用户的,因此请确保 运行 通过添加privileged: false
config.vm.provision :shell, privileged: false, run: "always", inline: <<-SH.gsub(/^\s*/,"")
source /home/vagrant/.bash_profile
aws s3 cp s3://bucket/sql/file /tmp/file # fail, missing credentials
echo $(printenv | grep AWS_) # outputs blank line
SH