Woj*_*ski 80 ruby command-line parameter-passing vagrant
我正在寻找一种方法将参数传递给Chef cookbook,如:
$ vagrant up some_parameter
然后some_parameter在其中一个Chef cookbook中使用.
Dra*_*ter 106
你不能将任何参数传递给vagrant.唯一的方法是使用环境变量
MY_VAR='my value' vagrant up
并ENV['MY_VAR']在食谱中使用.
Ben*_*ier 65
您还可以包含GetoptLong Ruby库,它允许您解析命令行选项.
Vagrantfile
require 'getoptlong'
opts = GetoptLong.new(
  [ '--custom-option', GetoptLong::OPTIONAL_ARGUMENT ]
)
customParameter=''
opts.each do |opt, arg|
  case opt
    when '--custom-option'
      customParameter=arg
  end
end
Vagrant.configure("2") do |config|
             ...
    config.vm.provision :shell do |s|
        s.args = "#{customParameter}"
    end
end
然后,您可以运行:
$ vagrant --custom-option=option up
$ vagrant --custom-option=option provision
注意:确保在vagrant命令之前指定了custom选项,以避免无效的选项验证错误.
有关库的详细信息在这里.
tsu*_*iga 23
可以从ARGV中读取变量,然后在继续进入配置阶段之前将其从中删除.修改ARGV感觉很糟糕,但我找不到任何其他命令行选项.
# Parse options
options = {}
options[:port_guest] = ARGV[1] || 8080
options[:port_host] = ARGV[2] || 8080
options[:port_guest] = Integer(options[:port_guest])
options[:port_host] = Integer(options[:port_host])
ARGV.delete_at(1)
ARGV.delete_at(1)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Create a forwarded port mapping for web server
  config.vm.network :forwarded_port, guest: options[:port_guest], host: options[:port_host]
  # Run shell provisioner
  config.vm.provision :shell, :path => "provision.sh", :args => "-g" + options[:port_guest].to_s + " -h" + options[:port_host].to_s
port_guest=8080
port_host=8080
while getopts ":g:h:" opt; do
    case "$opt" in
        g)
            port_guest="$OPTARG" ;;
        h)
            port_host="$OPTARG" ;;
    esac
done
小智 5
@ benjamin-gauthier的GetoptLong解决方案确实很整洁,非常适合红宝石和无业游民的范例。
但是,它需要多一行来修正对无用的参数的干净处理,例如vagrant destroy -f。
require 'getoptlong'
opts = GetoptLong.new(
  [ '--custom-option', GetoptLong::OPTIONAL_ARGUMENT ]
)
customParameter=''
opts.ordering=(GetoptLong::REQUIRE_ORDER)   ### this line.
opts.each do |opt, arg|
  case opt
    when '--custom-option'
      customParameter=arg
  end
end
这样,当处理自定义选项时,此代码块即可暂停。所以现在
 vagrant --custom-option up --provision
还是
 vagrant destroy -f
干净利落。
希望这可以帮助,