2014-04-19

Vagrant: Creating 10 vm's with 6 disks each

Hello lazyweb,

the Vagrantfile below works fine, but can probably be written simpler. I've been struggling to create variables like "servers=10" and "disks=6" to automate creation of 10 servers with 6 disks each.

Drop me a hint if you feel like creating those two loops.


paul@retinad:~/vagrant$ cat Vagrantfile
hosts = [ { name: 'server1', disk1: './server1disk1.vdi', disk2: 'server1disk2.vdi' },
          { name: 'server2', disk1: './server2disk1.vdi', disk2: 'server2disk2.vdi' },
          { name: 'server3', disk1: './server3disk1.vdi', disk2: 'server3disk2.vdi' }]

Vagrant.configure("2") do |config|

  config.vm.provider :virtualbox do |vb|
   vb.customize ["storagectl", :id, "--add", "sata", "--name", "SATA" , "--portcount", 2, "--hostiocache", "on"]
  end

  hosts.each do |host|

    config.vm.define host[:name] do |node|
      node.vm.hostname = host[:name]
      node.vm.box = "chef/centos-6.5"
      node.vm.network :public_network
      node.vm.synced_folder "/srv/data", "/data"
      node.vm.provider :virtualbox do |vb|
        vb.name = host[:name]
        vb.customize ['createhd', '--filename', host[:disk1], '--size', 2 * 1024]
        vb.customize ['createhd', '--filename', host[:disk2], '--size', 2 * 1024]
        vb.customize ['storageattach', :id, '--storagectl', "SATA", '--port', 1, '--device', 0, '--type', 'hdd', '--medium', host[:disk1] ]
        vb.customize ['storageattach', :id, '--storagectl', "SATA", '--port', 2, '--device', 0, '--type', 'hdd', '--medium', host[:disk2] ]
      end
    end

  end

end

5 comments:

Abel Lopez said...

I just had to do the same for a ceph cluster, I did it like this:
(1..3).each do |i|
config.vm.define "ceph#{i}" do |node|
node.vm.hostname = "ceph#{i}"
node.vm.network "private_network", ip: "172.24.0.#{i}", intnet: true
config.vm.provider "virtualbox" do |v|
v.memory = 512
v.cpus = 1
v.customize ['createhd', '--filename', "ceph_disk_#{i}a.vdi", '--size', 8192 ]
v.customize ['createhd', '--filename', "ceph_disk_#{i}b.vdi", '--size', 8192 ]
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "./ceph_disk_#{i}a.vdi"]
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', "./ceph_disk_#{i}b.vdi"]
end
end

end

Abel Lopez said...

I had to do a ceph cluster (3 nodes, 2 disks each)
I did it like:

(1..3).each do |i|
config.vm.define "ceph#{i}" do |node|
node.vm.hostname = "ceph#{i}"
node.vm.network "private_network", ip: "172.24.0.#{i}", intnet: true
config.vm.provider "virtualbox" do |v|
v.memory = 512
v.cpus = 1
v.customize ['createhd', '--filename', "ceph_disk_#{i}a.vdi", '--size', 8192 ]
v.customize ['createhd', '--filename', "ceph_disk_#{i}b.vdi", '--size', 8192 ]
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "./ceph_disk_#{i}a.vdi"]
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', "./ceph_disk_#{i}b.vdi"]
end
end

end

Paul Cobbaut said...

Thanks. I'll post my script when I get around to testing it.

Paul Cobbaut said...

It took a while, but here is my current Vagrantfile for any number of machines:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider :virtualbox do |vb|
vb.customize ["storagectl", :id, "--add", "sata", "--name", "SATA" , "--portcount", 2, "--hostiocache", "on"]
end
(1..3).each do |i|
config.vm.define "server#{i}" do |node|
node.vm.hostname = "server#{i}"
node.vm.box = "hfm4/centos7"
config.vm.box_check_update = true
node.vm.network :public_network, ip: "10.1.1.#{i}", netmask: '255.255.255.0'
node.vm.network :public_network, ip: "10.1.2.#{i}", netmask: '255.255.255.0'
node.vm.network :public_network, ip: "10.1.3.#{i}", netmask: '255.255.255.0'
config.vm.provider "virtualbox" do |v|
v.name = "server#{i}"
v.memory = 512
v.cpus = 1
v.customize ['createhd', '--filename', "server_#{i}a.vdi", '--size', 8192 ]
v.customize ['createhd', '--filename', "server_#{i}b.vdi", '--size', 8192 ]
v.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "./server_#{i}a.vdi"]
v.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', "./server_#{i}b.vdi"]
end
end
end
end

Marko KoĊĦmerl said...

I'm using the same configuration but vagrant gets stuck trying to connect via SSH.

==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2200
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...

Any clues why?