简体   繁体   中英

Vagrant: passing parameters in windows

I already found this question about how to pass parameters to the Vagrantfile environment, but it seems that it doesn't work on windows. In fact if I try to run:

SERV=client vagrant up

With this Vagrantfile:

# -*- mode: ruby -*-
# # vi: set ft=ruby :

# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"

# Require YAML module
require 'yaml'

# Read YAML file with box details
servers = YAML.load_file('RaftFS/servers.yaml')

# Create boxes
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Create servers
  # Iterate through entries in YAML file
  if ENV['SERV'] != "client"
      servers.each do |key,value|
        if key == ENV['SERV']
            config.vm.define key do |srv|
                srv.vm.box = value['box']
                    #srv.vm.network "private_network", ip: value['ip']
                if value['ip'] != ''
                    srv.vm.provision "shell", inline: "echo NO IP ADDRESS"
                    srv.vm.network :public_network, bridge:'wlan0'
                else        
                        srv.vm.network :public_network, ip:value['ip'] ,bridge:'wlan0'
                        srv.vm.provision "shell", inline: "echo IP FOUND FOR"
                end
                srv.vm.hostname=key
                srv.vm.synced_folder ".", "/vagrant" , disabled:true
                srv.vm.synced_folder "ServersFS/"+key+"/", "/vagrant/ServersFS" , create: true
                srv.vm.synced_folder "./RaftFS", "/vagrant/RaftFS"
                srv.vm.provision :shell do |shell|
                  shell.path = "provision.sh"
                  shell.args = "'TRUE'"
                end
                srv.vm.provider :virtualbox do |vb|
                    vb.name = key
                    vb.memory = value['ram']
                    end
            end
        end
      end
  else
      config.vm.define "client" do |cln|
        cln.vm.box = "hashicorp/precise32"
        cln.vm.network :public_network, bridge:'wlan0', ip:"192.168.1.140"
        cln.vm.hostname="client"
        cln.vm.provision :shell do |shell|
          shell.path = "provision.sh"
          shell.args = "'FALSE'"
        end
      end
  end
end

Windows prompt doesn't recognize SERV=client as valid command. I'm sorry for the question, but I'm totally new with both Vagrant and Ruby (and I usually program on Linux)!

So I stumbled on this issue as well. To pass parameters from command prompt to Vagrantfile it should pass as an environment variable, and you can do it in one line:

set "SERV=client" && vagrant up

In the Vagrantfile, you then can access the parameter as ENV['SERV']

A heads-up is that the environment variable will still exist in the environment after vagrant has finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM