如果env,在Vagrantfile中设置默认值。变量未设定

时间:2018-05-30 10:41:16

标签: ruby vagrant

如果未设置环境变量,我正尝试执行以下操作来设置默认值:

config.vm.box = ENV['VAGRANT_DEV_BOX'] || "ubuntu/xenial64"

导致以下错误:

/opt/vagrant/embedded/lib/ruby/2.4.0/rubygems/version.rb:208:in `initialize': Malformed version number string debian-VAGRANTSLASH-jessie64 (ArgumentError)

此时尚未设置VAGRANT_DEV_BOX变量。确认如下:

server   echo $VAGRANT_DEV_BOX

server 

是否可以在Ruby和/或Vagrantfile中执行此操作?

1 个答案:

答案 0 :(得分:1)

这要感谢#vagrant上的double-p,freenode:

  

<double-p>你不能只是内联ruby ..把它放在vagrant-configure之上,如:

port = ENV["HOST_PORT"] || 8080

Vagrant.configure("2") do |config|
  # Ubuntu 14.04 LTS
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 80, host: port
  config.vm.provision "shell", path: "vagrant/provision.sh"
end