After a while my VagrantFile becomes fairly sophisticated (complicated). It will be very nice if Vagrant can provide a features that allows us to pass command line parameters into the Ruby script in VagrantFile.
For example, we can say:
vagrant up --Dmode=test
in VagrantFile, it then is able to reference the parameter by
case properties[:mode]
when "test"
# ...
when "release"
# ...
end
Use environmental variables.
Environment variables would work, but it would still be a nice enhancement to be able to pass arguments as described.
I tried:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "oracle"
# Boot with a GUI so you can see the screen. (Default is headless)
# config.vm.boot_mode = :gui
# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
# any other machines on the same network, but cannot be accessed (through this
# network interface) by any external networks.
# config.vm.network :hostonly, "192.168.33.10"
# Assign this VM to a bridged network, allowing you to connect directly to a
# network using the host's network device. This makes the VM appear as another
# physical device on your network.
# config.vm.network :bridged
# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.forward_port 1521, ENV['VAGRANT_ORACLE_PORT']
....
but it fails with:
export VAGRANT_ORACLE_PORT=1521
et2448@ubuntu:~/projects/tac/jfr/tac-jfr-server$ !va
vagrant up
/var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/vm.rb:44:in `to_s': wrong number of arguments (1 for 0) (ArgumentError)
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/vm.rb:44:in `forward_port'
from /home/et2448/projects/tac/jfr/tac-jfr-server/Vagrantfile:32
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:83:in `call'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:83:in `load'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:79:in `each'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:79:in `load'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:76:in `each'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/config/loader.rb:76:in `load'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/environment.rb:387:in `load_config!'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/environment.rb:392:in `call'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/environment.rb:392:in `load_config!'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/lib/vagrant/environment.rb:327:in `load!'
from /var/lib/gems/1.8/gems/vagrant-1.0.4/bin/vagrant:40
from /usr/local/bin/vagrant:19:in `load'
from /usr/local/bin/vagrant:19
On Tue, Jan 21, 2014 at 2:15 AM, David J. M. Karlsen <
[email protected]> wrote:
config.vm.forward_port 1521, ENV['VAGRANT_ORACLE_PORT']
I think the correct format is:
config.vm.network :forwarded_port, guest: 1521, host: 1521
Are you on the latest vagrant? 1.4.3 ?
Alvaro.
Ah, figured it out - needed a to_i on the variable to convert it over to an integer so I got the correct method signature.
I use:
config.vm.provision "shell", path: "provision.sh", args: ENV['SHELL_ARGS']
then call :
SHELL_ARGS='-h' vagrant provision
Then anything that you would want to pass as the whole argument string will be passed in to the shell args.
I saw a solution on Stack Overflow that suggests using Ruby's GetoptLong
class to get options from the commandline. It would allow commands like:
vagrant --custom-option=option up
vagrant --custom-option=option provision
Special care may need to be taken to avoid interfering with vagrant's preexisting options. Some users have reported problems with this solution, which may be due to the versions of vagrant or ruby being used.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Environment variables would work, but it would still be a nice enhancement to be able to pass arguments as described.