getting the vagrant command line arguments inside the Vagrantfile

前端 未结 1 1672
说谎
说谎 2020-12-16 14:33

I have the following configuration that only makes sense for vagrant up command:

config.vm.provider :virtualbox do |vb|
  vb.gui = false
  if ENV[\"VB_GUI\"]         


        
相关标签:
1条回答
  • 2020-12-16 14:58

    A Vagrantfile is just ruby code so you can easily get the command line arguments using the ARGV array.

    Take the following vagrant command for example:

    vagrant up webserver

    That would start the Vagrant box defined as webserver in your Vagrantfile. You can then access the arguments like so:

    ARGV[0] = up
    ARGV[1] = webserver
    

    So using your example you need to do the following:

    config.vm.provider :virtualbox do |vb|
      vb.gui = false
      if ARGV[0] == "up"
        if ENV["VB_GUI"] == "true" then vb.gui = true
        else
           puts("[info] VB_GUI environment variable not set so running headless")
        end
      end
    end
    
    0 讨论(0)
提交回复
热议问题