This post is part 2 of a small series of articles about how to use pyVMomi to build, configure and manage vmware vsphere environments.
For more information on the earlier posts please refer to Part 1
You will recall that the strategy for our sample program is to encapsulate the configuration we want to achieve in a simple YAML file.
In this section we will extend that YAML file structure noting that in a vSphere environment you may want to create multiple datacenters and multiple clusters. For our design we want to be able to cater for multiple datacenters that may contain zero (or more clusters).
For this post we wont add the hosts into the cluster as we’ll cover that in part 3.
Previously we had the following definition :
We will now extend this by adding a section for datacenters that may contain cluster definitions.
As you can see, a topology section has been added. Basically, this allows us to represent multiple ‘dc’ items that can in turn encapsulate multiple (or no) cluster?
Now, let’s look at the code side of things. The YAML parser will take care of capturing our configuration from the labconfig.yaml file. We need to use that information to make the correct calls to the vSphere SDK via pyVmomi.
Before we start we need to have some idea how the objects that we want to create Datacenters and Clusters are reprented in the pyVmomi SDK.
The first place we should look is the github site where the pyVmomi project lives https://github.com/vmware/pyvmomi/.
Let’s drill a little deeper into the documentation and browse through the docs/vim directory. Here we can find considerable documentation on how the vSphere API is mapped to python functions and classes.
In vSphere, objects live in folders. You cannot create a datacenter object outside of the folder structure and you can see by browsing the Folder.rst file that CreateCluster is actually a method of the vim.Folder class.
We have discovered that to create a datacenter we need a folder to put it in. What folder should we use? The choice is arbitrary for this blog so we choose the top of the folder hierarchy known as the rootFolder unless we specifically request a folder name.
Since we are creating and modifying objects we need a strategy to handle situations when objects pre-exist or cannot be modified.
pyVmomi and python in general offer some easy to grasp strategies. pyVmomi allows us to query the vSphere environment to see if objects exist and python allows us to use try … except blocks to recover from faults passed back to us from vSphere.
As Datacenters and Clusters are easily identifiable objects we can search for them by name. If they exist then we’ll leave them alone, otherwise we’ll create them.
To search for objects in pyVmomi we’ll add the following helper function to our VCenter class.
This generic method allows us to query the vSphere environment for an object by name. For example, the following code snippet allows us to query for datacenters by name.
A similar strategy would be used for Clusters, however, in that case the class is vim.ClusterComputerResource
At this point we can extend our initial program to include the following functionality :
- Checking to see if a Datacenter exists and if it does not then create it in the root folder.
- If required, check to see if a specified cluster exists and if it does not then create it in the specified datacenter.
Here is the complete program :
Here is what the VCSA looked like before the job executed.
and this is what it looks like after execution.
If the program is executed a second or subsequent time then you will see warnings as follows :
Other parts in the series can be found below :