我正在寻找一种像这样将参数传递给Chef cookbook的方法:
$ vagrant up some_parameter
然后在其中一个Chef烹饪书中使用some_parameter
参数。
我正在寻找一种像这样将参数传递给Chef cookbook的方法:
$ vagrant up some_parameter
然后在其中一个Chef烹饪书中使用some_parameter
参数。
MY_VAR='my value' vagrant up
使用 ENV['MY_VAR']
在配方中。
您还可以包含 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命令之前指定自定义选项,以避免无效选项验证错误。
有关该库的更多信息在此处。
`vagrant --custom-option=option destroy -f`
`vagrant:无效选项--f`
- Renat Zaripovvagrant --custom-option=option -- up
应该就足够了。 - CESCO在进行配置阶段之前,可以从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
puts ARGV
可以正确显示数组。 - majkinetorvagrant/embedded/gems/gems/vagrant-1.7.2/lib/vagrant/plugin/v2/command.rb
中添加了一行puts "#{ARGV}"
代码,并且它会在删除Vagrantfile中相关参数之前打印该行,这意味着删除是徒劳的,因为ARG被传递给验证器,验证器在对ARGV进行任何操作之前输出“指定了无效选项”。 - BogdanSorlearequire '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
可以得到清晰的处理。
希望这能帮到您,
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