Home > other >  Trying to pass ENV value to VagrantFile but it doesn't work with loop
Trying to pass ENV value to VagrantFile but it doesn't work with loop

Time:08-26

I'm trying to pass an environment variable to my VagrantFile and using the environment variable in a loop to create my VMs. Problem is that it says bad value and doesn't execute the loop.

Vagrant File

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

Command

N_SERVER='3' vagrant up

Error

    13: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/bin/vagrant:194:in `<main>'
    12: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/bin/vagrant:194:in `new'
    11: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:178:in `initialize'
    10: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:974:in `process_configured_plugins'
     9: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:793:in `vagrantfile'
     8: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/environment.rb:793:in `new'
     7: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/vagrantfile.rb:29:in `initialize'
     6: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:116:in `load'
     5: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:116:in `each'
     4: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:119:in `block in load'
     3: from /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/lib/vagrant/config/loader.rb:119:in `each'
     2: from /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: from /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)>': bad value for range (ArgumentError)

CodePudding user response:

That's because the script reads it as a string but it needs an integer to use it for the loop.

You can fix with something like, converting the N_SERVERS as an int when needed

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.to_i).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
  • Related