0

I am trying to pass in arguments (via known ruby methods) to my vagrant up command line, but am getting machine not found errors. What is the correct way to do this in Vagrant?

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Parse options
options = {}
options[:keyfile] = ARGV[1] || false         # Your Github authentication keyfile
options[:boxtype] = ARGV[2] || 'virtualbox' # Type of virtual appliance to load
options[:version] = ARGV[3] || 'latest'     # Box version to load (not used currently)

ARGV.delete_at(1)
ARGV.delete_at(1)
ARGV.delete_at(1)


Vagrant.configure("2") do | config |

  puts ("==> [info] Looking for #{options[:keyfile]}")
  if File.file?(options[:keyfile])
    config.vm.provision :shell, :inline => "echo -e '#{File.read(options[:keyfile])}' > '/home/vagrant/.ssh/GitKey'"
  else
    puts ("==> [error] The require RSA key: #{options[:keyfile]} does not exist, exiting.")
    abort
  end
end

Error

$ vagrant up ~/.ssh/github_rsa
The machine with the name '/Users/ehime/.ssh/github_rsa' was not found configured for
this Vagrant environment.

EDIT

Trying this a different way gives me some slightly more promising results

require 'optparse'
require 'ostruct'

....

options = OpenStruct.new
OptionParser.new do | opt |
  opt.on('-v', '--version VERSION', 'Box version to load (not used currently)')  { | o | options.version = o }
  opt.on('-k', '--keyfile KEYFILE', 'Your Github authentication keyfile')        { | o | options.keyfile = o }
  opt.on('-b', '--boxfile BOXTYPE', 'Type of virtual appliance to load')         { | o | options.boxtype = o }
end.parse!

Vagrant.configure("2") do | config |

  puts ("==> [info] Looking for #{options.keyfile}")
  if File.file?(options.keyfile)
    config.vm.provision :shell, :inline => "echo -e '#{File.read(options.keyfile)}' > '/home/vagrant/.ssh/GitKey'"
  else
    puts ("==> [error] The require RSA key: #{options.keyfile} does not exist, exiting.")
    abort
  end

....

Gets me pretty close as well, but it needs the flags unset somehow so they don't conflict with vagrant. At least the help flag works

 $ vagrant up -k /Users/ehime/.ssh/github_rsa
 ==> [info] Looking for /Users/ehime/.ssh/github_rsa
 An invalid option was specified. The help for this command
 is available below.

 Usage: vagrant up [options] [name]

 Options:

         --[no-]provision             Enable or disable provisioning
         --provision-with x,y,z       Enable only certain provisioners, by type.
         --[no-]destroy-on-error      Destroy machine if any fatal error happens (default to true)
         --[no-]parallel              Enable or disable parallelism if provider supports it
         --provider PROVIDER          Back the machine with a specific provider
     -h, --help                       Print this help 

Help

 $ vagrant up -h
 Usage: vagrant up [options] [name]

 Options:

         --[no-]provision             Enable or disable provisioning
         --provision-with x,y,z       Enable only certain provisioners, by type.
         --[no-]destroy-on-error      Destroy machine if any fatal error happens (default to true)
         --[no-]parallel              Enable or disable parallelism if provider supports it
         --provider PROVIDER          Back the machine with a specific provider
     -h, --help                       Print this help
 Usage: vagrant [options]
     -v, --version VERSION            Box version to load (not used currently)
     -k, --keyfile KEYFILE            Your Github authentication keyfile
     -b, --boxfile BOXTYPE            Type of virtual appliance to load 
ehime
  • 8,025
  • 14
  • 51
  • 110
  • I use environment variables for this, see: http://stackoverflow.com/a/14124316/627806. Perhaps it's not as nice from the user point of view, but it's simple and it works. – Jon Burgess Jul 17 '15 at 00:47

1 Answers1

0

The Vagrantfile is not executed directly so you cannot just pass in the arguments as you would with a normal script. vagrant looks for the file inside cwd() and bring it in.
Would go the route of the env vars or a template file which you generate before running vagrant.

Mircea
  • 10,216
  • 2
  • 30
  • 46