将命令行参数传递给 vagrant shell 脚本配置器 [英] Pass command line argument to vagrant shell script provisioner
问题描述
类似于这个问题:将变量传递给shell脚本流浪者中的供应商
我想将变量传递给 shell 脚本供应商,但我想在调用供应商时在命令行上设置这些变量.这可能吗?
I want to pass variables to a shell script provisioner but I want to set these variables on the command line when I call the provisioner. Is this possible?
推荐答案
这就是我要尝试的 - 我想有一些可能性,因为 Vagrantfile 是一个 ruby 脚本,你可以使用大部分 ruby 可能性
This is what I would try - I guess there are possibilities as Vagrantfile is a ruby script, you can use most of ruby possibilities
小心,因为 vagrant 可能需要检查变量,例如在执行 vagrant up arg1 arg2
时,它期望 arg1 和 arg2 是在 Vagrantfile 中定义的机器名称,并且会引发错误找不到
Be careful though as vagrant might need to check for variables, for example when doing vagrant up arg1 arg2
, it expects arg1 and arg2 to be machine names defined in Vagrantfile and will raise an error as it cannot find it
所以你需要像这样传递这些变量
So you would need to pass those variables like
vagrant --arg1 --arg2 up
你可以阅读它们
# -*- mode: ruby -*-
# vi: set ft=ruby :
v1 = ARGV[0]
v2 = ARGV[1]
array_arg = [v1, v2]
Vagrant.configure("2") do |config|
blabla config
array_arg.each do |arg|
config.vm.provision "shell", run: "always" do |s|
s.inline = "echo $1"
s.args = arg
end
end
end
例如,执行会给
fhenri@machine:~/project$ vagrant --arg1 --arg2 up
. . . . .
==> default: Running provisioner: shell...
default: Running: inline script
==> default: stdin: is not a tty
==> default: --arg1
==> default: Running provisioner: shell...
default: Running: inline script
==> default: stdin: is not a tty
==> default: --arg2
这篇关于将命令行参数传递给 vagrant shell 脚本配置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!