Using array variables in Vagrantfile?

828 views
Skip to first unread message

Shashank Korada

unread,
Feb 4, 2018, 3:52:47 PM2/4/18
to Vagrant
Hi,

I am trying to write a Vagrantfile that spins up 'N' VMs with static IPs defined my me which are defined by an environment variable(array)

export ARR_IP=("10.20.20.10" "10.20.20.11" "10.20.20.12")

Vagrantfile:

CFG_NUM = 3
CFG_IP = "${ARR_IP[*]}"

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.ssh.forward_agent = true
  (1..CFG_NUM).each do |i|
    config.vm.define CFG_NAME+"#{i}" do |s|
      s.vm.box = "ceph/ubuntu-xenial"
      s.vm.hostname = CFG_NAME+"#{i}"
      s.vm.network :private_network, ip: CFG_IP["#{i-1}"]    ====> trying to iterate over IPs
      s.vm.provider :libvirt do |v|
        v.memory = 4094
        v.cpus = 2
        v.nested = true
      end
    end
  end
end

This is throwing an error for me which says IP is nil
Error occurred while creating new network: {:iface_type=>:private_network, :netmask=>"255.255.255.0", :dhcp_enabled=>true, :forward_mode=>"nat", :ip=>nil, :protocol=>"tcp", :id=>"5d813bfc-2d27-49cf-8351-9948e73f9b66"}.

What is the correct way to substitute the array element here?

Regards


Shashank Korada

unread,
Feb 4, 2018, 4:39:58 PM2/4/18
to Vagrant
Turns out I cant be exporting an array in bash.
So I now want to export a variable with space separated IPs

ARR_IP="10.20.20.10 10.20.20.11 10.20.20.12"

Is there a way to break this up in the Vagrantfile? Some sort of operations?

Alvaro Miranda Aguilera

unread,
Feb 4, 2018, 5:44:18 PM2/4/18
to vagra...@googlegroups.com
you can use:

["192.168.11.11", "192.168.12.12"].to_enum.with_index(1).each do |ip, i|

then you can use the ip and/or the index/counter i

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/8af38eae-0606-4f40-ad09-22b2a04c39b3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Shashank Korada

unread,
Feb 5, 2018, 1:45:05 PM2/5/18
to Vagrant
Thank you Alvaro. This certainly helps but since I am exporting a space separated(or comma separated) variable of IP addresses can I perform operations on the env var in the vagrantfile for it to become a list that u mentioned

export LIST_IP="10.10.10.10 10.10.10.11"

in Vagrantfile:
CFG_IP=$LIST_IP (some action on LIST_IP)

How can I perform some operation on the var LIST_IP in the vagrant file so that CFG_IP becomes ["10.10.10.10", "10.10.10.11"] so that its usable like the way you defined.

Regards,

To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.



--
Alvaro

Shashank Korada

unread,
Feb 5, 2018, 10:10:37 PM2/5/18
to Vagrant
I tried to do the following:

     CFG_IP="$LIST_IPS".split(",") or CFG_IP="$LIST_IPS".split(" ")

Neither worked and added a debug to understand

DEBUG create_networks: In config found network type private_network options {:ip=>"$LIST_IPS", :protocol=>"tcp", :id=>"49129a87-dcdd-4539-a898-e2ef8b2bed38"}

Dont think the substitution worked as I had expected.

Shashank Korada

unread,
Feb 6, 2018, 12:15:13 AM2/6/18
to Vagrant
I got some help and the following can be done if used directly in the vagrantfile

   "10.10.10.11 10.10.10.12".scan(/\S+/)

   although I am unable to substitute the variable that I export. If before running vagrant up I export the variable
   export LIST_OF_IP="10.10.10.11 10.10.10.12"

   and the vagrantfile has the following:
   CFG_IP = ENV['LIST_OF_IP'].scan(/\S+/)

   It throws an error:
   
   Message: NoMethodError: undefined method `scan' for nil:NilClass. Any ideas on how the variable can be substituted

   CFG_IP = ENV['LIST_OF_IP'].scan(/\S+/)   ==> have also tried "$LIST_OF_IP".scan(/\S+/)
   CFG_NAME="test-vm-"

#TODO: Configure private networks and assign static ip

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.ssh.forward_agent = true
  #(1..CFG_NUM).each do |i|
  CFG_IP.to_enum.with_index(1).each do |ip, i|
    config.vm.define CFG_NAME+"#{i}" do |s|
      s.vm.box = "ceph/ubuntu-xenial"
      s.vm.hostname = CFG_NAME+"#{i}"
      s.vm.network :private_network, ip: ip
      s.vm.provider :libvirt do |v|
        v.memory = 4094
        v.cpus = 2
        v.nested = true
      end
    end
  end
end
Reply all
Reply to author
Forward
0 new messages