Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a default value in Vagrantfile if env. variable not set

Tags:

ruby

vagrant

I'm trying to do something like the following to set a default value if an environment variable is not set:

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

Which causes the following error:

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

The VAGRANT_DEV_BOX variable hasn't been set at this point. Confirmed like so:

server 🦄  echo $VAGRANT_DEV_BOX

server 🦄

Is it possible to do this in Ruby and/or Vagrantfile?

like image 218
BugHunterUK Avatar asked Mar 06 '23 02:03

BugHunterUK


1 Answers

This is thanks to double-p on #vagrant, freenode:

<double-p> you cannot just inline ruby.. put this above vagrant-configure, like:

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
like image 75
BugHunterUK Avatar answered Apr 07 '23 01:04

BugHunterUK