使用vagrant provisioner创建linux环境变量

使用vagrant provisioner创建linux环境变量,vagrant,Vagrant,我让vagrant运行Ubuntu进行开发。我使用shell脚本provisioner下载/安装依赖项并创建一些别名,但在使用provisioner创建环境变量(用于项目中的几个标志)方面遇到了困难。最初我有这样的想法: export MY_VAR='value' 进入我的provisioner脚本中,但随后发现无法通过正常运行shell脚本从内部执行。很公平,所以我试着将我的流浪者档案行改为: config.vm.provision "shell", inline: “source setu

我让vagrant运行Ubuntu进行开发。我使用shell脚本provisioner下载/安装依赖项并创建一些别名,但在使用provisioner创建环境变量(用于项目中的几个标志)方面遇到了困难。最初我有这样的想法:

export MY_VAR='value'
进入我的provisioner脚本中,但随后发现无法通过正常运行shell脚本从内部执行。很公平,所以我试着将我的流浪者档案行改为:

config.vm.provision "shell", inline: “source setup.sh"
这并没有解决问题。环境变量仍然不存在。我尝试将导出直接添加为内联:

config.vm.provision "shell", inline: “export MY_VAR='value'"

不走运。我进去的时候还没有全球环境。有没有办法使用shell脚本设置bash环境变量,或者是时候放弃shell Provisioniers并学习chef了?

您应该让provisioning脚本在您的配置文件中添加一行内容。配置文件:

echo "export VAR=value" >> ~/.profile

登录时,bash将读取.profile脚本并设置变量。

鉴于接受的答案确实会在每次运行
vagrant provision
时向.profile(或.bashrc)文件中添加
export VAR=value
,下面是我如何快速添加环境变量的

source ~/.profile && [ -z "$VAR" ] && echo "export VAR=value" >> ~/.profile
细分:

  • source~/.profile
    :加载当前的
    .profile
    文件
  • [-z“$VAR”]
    :检查是否设置了
    VAR
    ,如果没有:
  • echo“export VAR=value”>~/.profile
    :将导出行添加到
    .profile
总而言之:

我通常将puphpet用于我的vagrant框,因此我倾向于使用它使用的目录结构,这意味着将我的配置脚本放入
puphpet/shell/*
(相对于
vagrant文件
文件)。在该文件中,可以添加任意数量的环境变量:

#!/usr/bin/env bash
#Replace .profile with .bashrc if required
source ~/.profile
if [ -z "$VAR" ]; then # only checks if VAR is set, regardless of its value
    echo "export VAR=value" >> ~/.profile
fi
#other env variables and profile stuff here
如果要将环境变量设置为特定值,即使已设置,也可以使用以下内容替换
[-z“$VAR”]
(如Maks3w所建议):

然后只需将其添加到您的文件:

config.vm.provision "shell", path: "puphpet/shell/your-script.sh"

这就应该做到了……

这个答案展示了如何添加环境变量,只需使用Ruby-herdoc(Here-doc)语法修改
VagrantFile

$install\u user\u vars=~/.profile
fi
如果[$(pwd)!=“/流浪者”];然后
echo“cd/vagrant”>>~/.配置文件
fi
剧本
config.vm.provision“shell”,内联:$install\u user\u vars

注意:close
脚本必须是他自己行中的第一个字符

以下是我如何使
$GOPATH
工作的:

config.vm.provision "shell", inline: <<-SHELL
  echo -n                              >  /etc/profile.d/gopath.sh
  echo 'export GOPATH=$HOME/go'        >> /etc/profile.d/gopath.sh
  echo 'export PATH=$PATH:$GOPATH/bin' >> /etc/profile.d/gopath.sh
SHELL
config.vm.provision“shell”,内联:>/etc/profile.d/gopath.sh
echo'export PATH=$PATH:$GOPATH/bin'>>/etc/profile.d/GOPATH.sh
壳

使用单引号和
$HOME
(而不是
~
)是必要的——否则我无法让它工作。

我也在寻找同样的东西。我想在文件中设置
http(s)\u proxy
环境变量。要求是我可以随时更改这些变量,并
vagrant reload
应用它们

最后,我提出了解决方案:

config.vm.provision "shell", inline: "> /etc/profile.d/myvars.sh", run: "always"
config.vm.provision "shell", inline: "echo \"export http_proxy=http://proxy.somedomain.com:3128\" >> /etc/profile.d/myvars.sh", run: "always"
config.vm.provision "shell", inline: "echo \"export https_proxy=https://proxy.somedomain.com:3128\" >> /etc/profile.d/myvars.sh", run: "always"
0)/etc/profile.d/*.sh脚本在bash shell启动时运行
1) 从myvars中删除所有内容。sh
2) 设置第一个变量
3) 设置第二个变量


因为我可以添加/删除变量,并在
vagrant reload
之后应用它们。

在CentOs7中,您需要将环境变量放置在
/etc/profile.d/
这在我的情况下起了作用:

machine.vm.provision 'shell',
  inline: 'sudo su - && echo "export VAR1=test export VAR2=test" > /etc/profile.d/vars.sh'

是的,成功了!谢谢。请注意,我正在使用CentOS 7,我必须执行
echo“export VAR=value”>~/.bashrc
此修复程序不遵循“流浪方式”。按原样在vagrant中添加此项将在每个条款上追加一行cicle@Maks3w:公正的评论,虽然很容易修复(添加了一个显示如何修复的答案),但也许这个答案可以修改为完整的,例如,显示整个config.vm。。。。行?我喜欢使用-z开关来代替grepI have answer和100%完整的Vagrant wat(没有外部脚本文件)@Maks3w:这也是一个有效的选项。不确定
[$VAR!=“value”]
位,因为出于开发原因,vbox的用户可能已经更改了env变量,并且他可能不希望通过设置Vagrant的设置阶段来更改值,因为1.4(IIRC)在用户需求下。因此,如果他/她启动了供应阶段,那么他/她真的希望重置配置。@Maks3w:不一定。我在工作中使用vagrant,我们在团队中工作,经常有人更新vagrant回购(添加vhost,或设置新工具),这要求我们提供箱子。我希望资源调配能够添加新内容,而不会中断我自己的更改。这取决于OP IMHO的具体需求/用例
config.vm.provision "shell", inline: "> /etc/profile.d/myvars.sh", run: "always"
config.vm.provision "shell", inline: "echo \"export http_proxy=http://proxy.somedomain.com:3128\" >> /etc/profile.d/myvars.sh", run: "always"
config.vm.provision "shell", inline: "echo \"export https_proxy=https://proxy.somedomain.com:3128\" >> /etc/profile.d/myvars.sh", run: "always"
machine.vm.provision 'shell',
  inline: 'sudo su - && echo "export VAR1=test export VAR2=test" > /etc/profile.d/vars.sh'