You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

传递环境变量至VagrantFile循环创建VM失败:范围值错误

问题:传递环境变量到Vagrantfile后,循环创建虚拟机报错

尝试将环境变量传递至VagrantFile,并用该变量通过循环创建虚拟机,但执行时提示范围值错误,循环无法运行。

原VagrantFile代码

N_SERVERS=ENV["N_SERVER"]
# The "2" in the first line below represents the version of the configuration object config that will be used for configuration for that block (the section between the do and the end). This object can be very different from version to version.
Vagrant.configure("2") do |config| 
 
  #N_ loop to create N worker nodes
  (1..N_SERVERS).each do |i|
    config.vm.define "worker#{i}" do |worker|
     worker.vm.box_download_insecure = true
     worker.vm.box = "hashicorp/bionic64"
     worker.vm.network :private_network, ip: "192.168.56.#{20+i}"
     worker.vm.hostname = "worker#{i}"
     worker.vm.provider "virtualbox" do |v|
      v.name = "worker#{i}"
      v.memory = 2048
      v.cpus = 1
     end   
    end
  end
end

执行命令

N_SERVER='3' vagrant up

错误信息(翻译后)

13: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/bin/vagrant:194:in `<main>'
    12: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/bin/vagrant:194:in `new'
    11: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:178:in `initialize'
    10: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:974:in `process_configured_plugins'
     9: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:793:in `vagrantfile'
     8: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:793:in `new'
     7: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/vagrantfile.rb:29:in `initialize'
     6: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:116:in `load'
     5: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:116:in `each'
     4: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:119:in `block in load'
     3: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:119:in `each'
     2: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:126:in `block (2 levels) in load'
     1: 来自 /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/v2/loader.rb:37:in `load'
/home/xgrid/bash_script/Vagrantfile:6:in `block in <top (required)>': 范围值无效 (ArgumentError)

解决方案

问题出在环境变量的类型:从ENV获取的值是字符串类型(比如你传入的'3'是字符串),而Ruby的范围操作(1..N_SERVERS)要求两端必须是整数,因此会抛出范围值错误。

只需将环境变量转换为整数类型即可,同时建议添加默认值,避免未设置环境变量时出错:

修改后的VagrantFile代码

# 将环境变量转为整数,未设置时默认创建1台虚拟机
N_SERVERS=ENV["N_SERVER"]&.to_i || 1
# The "2" in the first line below represents the version of the configuration object config that will be used for configuration for that block (the section between the do and the end). This object can be very different from version to version.
Vagrant.configure("2") do |config| 
 
  #N_ loop to create N worker nodes
  (1..N_SERVERS).each do |i|
    config.vm.define "worker#{i}" do |worker|
     worker.vm.box_download_insecure = true
     worker.vm.box = "hashicorp/bionic64"
     worker.vm.network :private_network, ip: "192.168.56.#{20+i}"
     worker.vm.hostname = "worker#{i}"
     worker.vm.provider "virtualbox" do |v|
      v.name = "worker#{i}"
      v.memory = 2048
      v.cpus = 1
     end   
    end
  end
end

修改后重新执行原命令N_SERVER='3' vagrant up,即可正常循环创建3台虚拟机。

内容的提问来源于stack exchange,提问作者Muhammad Ali Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 00:15:45