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

8 comments:

Unknown 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

Unknown 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

Unknown 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?

indirection said...

Thanks for leaving this online ... I took your code and adapted it for mine.

Quickly clean out *all* VMs on your machine:
-----------------------------------
File: ./reprovision-all-vagrant.sh
-----------------------------------
print_line=$(printf '%*s\n' "$(tput cols)" '' | tr ' ' '#')

printf $print_line
echo "### vagrant global-status --prune"
vagrant global-status --prune

printf $print_line
echo "### vagrant global-status | grep running | cut -d ' ' -f 1 "
vagrant global-status | grep running | cut -d ' ' -f 1

printf $print_line
echo "### vagrant global-status | grep running | cut -d ' ' -f 1 | xargs vagrant destroy -f"
vagrant global-status | grep running | cut -d ' ' -f 1 | xargs vagrant destroy -f

printf $print_line
echo "### vagrant global-status"
vagrant global-status

printf $print_line
echo "### vagrant up"
vagrant up


-----------------------------------
File: Vagrantfile
-----------------------------------
# -*- mode: ruby -*-
# vi: set ft=ruby :
hosts = [ { name: 'ubuntu-20-04-1', },
{ name: 'ubuntu-20-04-2', },
{ name: 'ubuntu-20-04-3', }]
Vagrant.configure("2") do |config|
hosts.each do |host|
config.vm.define host[:name] do |node|
node.vm.hostname = host[:name]
node.vm.box = "bento/ubuntu-20.04"
node.vm.network "public_network", bridge: [
"en0: Wi-Fi (Wireless)",
"en7: Thunderbolt Ethernet Slot 2",
]
node.vm.synced_folder ".", "/vagrant", disabled: true
# node.vm.synced_folder "~/Documents-Local/Ansible-LearnLinuxTV", "/data"
node.vm.provider :virtualbox do |vb|
vb.name = host[:name]
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
node.vm.provision "shell", inline: <<-SHELL
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-'
echo "--- $HOSTNAME"
sudo apt-get update
sudo apt-get -y upgrade
sudo timedatectl set-timezone America/New_York
SHELL
end
end
end

indirection said...

Thanks for leaving this online ... I took your code and adapted it for mine.

Quickly clean out *all* VMs on your machine:
-----------------------------------
File: ./reprovision-all-vagrant.sh
-----------------------------------
print_line=$(printf '%*s\n' "$(tput cols)" '' | tr ' ' '#')

printf $print_line
echo "### vagrant global-status --prune"
vagrant global-status --prune

printf $print_line
echo "### vagrant global-status | grep running | cut -d ' ' -f 1 "
vagrant global-status | grep running | cut -d ' ' -f 1

printf $print_line
echo "### vagrant global-status | grep running | cut -d ' ' -f 1 | xargs vagrant destroy -f"
vagrant global-status | grep running | cut -d ' ' -f 1 | xargs vagrant destroy -f

printf $print_line
echo "### vagrant global-status"
vagrant global-status

printf $print_line
echo "### vagrant up"
vagrant up


-----------------------------------
File: Vagrantfile
-----------------------------------
# -*- mode: ruby -*-
# vi: set ft=ruby :
hosts = [ { name: 'ubuntu-20-04-1', },
{ name: 'ubuntu-20-04-2', },
{ name: 'ubuntu-20-04-3', }]
Vagrant.configure("2") do |config|
hosts.each do |host|
config.vm.define host[:name] do |node|
node.vm.hostname = host[:name]
node.vm.box = "bento/ubuntu-20.04"
node.vm.network "public_network", bridge: [
"en0: Wi-Fi (Wireless)",
"en7: Thunderbolt Ethernet Slot 2",
]
node.vm.synced_folder ".", "/vagrant", disabled: true
# node.vm.synced_folder "~/Documents-Local/Ansible-LearnLinuxTV", "/data"
node.vm.provider :virtualbox do |vb|
vb.name = host[:name]
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
node.vm.provision "shell", inline: <<-SHELL
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-'
echo "--- $HOSTNAME"
sudo apt-get update
sudo apt-get -y upgrade
sudo timedatectl set-timezone America/New_York
SHELL
end
end
end

Paul Cobbaut said...

@indirection: You're welcome :)