终端上的 Vagrant argv 输入抱怨机器名称

Vagrant argv input on terminal complains about machine name

我试图将参数(通过已知的 ruby 方法)传递到我的 vagrant up 命令行,但我收到机器未找到错误。在 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

错误

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

编辑

以不同的方式尝试这个给了我一些更有希望的结果

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

....

也让我非常接近,但它需要以某种方式取消设置标志,这样它们就不会与流浪者冲突。至少帮助标志有效

 $ 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 

帮助

 $ 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 

Vagrantfile 不会直接执行,因此您不能像使用普通脚本那样只传入参数。 vagrant 在 cwd() 里面寻找文件,并把它带进来。
会走你在 运行 vagrant 之前生成的环境变量或模板文件的路线。