将环境变量传递给vagrant shell provisioner

时间:2013-10-29 01:26:35

标签: vagrant environment-variables

如果你使用的是Ruby配置器,调用vagrant up时传递环境变量看起来很简单:

VAR=123 vagrant up

在Vagrantfile中:

ENV['VAR']

如何使用:shell配置程序执行此操作?简单地这样做似乎不起作用:

$VAR

12 个答案:

答案 0 :(得分:62)

Since Vagrant 1.8.0你可以忘记其他答案的丑陋黑客。只需对shell配置程序使用env选项(docs)。

在Vagrantfile中使用它:

config.vm.provision "shell", path: "provisionscript.sh", env: {"MYVAR" => "value"}

这将仅为配置脚本设置环境。如果您需要为VM中的所有进程设置持久性环境变量,则这超出了Vagrant配置的范围,请在此处查看:Shell environment variables in vagrant files are only passed on first up

答案 1 :(得分:41)

这不太理想,但我现在才开始工作:

config.vm.provision "shell" do |s|
    s.inline = "VAR1 is $1 and VAR2 is $2"
    s.args   = "#{ENV['VAR1']} #{ENV['VAR2']}"
end

答案 2 :(得分:16)

我为基于CentOS的配置提供了这个解决方案:将所有必需的环境变量放在/etc/profile.d/vagrant.sh文件中,然后在任何配置脚本中访问它。

简而言之:

  $before_script = <<SCRIPT
  echo # vagrant profile script > /etc/profile.d/vagrant.sh
  echo export ENV_VAR1=foo.com/bar >> /etc/profile.d/vagrant.sh
  echo export ENV_VAR2=bar.com/foo >> /etc/profile.d/vagrant.sh
  chmod +x /etc/profile.d/vagrant.sh
SCRIPT

  $after_script = <<SCRIPT
    rm -rf /etc/profile.d/vagrant.sh
SCRIPT

  config.vm.provision "shell", inline: $before_script
  config.vm.provision "shell", path: "build.sh"
  config.vm.provision "shell", inline: $after_script

可在此处找到完整的Vagrantfile https://gist.github.com/bivas/6192d6e422f8ff87c29d

答案 3 :(得分:14)

对于后代(又如我再次谷歌)...可以通过env传递键值对:

box.vm.provision :shell do |s|
  s.env = {AWS_ACCESS_KEY:ENV['AWS_ACCESS_KEY'], AWS_SECRET_KEY:ENV['AWS_SECRET_KEY']}
  s.path = 'scripts/bootstrap.sh'
end

然后在你的脚本中引用它们:

export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY}
export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_KEY}

奖金功能:

  

Vagrant将处理环境变量值的引用,但是   钥匙保持不变

答案 4 :(得分:7)

有人最终会在配置脚本的环境中寻找如何设置变量,这对我有用。

config.vm.provision :shell, :inline => <<-SH
  export GRAPHITE_HOST=192.168.33.10
  /vagrant/install_app_with_monitoring.sh
SH

请注意,这假设您在VM上共享工作目录/vagrant,但这应该是默认值。

答案 5 :(得分:5)

您可以在内联脚本中使用#{ENV['VAR']},例如:

config.vm.provision "shell", inline: <<-END
  ...
  # Install my dotfiles are there.  If you're in a hurry you can do
  # SKIP_DOTFILES=1 vagrant up
  if ! [ -d /home/vagrant/dotfiles ] && [ -z '#{ENV['SKIP_DOTFILES']}']; then
    if ! [ -x /usr/bin/git ]; then
      DEBIAN_FRONTEND=noninteractive apt-get install -y git
    fi
    su - vagrant -c 'git clone https://github.com/mgedmin/dotfiles'
    su - vagrant -c 'dotfiles/install.sh'
  fi
  ...
  END

从工作的Vagrant文​​件中获取的示例。

这有一些缺点:如果$ VAR包含单引号,事情就会中断。

答案 6 :(得分:3)

这些答案大部分似乎已过时。在Vagrant 2.1.1中,这对我有用:

  VAGRANTFILE_API_VERSION = "2" //...

  machine.vm.provision "shell", 
    env: {
      "ELASTIC_XMS" => servers["elastic"]["memory_xms"], 
      "ELASTIC_XMX" => servers["elastic"]["memory_xmx"]
    }, 
    inline: "sed -i -e \"s/-Xms.*/$ELASTIC_XMS/g\" /etc/elasticsearch/jvm.options"

答案 7 :(得分:2)

vagrant-env插件就是这样做的。有了它,您可以将环境变量添加到本地目录中的.env文件中,该文件将加载到Vagrant文件中。我建议您.env保留.gitignore,这样就可以保证您的隐私。

答案 8 :(得分:1)

以下是我的工作方式。

我从使用vagrant puppet provisioner方式转到使用shell配置器。我这样做主要是因为我想木偶不以root身份运行,shell提供商给你:privileged =&gt;假的。

我的老路:

config.vm.provision :puppet do |puppet|
  puppet.module_path = ENV.fetch('MODULES_PATH', 'modules')
  puppet.manifests_path = ENV.fetch('MANIFESTS_PATH', 'manifests')
  puppet.manifest_file  = ENV.fetch('MANIFEST_FILE', 'site.pp')
  puppet.options = "--debug"
end

我的新方式:

config.vm.provision :shell, :privileged => false do |shell|
  shell.inline = "puppet apply --debug --modulepath '/vagrant/#{ENV.fetch('MODULES_PATH', 'modules')}' --detailed-exitcodes '/vagrant/#{ENV.fetch('MANIFESTS_PATH', 'manifests')}/#{ENV.fetch('MANIFEST_FILE', 'site.pp')}'"
end

答案 9 :(得分:1)

您只需在shell文件中使用inline指定Vagrantfile

config.vm.provision "shell", inline: %Q(/usr/bin/env FOO=1 BAR=1 bash /path/to/script.sh)

或者从YAML文件中加载一些额外的变量:

require 'yaml'
dir = File.dirname(File.expand_path(__FILE__))
vconfig = YAML::load_file("#{dir}/config.yml")
config.vm.provision "shell", inline: %Q(/usr/bin/env FOO=#{vconfig['foo']} bash /path/to/script.sh)

或者,您可以从命令行实现一些可选参数,例如:

# Parse optional arguments.
opts = GetoptLong.new(
  [ '--foo',  GetoptLong::OPTIONAL_ARGUMENT ], # With optional parameter.
  [ '--bar',  GetoptLong::OPTIONAL_ARGUMENT ], # With optional parameter.files.
)
opts.each do |opt, arg|
  case opt
    when '--foo'
      foo==arg
    when '--bar'
      bar=arg
  end
end

然后使用:opt['--foo'].to_s

另请参阅:How to pass parameter on Vagrant up and have it in the scope of Chef cookbook?

答案 10 :(得分:-2)

这对我有用

VAGRANTFILE_API_VERSION = "2"

kettle_dir = ENV['KETTLE_DIR']
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   config.vm.synced_folder kettle_dir, "/pentaho"
   config.vm.box = "ubuntu/trusty64"
end

答案 11 :(得分:-3)

在ubutnu框中,我只是在bootstrap.sh中执行了以下操作:

echo "DBHOST=localhost" >> /etc/environment
echo "DBNAME=foo" >> /etc/environment
echo "DBUSER=root" >> /etc/environment
echo "DBPASSWD=root" >> /etc/environment