vagrant-vsphere icon indicating copy to clipboard operation
vagrant-vsphere copied to clipboard

How do I clone a VM multiple times using the same Vagrantfile?

Open ziplinetest opened this issue 9 years ago • 3 comments

I am able to use this plugin to clone an existing VM (using clone_from_vm = true). However this gives me just one clone. How do I modify the Vagrantfile so that I can have multiple VMs cloned from the same source VM?

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.box = "dummy" config.vm.box_url = "./example_box/dummy.box"

config.vm.provider :vsphere do |vsphere| vsphere.host = '10.11.98.125' vsphere.data_center_name = 'dc1' vsphere.template_name = 'VMs/SRC_VM' vsphere.name = 'DEST_VM1' vsphere.clone_from_vm = true vsphere.user = '[email protected]' vsphere.password = 'password' vsphere.insecure = true vsphere.data_store_name = 'datastore1' end

end

ziplinetest avatar Jun 01 '15 18:06 ziplinetest

Look at this: http://docs.vagrantup.com/v2/multi-machine/index.html

mkuzmin avatar Jun 01 '15 22:06 mkuzmin

Hello,

This is what I am using.

I define vm_prefix as the base prefix, say webserver

and the vm_number will be say 3, so you end with webserver1, webserver2, webserver3

#name of vm to current directory
vm_prefix = "webserver"
#number of vms
vm_number = 1
#ram
vm_ram = 1024
#cpu
vm_cpu = 2

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

  config.vm.box = 'dummy'
  config.vm.box_url = '../dummy.box'
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.provision "shell", run: "always", inline: "[ -f /etc/udev/rules.d/70-persistent-net.rules ] && rm -fr /etc/udev/rules.d/70-persistent-net.rules"

  config.ssh.username = 'root'
  config.ssh.password = 'root'
  config.ssh.forward_x11 = true

  (1..vm_number).each do |i|
    config.vm.define vm_name = "#{vm_prefix}%01d" % i do |config|
      config.vm.hostname = vm_name
      config.vm.provider :vsphere do |vsphere|
        vsphere.vm_base_path = 'vagrant-vsphere'  
        vsphere.name = vm_name
        vsphere.template_name = 'oracle6-template'
        vsphere.linked_clone = false
        vsphere.data_store_name = 'datastore1'
        vsphere.memory_mb = vm_ram
        vsphere.cpu_count = vm_cpu
        vsphere.host = '192.168.1.28'
        vsphere.insecure = true
        vsphere.compute_resource_name = '192.168.1.30'
        vsphere.user = '[email protected]'
        vsphere.password = '<password>'
      end
    end
  end
end

kikitux avatar Jun 17 '15 09:06 kikitux

Hi,

this is the way I have been doing this:

jupiter = { 'name' => "jupiter", 'ip' => "192.168.1.141" }
mars = { 'name' => "mars", 'ip' => "192.168.1.142" }
vms = [ jupiter, mars]

Vagrant.configure(2) do |config|

  vms.each do |node|
    name = node['name']
    ip = node['ip']
    config.vm.define name do |node_config|
        node_config.vm.network 'private_network', ip: "#{ip}"
        node_config.vm.box = "dummy"
        node_config.vm.box_url = "./dummy.box"
        node_config.vm.provider :vsphere do |vsphere|
          vsphere.host = 'vcenter01.leobigfoot.com'
          vsphere.name = name
          vsphere.customization_spec_name = 'Ubuntu-15.10'
          vsphere.data_center_name = 'Home'
          vsphere.compute_resource_name = '192.168.1.12'
          vsphere.clone_from_vm = false
          vsphere.template_name = 'Ubuntu-15.10'
          vsphere.data_store_name = 'VMFS02'
          vsphere.memory_mb = '2048'
          vsphere.cpu_count = '2'
          vsphere.user = 'xxxx'
          vsphere.password = 'xxxxxx'
          vsphere.insecure = true
        end
        node_config.vm.provision "shell" do |sh|
         sh.args = ""
         sh.path = "./provision.sh"
        end
    end
  end
end

Basically, I use associative arrays to store the characteristics of my VMs. This gives me the flexibility to completely customize each VM if I want to. Then I put those associative arrays into a normal array and iterate through its elements to create my VMs. It works perfectly. It sets up both VMs (in this example) with the correct IP addresses and hostnames.

Let me know if this helps.

Thanks a lot, Bertrand.

dbblackdiamond avatar Nov 18 '15 08:11 dbblackdiamond