random technotes…

Main menu

Post navigation

Create a new virtual machine in Vsphere with Python, Pysphere and the VMWare API

The VMWare API is very extensive and allows you to do almost all operations that are possible with VMWare using API calls. In order to be able to easily create and deploy new virtual machines, it can be a good idea to standardize and create VM’s using a (Python) script that calls the API. In this post, I will give some examples on how to easily create a new VM using Pysphere and the VMWare API.

Preparation to run the script

In order to use pysphere, we need to install the Pysphere library. The easiest way is to do this with PIP:

1

2

3

4

5

6

7

8

9

10

11

12

13

[jensd@cen~]$sudoyuminstallpython-pip

...

Complete!

[jensd@cen~]$sudopipinstallpysphere

Downloading/unpackingpysphere

Downloadingpysphere-0.1.8.zip(538kB):538kBdownloaded

Runningsetup.pyegg_infoforpackagepysphere

Installingcollectedpackages:pysphere

Runningsetup.pyinstallforpysphere

Successfullyinstalledpysphere

Cleaningup...

In order to simplify my scripts that were using Pysphere, I created a file that can be included into other scripts. The file contains function definitions related to Pysphere. Create a file vm_include.py and copy the following contents to the file or download it here: http://jensd.be/download/vm_include.py

Edit the host, user and password on line 7, 8 and 9 to match your installation.

When executing the script, the new VM should be created as we defined and after creating, it should be started.

1

2

3

[jensd@cen~]$chmod+xcreate-vm.py

[jensd@cen~]$./create-vm.py

Result:Succesfullycreatedguest:newvm

When looking at the inventory in the vSphere client, we can see that these action were performed as we expected:

As you can see, it’s very easy to get started to create VM’s from a script language like Python. This example on itself isn’t very ground-breaking, it’s what you do in combination with the script that matters.

For example, in order to create multiple new VM’s, I combine the above with a script that takes a YAML file as input. The input contains a list of specifications for the new machines. When executed, the script creates the VM’s, creates an entry in DNS for them, generates a root password and stores it in our password manager, gets the MAC of the newly created VM and PXE-boots the new VM with a kickstart file. As a result, it’s possible to deploy 100’s of VM’s including OS-installation and DNS-entry in minutes without any further action and even more important, without mistakes :)

38 thoughts on “Create a new virtual machine in Vsphere with Python, Pysphere and the VMWare API”

Hi Jens,
Thanks for the script for creating vm using Pysphere API. It helped a lot.

Since I am a newbie to Python & Pysphere API, I need help from you.
I want to achieve build automation same way as you have mentioned in this post “Create a new virtual machine in Vsphere with Python, Pysphere and the VMWare API” (http://jensd.be/?p=370).

I want combine the above script with a script that takes a YAML file as input. The input contains a list of specifications for the new machines. When executed, the script creates the VM’s, gets the MAC of the newly created VM and PXE-boots the new VM with a kickstart file.

Can you provide me detailed steps to achieve this ?
Thanks in advance.
Parimal

Hi Jens,
I try to change the format of the disk to by replacing the line disk_backing=VI.ns0.VirtualDiskFlatVer2BackingInfo_Def(“disk_backing”).pyclass() by
disk_backing=VI.ns0.VirtualDiskSparseVer2BackingInfo_Def(“disk_backing”).pyclass()

But I have the error: Result: Cannot create guest: The device or operation specified at index ‘2’ is not supported for the existing virtual machine platform.

Hi Jens,
I found my error.
To enable a disk with a thin provisionning type, I’ve just need to set the thinProvisioned( property, not to change to a VirtualDiskSparseVer2BackingInfo_Def disk that I think is used by VMware workstation.

So, it works perfectly with
disk_backing=VI.ns0.VirtualDiskFlatVer2BackingInfo_Def(“disk_backing”).pyclass()
disk_backing.set_element_thinProvisioned(“True”)

What I did at the time in my Python script was the following:
– Create the VM using the scripts in this post
– Once the VM is created, fetch back the MAC-address of the virtual NIC
– Generate a root password
– Generate a kickstart file (fill in some values in a template)
– Generate a PXE-boot file supplying the kickstart file and name it with the MAC-address
– Start the VM and let it PXE-boot (default in ESX)

The VM will fetch the PXE-boot file and start the installation using the parameters supplied in the kickstart. Once installed, the boot order will prevent the VM to boot from PXE (you could also delete it afterwards).

For the rest of the deployment you can continue with your favourite CM-tool (Ansible/Puppet/Chef/…)

Hello I am facing th same error, I am running the script on ubuntu. Is it ohk? or i needed to run over centos.? I can see that pyshere library is installed which other libraries are needed?
Please help,
Thanks,

Hello, I have solved it with the help of a friend, below I leave the code for those who need it. Basically the problem is that the certificate is self-signed and should be ignored. Just add lines 7 and 11

hi
i install your scripts but i have problem
i change host to *.*
but the error display
//////////////////////////////////////////////////////
Cannot find host: 10.0.0.1
/////////////////////////////////////////////////////
Please help me
Tnx

I am getting following error ->
Note that My Host IP and esx_host ip’s are same because on esx_host only I want to create new vm.
Error —->
Result: Cannot find host: 172.16.213.222
Traceback (most recent call last):
File “./create-vm.py”, line 39, in
main()
File “./create-vm.py”, line 33, in main
vm_include.powerOnGuest(host_con,guest_name)
File “/root/training/python_training/my_practice/net/vm_include.py”, line 213, in powerOnGuest
vm=host_con.get_vm_by_name(guest_name)
File “/usr/lib/python2.7/site-packages/pysphere/vi_server.py”, line 304, in get_vm_by_name
FaultTypes.NOT_CONNECTED)
pysphere.resources.vi_exception.VIException: [Not Connected]: Must call ‘connect’ before invoking this method

I’m hitting the following error. Please help me in resolving this error.
Traceback (most recent call last):
File “C:/Users/VinuMalu’s/PycharmProjects/Malathi/sphere.py”, line 81, in
cg.create_guest_os()
File “C:/Users/VinuMalu’s/PycharmProjects/Malathi/sphere.py”, line 65, in create_guest_os
for p in cr.PropSet:
File “C:\Python27\lib\site-packages\pysphere\ZSI\generate\pyclass.py”, line 160, in get
return getattr(self, what().aname)
AttributeError: ‘DynamicData_Holder’ object has no attribute ‘_propSet’