ruby 如何在“vagrant up”上传递参数并将其置于 Vagrantfile 的范围内?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14124234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:38:26  来源:igfitidea点击:

How to pass parameter on 'vagrant up' and have it in the scope of Vagrantfile?

rubycommand-linevagrantparameter-passing

提问by Wojciech Bednarski

I'm looking for a way to pass parameter to Chef cookbook like:

我正在寻找一种将参数传递给 Chef 食谱的方法,例如:

$ vagrant up some_parameter

And then use some_parameterinside one of the Chef cookbooks.

然后some_parameter在其中一本厨师食谱中使用。

回答by Draco Ater

You cannot pass any parameter to vagrant. The only way is to use environment variables

您不能将任何参数传递给 vagrant。唯一的方法是使用环境变量

MY_VAR='my value' vagrant up

And use ENV['MY_VAR']in recipe.

ENV['MY_VAR']在配方中使用。

回答by Benjamin Gauthier

You also can include the GetoptLongRuby library that allows you to parse command line options.

您还可以包含允许您解析命令行选项的GetoptLongRuby 库。

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

Then, you can run :

然后,您可以运行:

$ vagrant --custom-option=option up
$ vagrant --custom-option=option provision

Note: Make sure that the custom option is specified beforethe vagrant command to avoid an invalid option validation error.

注意:确保在 vagrant 命令之前指定自定义选项以避免无效选项验证错误。

More information about the library here.

关于图书馆的更多信息在这里

回答by tsuriga

It is possible to read variables from ARGV and then remove them from it before proceeding to configuration phase. It feels icky to modify ARGV but I couldn't find any other way for command-line options.

可以从 ARGV 读取变量,然后在进行配置阶段之前从中删除它们。修改 ARGV 感觉很糟糕,但我找不到任何其他命令行选项的方法。

Vagrantfile

流浪档案

# 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

 

 

provision.sh

配置文件

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

回答by Kannan Varadhan

@benjamin-gauthier 's GetoptLong solution is really neat, fits in with the ruby and vagrant paradigm well.

@benjamin-gauthier 的 GetoptLong 解决方案非常简洁,非常适合 ruby​​ 和 vagrant 范式。

It however, needs one extra line to fix clean handling of the vagrant arguments, such as vagrant destroy -f.

然而,它需要一个额外的行来修复流浪参数的干净处理,例如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

which allows this block of code to pause when the custom options are processed. so now, vagrant --custom-option up --provisionor vagrant destroy -fare cleanly handled.

这允许此代码块在处理自定义选项时暂停。所以现在, vagrant --custom-option up --provision还是 vagrant destroy -f处理得干干净净。

Hope this helps,

希望这可以帮助,

回答by sophia

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

    class Username
        def to_s
            print "Virtual machine needs you proxy user and password.\n"
            print "Username: " 
            STDIN.gets.chomp
        end
    end

    class Password
        def to_s
            begin
            system 'stty -echo'
            print "Password: "
            pass = URI.escape(STDIN.gets.chomp)
            ensure
            system 'stty echo'
            end
            pass
        end
    end

    config.vm.provision "shell", env: {"USERNAME" => Username.new, "PASSWORD" => Password.new}, inline: <<-SHELL
        echo username: $USERNAME
        echo password: $PASSWORD
SHELL
    end
end