VCF Automation Blog

from Stefan Schnell

VCF Automation offers five runtime environments: JavaScript, Node.js, PowerCLI (PowerShell), PowerShell and Python. VMware PowerCLI is a command-line and scripting tool built on PowerShell, to manage and automate VMware infrastructure. For similar purposes VMware also offers pyVmomi, which is based on Python. This now raises the question whether it is possible to build an equivalent runtime environment with Python and pyVmomi. This blog describes how this can be achieved.

Building a Python Runtime Environment
with pyVmomi


First we need to prepare pyVmomi for the use in VCF Automation.
"""
@runtime python:3.10
@memoryLimit 512000000
@timeout 600
@param {SecureString} in_password
@outputType Properties
"""

import json
import ssl
from pyVim.connect import *

def handler(context, inputs):
    jsonOut = json.dumps(inputs, separators = (",", ":"))
    # print("Inputs were {0}".format(jsonOut))

    context = ssl._create_unverified_context()

    try:

        # The information on the connection is exemplary from the HOL.
        # The information are available at:
        # Assembler > Infrastructure > Cloud Accounts
        serviceInstance = Connect(
            host = "vcsa-01a.corp.vmbeans.com",
            port = 443,
            user = "administrator@corp.vmbeans.com",
            # pwd = "VMware1!",
            pwd = inputs["in_password"],
            sslContext = context
        )

        serviceInstanceContent = serviceInstance.RetrieveContent()

        aboutInfo = serviceInstanceContent.about
        print("Name:", aboutInfo.name)
        print("Fullname:", aboutInfo.fullName)
        print("Vendor:", aboutInfo.vendor)
        print("Version:", aboutInfo.version)
        print("Build:", aboutInfo.build)
        print("OS:", aboutInfo.osType)
        print("API Type:", aboutInfo.apiType)
        print("API Version:", aboutInfo.apiVersion)
        print("Unique Id:", aboutInfo.instanceUuid)
        print("License Product Name", aboutInfo.licenseProductName)
        print("License Product Version:", aboutInfo.licenseProductVersion)

        Disconnect(serviceInstance)

    except Exception as ex:
        print(ex.msg)

    outputs = {
        "status": "done"
    }

    return outputs

Now we can import this ZIP package into VCF Automation. Switch to runtime environment Python, choose the type Zip and import it. As return type choose Properties and add to the inputs the argument in_password with the SecureString type. Now it is possible to execute the action, after the password has been entered.

vmware aria automation with pyvmomi in python runtime environmentVCF Automation with pyVmomi in Python runtime environment

Conclusion

As we can see, it is possible with just a few steps to build an environment to manage and automate VMware infrastructure on Python. pyVmomi, which comes from VMware itself, is designed for this. Combining this with VCF Automation offers us the possibility to work with pyVmomi, similar to PowerCLI, in this context.