Skip to content

Python actions and environments

JavaScript is Orchestrator’s native language, but it isn’t the only one. An action or a scriptable task can also run Python, Node.js, PowerShell, or PowerCLI. Each runs outside the Rhino engine, in its own container, and can use the libraries of that language. That’s the reason to choose one: a Python package that parses certificates, talks to a vendor’s API, or reads a spreadsheet saves you writing the same thing in ES5.

In this recipe you write a small Python action with no extra packages, then build an environment that adds the cryptography package, and use it in an action that reports when a server’s TLS certificate expires. Last, you call that Python action from JavaScript.

Getting ready

  • The action module com.vcfguide.chapter4 from recipe 4.1.
  • Access from the Orchestrator appliance to a Python package repository. That’s PyPI on the internet, or an internal mirror that offers the same “simple” index, such as a Nexus or Artifactory proxy.
  • An administrator login, for the repository setting in step 4.
  • A TLS server to test against. The examples use vc01.vmw.lab on port 443.

How to do it

Write a first Python action

  1. In Library > Actions, create the action splitHostName in the module com.vcfguide.chapter4.
  2. On the Script tab, set the Runtime Environment to Python 3.14. Add one input, fqdn, of type string, and set the Return type to Properties. Leave Memory limit and Timeout at their defaults.
  3. Replace the sample code with this script and click Save:
com.vcfguide.chapter4/splitHostName
def handler(context, inputs):
"""Splits a fully qualified host name into the host and the domain."""
fqdn = inputs["fqdn"].strip().lower()
host, _, domain = fqdn.partition(".")
print("Run " + str(context.get("executionId")) + ": " + host + " in " + domain)
return {"host": host, "domain": domain}
  1. Run it with fqdn = vc01.vmw.lab. The result is host = vc01 and domain = vmw.lab, and the log shows the line printed by print. The first run takes a few seconds longer than a JavaScript action, while Orchestrator starts the Python runtime.
Script tab of the action splitHostName. Runtime Environment Python 3.14, Type Script. The editor shows def handler(context, inputs) splitting inputs fqdn with partition and returning a dictionary with host and domain. Properties: Return type Properties; input fqdn of type string described as Fully qualified host name; Memory limit (MB) 64; Timeout (seconds) 180.
Figure 4.6. A Python action: the runtime, the typed inputs and return type, the limits, and the handler function.
Needs a lab check. On 9.1.1, check the sample code the editor fills in for a new Python action, and the keys of context (the 9.x docs name vroURL and executionId; executionId works, as the script above shows).

Allow a package repository

  1. As an administrator, open System Settings > Performance and Features > Environment Repositories.
  2. Choose where packages come from:
    • To use PyPI (and npm, for Node.js), switch on Enable Default Public Repository for Environments under Default Public Repository. The switch carries the note I acknowledge the security implications of enabling the Default Public Repository. A second switch, Suppress the warning when selecting the Default Public Repository, stops the client from warning each time you pick it.
    • To use an internal mirror, switch on Enable Additional Repositories for Environments, add the mirror, and pick it later for each dependency.
  3. Click Save. Saving this page restarts the Orchestrator server: a banner says Configuration Changes detected. The Orchestrator server is expected to restart in … seconds.
System Settings, Performance and Features page. Under Environment Repositories, the switches Enable Default Public Repository for Environments and Suppress the warning when selecting the Default Public Repository are on.
Figure 4.7. Allowing Orchestrator to download packages.

Build an environment

  1. Go to Assets > Environments and click New Environment.
  2. On the General tab, enter the name vcfguide_python_crypto and the description Python 3.14 with cryptography, for the VCF Orchestrator Guide.
  3. On the Definition tab, set the Runtime to Python 3.14. Under Dependencies, click Add and enter:
  • Name: cryptography
  • Version: 43.0.3
  • Repository: the default, or your mirror
  1. Click Create. Orchestrator downloads the package and builds the environment. Open the Download Logs tab and check that cryptography and the packages it needs were installed. The log ends with Successfully installed and [Completed downloading dependencies], and the status next to the name reads Up To Date. Expect a few lines marked ERROR along the way: the log files everything pip writes to its error stream under that level, including harmless warnings. Judge the build by the last lines and the status, not by the colour.
Environment vcfguide_python_crypto, status Up To Date, Download Logs tab. pip collects cryptography 43.0.3, cffi and pycparser as ready-built wheels, installs them, and ends with Completed downloading dependencies. Three lines in between are marked ERROR but are only pip warnings.
Figure 4.8. An environment with one Python dependency, after a successful build.

Use the environment from an action

  1. Create the action getCertificateExpiry in com.vcfguide.chapter4. On the Script tab, set the Runtime Environment to the environment vcfguide_python_crypto instead of plain Python 3.14.
  2. Add the inputs hostName of type string and port of type number, and set the Return type to Properties. Enter this script:
com.vcfguide.chapter4/getCertificateExpiry
import socket
import ssl
from datetime import datetime, timezone
from cryptography import x509
def handler(context, inputs):
"""Returns when the TLS certificate of hostName:port expires. Reads only."""
host = inputs["hostName"]
port = int(inputs.get("port") or 443)
# Read the certificate whoever signed it: we only want its dates
tls = ssl.create_default_context()
tls.check_hostname = False
tls.verify_mode = ssl.CERT_NONE
with socket.create_connection((host, port), timeout=10) as sock:
with tls.wrap_socket(sock, server_hostname=host) as conn:
der = conn.getpeercert(binary_form=True)
cert = x509.load_der_x509_certificate(der)
not_after = cert.not_valid_after_utc
days_left = (not_after - datetime.now(timezone.utc)).days
print(f"{host}:{port} certificate expires {not_after:%Y-%m-%d} ({days_left} days)")
return {
"host": host,
"subject": cert.subject.rfc4514_string(),
"notAfter": not_after.isoformat(),
"daysLeft": days_left,
}
  1. Run it with hostName = vc01.vmw.lab and port = 443. The result shows the certificate’s subject, its expiry date, and the days left.
Action getCertificateExpiry after a run with hostName vc01.vmw.lab and port 443. Action Result: notAfter 2028-09-06T07:04:20+00:00, subject C=US,CN=vc01.vmw.lab, host vc01.vmw.lab, daysLeft 646. Below it, the Logs tab with the line vc01.vmw.lab:443 certificate expires 2028-09-06 (646 days).
Figure 4.9. The Python action's result: the certificate of vc01.vmw.lab and the days it has left.

Call it from JavaScript

  1. Python actions are called like any other. In a JavaScript action or scriptable task:
Calling the Python action from JavaScript
var expiry = System.getModule("com.vcfguide.chapter4").getCertificateExpiry("vc01.vmw.lab", 443);
System.log(expiry.get("host") + " certificate expires in " + expiry.get("daysLeft") + " days");
if (expiry.get("daysLeft") < 30) {
System.warn("Renew the certificate of " + expiry.get("host") + " soon");
}
Needs a lab check. Check on 9.1.1 that a JavaScript script can call a Python action through System.getModule, that the returned dictionary arrives as Properties, and whether daysLeft arrives as a number. Also confirm that the Python container can reach vc01.vmw.lab on port 443 (it runs on the appliance, so it should resolve lab names).

How it works

A JavaScript action runs inside the Orchestrator server. A Python action doesn’t. When it’s called, Orchestrator turns the inputs into JSON, starts the chosen runtime in a separate container on the appliance, and calls your function handler(context, inputs). When the function returns, Orchestrator turns its return value back from JSON into the action’s return type. That’s why the action needs a typed return value, and why a Python function returns a plain dict where JavaScript returns Properties.

The JSON trip decides what you can pass:

  • Inputs and return values can be string, number, boolean, Date, Properties, composite types, and arrays of these. A dict maps to Properties, and a list to an array.
  • Plug-in objects don’t cross. A Python action can’t take a VC:VirtualMachine and call its methods, because the object lives in the Java process and the script doesn’t. Pass the VM’s name or ID as a string, and do the vSphere work in JavaScript or through the vCenter API from Python.
  • context carries details of the call. The 9.x documentation names vroURL, the address of the Orchestrator server, and executionId, the ID of the run.

Each runtime has limits per run, shown on the action’s Script tab as Memory limit (MB) and Timeout (seconds). The defaults are 64 MB of memory and 180 seconds for Python and Node.js, and 150 MB and 180 seconds for PowerShell and PowerCLI; on 9.1.1 the server reports the memory defaults through GET /vco/api/server-configuration/polyglot-runtimes. A run that goes over either limit fails. Change them on the action’s Script tab, or for every action that uses an environment, on the environment’s Definition tab.

An environment is a runtime plus the packages and environment variables your scripts need. Orchestrator downloads the packages once, when you create or change the environment, and every action that uses it gets them. Packages come only from the repositories allowed under Environment Repositories: PyPI for Python, npm for Node.js, and the PowerShell Gallery or another NuGet v2 feed for PowerShell. In VCF 9 the public repositories are off until an administrator turns them on.

Back in vRO 5.5–7Write every script in JavaScript. To use Python or PowerShell, run it on another host through the SSH or PowerShell plug-in, or start a local process after allowing it with a system property.
In Orchestrator 9.xChoose Python 3.14, Node.js 24, PowerShell 7.6, or PowerCLI 9.1.1 (on PowerShell 7.6) as the runtime of an action or scriptable task, and add packages with an environment.

On the lab’s Orchestrator 9.1.1, GET /vco/api/server-configuration/polyglot-runtimes returns exactly four runtimes: node:24, python:3.14, powershell:7.6 and powercli:9.1.1-powershell-7.6. Broadcom’s 9.1 core concepts page still says Node.js 20; the server is the one to trust. Run the same call on your own server before you pick a runtime.

There's more

9.1.2: type hints for inputs . From 9.1.2, the editor understands Python type hints for input parameters and checks references to them while you type. The release notes don’t show the syntax.

Needs a lab check. From the 9.1.2 release notes; not yet tried in the lab, which runs 9.1.1. On 9.1.2, find out where the hints go (on the handler signature, or on inputs), how a typo in an input name is reported, and whether the hints change anything at run time.

9.1.2: one page for every repository . From 9.1.2, the package repositories of step 5 and the Git repositories of recipe 6.3 are managed together under Administration > Repositories, where an administrator can search, filter, edit and delete them. Broadcom’s point is control: environments can only take packages from repositories an administrator approved. On 9.1.1 and earlier, use System Settings > Performance and Features > Environment Repositories as in step 5.

Needs a lab check. From the 9.1.2 release notes; not yet tried in the lab, which runs 9.1.1. On 9.1.2, record whether Environment Repositories under Performance and Features is gone or redirects to Administration > Repositories, and the labels for adding a PyPI or npm mirror there.

Keep the loop inside Python. Every call to a Python action starts a runtime, which costs seconds rather than milliseconds. Checking 50 certificates with 50 calls from a JavaScript loop is slow. Pass the list of hosts in one call, as an Array/string input, and loop in Python.

Pin your versions. Give every dependency an exact version, as in step 9. An environment with no version takes whatever is newest the next time it’s rebuilt, and an upgrade of a package you didn’t touch can break your action. Change versions on purpose, then run the actions that use the environment.

Read environment variables with os.environ. Values on the environment’s environment variables list reach the script as ordinary process environment variables, for example os.environ.get("LOG_LEVEL", "INFO"). Don’t keep passwords there: anyone who can open the environment can read them. Pass a secret as an input instead, from a configuration element (recipe 4.7).

Calling Orchestrator back. On 8.x, context also held a function getToken that returns a bearer token, so a Python script can call the Orchestrator REST API at vroURL as the user who started the run. Test it before you rely on it in VCF 9.

Needs a lab check. Check whether context“getToken” exists on 9.1.1 and returns a token that the REST API at context[“vroURL”] accepts.

ZIP actions for bundled code. If a package isn’t in any repository you can reach, or your script is several files, set the action’s type to Zip, import a ZIP file, and set the entry handler. The default, handler.handler, means the function handler in the file handler.py at the top of the ZIP. For a function main in check.py, enter check.main. Put packages in the ZIP with pip install --target=. <package> before you zip the folder. An environment is easier to maintain, so prefer one when you can.

Packages that need system libraries. A few Python packages need operating system libraries that the runtime image doesn’t have, or are compiled for a different system. Broadcom’s 9.1 documentation has a page on configuring a Photon OS container for such Python packages. Read it before you fight a failed build in the Download Logs tab.

Debug in your own editor. Python and Node.js scripts can be debugged from an external IDE, which Broadcom’s documentation covers. PowerShell and PowerCLI scripts can’t.

See also