Pass command line argument to vagrant shell script provisioner

拈花ヽ惹草 提交于 2019-12-31 01:42:11

问题


Similar to this question: Passing variable to a shell script provisioner in vagrant

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?


回答1:


This is what I would try - I guess there are possibilities as Vagrantfile is a ruby script, you can use most of ruby possibilities

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

To read them you could

# -*- 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

for example, the execution would give

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


来源:https://stackoverflow.com/questions/33070700/pass-command-line-argument-to-vagrant-shell-script-provisioner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!