multi-clone.py: CSV import and MAC customization

After a very busy year, I finally came around to improving my multi-clone.py pyVmomi script. I have added a minor improvement, being able to disable SSL certificate warnings, and two major improvements: CSV import and MAC customization

CSV Import

There is a new command line flag, -C, which allows you to specify a CSV file. This CSV file provides multi-clone.py with the necessary information to clone the template into a VM for each line. The fields in the CSV should be:

A line should look like this:

"VM Name";"Resource Pool";"Folder";"MAC address";"Post-processing script"

If one of the fields is empty, but the command line flag is provided, the value from the command line is used. For instance, if you started the script with the following command line:

python multi-clone.py -C /tmp/clone.csv -H vcenter01.sub.domain.tld -S -t DSL -T 4 -u [email protected] -r Development -f Linux -s /tmp/script.sh

and used the following CSV:

"VM01";"Testing";"Tools";"";"/tmp/tools.sh"
"VM02";"";"";"";""

What would happen is:

So, the command line values are only used if no values in the CSV are provided.

Mac customization

In the CSV, you can provide a MAC address for each VM. This MAC address will be assigned to the first NIC the script finds on the new VM before powering it on (if that is not disabled).

Two warnings:

Overview

As always, you can find the script on my GitHub pagehttps://github.com/pdellaert/vSphere-Python/blob/master/multi-clone.py

For more documentation: https://github.com/pdellaert/vSphere-Python/blob/master/docs/multi-clone.md

And just for good measure, the full description of all flags as presented by the -h flag:

usage: multi-clone.py [-h] [-6] [-b BASENAME] [-c COUNT] [-C CSVFILE] [-d]
                      [-f FOLDER] -H HOST [-i] [-m] [-l LOGFILE] [-n AMOUNT]
                      [-o PORT] [-p PASSWORD] [-P] [-r RESOURCE_POOL]
                      [-s POST_SCRIPT] [-S] -t TEMPLATE [-T THREADS] -u
                      USERNAME [-v] [-w MAXWAIT]

Deploy a template into multiple VM's. You can get information returned with
the name of the virtual machine created and it's main mac and ip address.
Either in IPv4 or IPv6 format. You can specify which folder and/or resource
pool the clone should be placed in. Verbose and debug output can either be
send to stdout, or saved to a log file. A post-script can be specified for
post-processing. And it can all be done in a number of parallel threads you
specify. The script also provides the ability to use a CSV for a lot of it
settings and if you want to specify the mac address of the clones (usefull for
DHCP/PXE configuration).

optional arguments:
  -h, --help            show this help message and exit
  -6, --six             Get IPv6 address for VMs instead of IPv4
  -b BASENAME, --basename BASENAME
                        Basename of the newly deployed VMs
  -c COUNT, --count COUNT
                        Starting count, the name of the first VM deployed will
                        be <basename>-<count>, the second will be
                        <basename>-<count+1> (default = 1)
  -C CSVFILE, --csv CSVFILE
                        An optional CSV overwritting the basename and count.
                        For each line, a clone will be created. A line consits
                        of the following fields, fields inside <> are
                        mandatory, fields with [] are not: "<Clone
                        name>";"[Resouce Pool]";"[Folder]";"[MAC
                        Address]";"[Post Script]"
  -d, --debug           Enable debug output
  -f FOLDER, --folder FOLDER
                        The folder in which the new VMs should reside (default
                        = same folder as source virtual machine)
  -H HOST, --host HOST  The vCenter or ESXi host to connect to
  -i, --print-ips       Enable IP output
  -m, --print-macs      Enable MAC output
  -l LOGFILE, --log-file LOGFILE
                        File to log to (default = stdout)
  -n AMOUNT, --number AMOUNT
                        Amount of VMs to deploy (default = 1)
  -o PORT, --port PORT  Server port to connect to (default = 443)
  -p PASSWORD, --password PASSWORD
                        The password with which to connect to the host. If not
                        specified, the user is prompted at runtime for a
                        password
  -P, --disable-power-on
                        Disable power on of cloned VMs
  -r RESOURCE_POOL, --resource-pool RESOURCE_POOL
                        The resource pool in which the new VMs should reside,
                        (default = Resources, the root resource pool)
  -s POST_SCRIPT, --post-script POST_SCRIPT
                        Script to be called after each VM is created and
                        booted. Arguments passed: name mac-address ip-address
  -S, --disable-SSL-certificate-verification
                        Disable SSL certificate verification on connect
  -t TEMPLATE, --template TEMPLATE
                        Template to deploy
  -T THREADS, --threads THREADS
                        Amount of threads to use. Choose the amount of threads
                        with the speed of your datastore in mind, each thread
                        starts the creation of a virtual machine. (default =
                        1)
  -u USERNAME, --user USERNAME
                        The username with which to connect to the host
  -v, --verbose         Enable verbose output
  -w MAXWAIT, --wait-max MAXWAIT
                        Maximum amount of seconds to wait when gathering
                        information (default = 120)