Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the vagrant command line arguments inside the Vagrantfile

Tags:

vagrant

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"] == "true" then vb.gui = true
  else
     puts("[info] VB_GUI environment variable not set so running headless")
  end
end

Is it possible to hook into the vagrant API to retrieve the command currently being executed? E.g.

config.vm.provider :virtualbox do |vb|
  vb.gui = false
  if VAGRANT_API.command == "up"  # how can I do this?
    if ENV["VB_GUI"] == "true" then vb.gui = true
    else
       puts("[info] VB_GUI environment variable not set so running headless")
    end
  end
end
like image 336
Chris Snow Avatar asked Jan 01 '14 09:01

Chris Snow


People also ask

What is the command you uses to initializes the current directory to be a vagrant environment by creating an initial Vagrantfile if one does not already exist?

Command: vagrant init [name [url]] This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one does not already exist.

What is vagrant Vagrantfile?

A Basic Vagrantfile A Vagrantfile is a Ruby file that instructs Vagrant to create, depending on how it is executed, new Vagrant machines or boxes. You can see a box as a compiled Vagrantfile. It describes a type of Vagrant machines. From a box, we can create new Vagrant machines.


1 Answers

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
like image 169
Matt Cooper Avatar answered Oct 10 '22 03:10

Matt Cooper