How To Make vRO Execute Python Code Blocks

vRealize Orchestrator (vRO) is a powerful automation platform that enables you to automate and orchestrate various IT processes, including the execution of Python scripts. In this article, we will show you how to set up vRO to run Python scripts and provide some tips along the way.

Prerequisites

Before you can run Python scripts in vRO, you need to have the following:

  • A working installation of vRealize Orchestrator (8.10.2+).
  • A vCloud Suite Advanced or Enterprise license. You cannot run Python scripts with the standard vCenter license for vRO

Setting up vRO to run Python scripts

To set up vRO to run Python scripts, follow these steps:

  • Open the vRealize Orchestrator client and log in with your administrator credentials. (https://<servername>/orchestration-ui)
  • In the main menu, go to the “Assets” and select “Environments” from the submenu.
  • Click the “New Environment” button to create a python3.7 environment.
  • In the “General” tab, enter a name and a description for the environment.
  • Add the needed dependencies. For instance pyvmomi to work with vSphere environments. These are what you would normally install using pip.
  • Adjust memory needed for the environment. 64 MB is not enough to import pyvmomi. Set it to 256 MB or more.
  • Go to the Download Logs tab to check for installation errors
  • Save the environment.

Create a test workflow with a python script

  • In the main menu, go to the “Library” and select “Workflows” from the submenu.
  • Click the “New Workflow” button to create a test workflow.
  • Give it a name and click “Create”
  • Go to the “Schema” tab and drag in a Scriptable task
  • Mark the scriptable task and change the “Runtime Environment” to the python environment you just created.
  • Orchestrator will give you an example code. You should make a mental note of the structure.
import json

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

    outputs = {
      "status": "done"
    }

    return outputs

All inputs will be offered as a python dictionary, and you should return the output from the script as a dictionary as well.

Let’s make a script that will log into vCenter and list the clusters.

You will need to define the following inputs to the script.

  • vCenter Fully qualified domain name
  • Username
  • Password

All three variables should be defined in almost the same way.

  • Under “General” expand “Inputs/Outputs”
  • Under Inputs, click the (+) to add a variable
  • Define a name and a description if you wish
  • vCenterName and Username should be defined as “Type” “string”, but Password should be defined as a “SecureString” Be sure to mark them as Inputs and not variables.
If you need to you can go to the Inputs/Outputs tab and make adjustments.

Lets type some code.

from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import atexit

def handler(context, inputs):

    print(inputs)
    print(context)
    
    vcenter_name = inputs['vCenterName']
    username = inputs['Username']
    password = inputs['Password']

    try:
        vc_connection = SmartConnect(host=vcenter_name, user=username, pwd=password, port=443, disableSslCertValidation=True)
        # You should change disableSslCertValidation to False if you are using trusted certificates
    except exception as e:
        print("Could not connect to vCenter. Error: " + e)
        return {"out" : None}

    atexit.register(Disconnect, vc_connection)

    # Get All Hosts
    content = vc_connection.RetrieveContent()
    vmhosts = [ host for host in content.viewManager.CreateContainerView(content.rootFolder, [vim.HostSystem], recursive=True).view ]

    for vmhost in vmhosts:
        print(vmhost.name)

    Disconnect(vc_connection)

Save the workflow and give it a test run

Running Python scripts in vRO

To run a Python script in vRO, follow these steps:

  1. In the vRealize Orchestrator client, go to the “Library” and select “Workflows” from the submenu.
  2. In the workflow library, locate the workflow that you want to run and click on it.
  3. Click the “RUN” button to start the action.
  4. In the “Start Workflow” dialog, enter the input parameters for the workflow and click “RUN”.

Troubleshooting common issues

If you encounter any issues when running Python scripts in vRO, here are some things you can try:

  • Make sure that you have a vCloud Suite Advanced or Enterprise license, which includes ability for vRO to run Python scripts, and create python 3.7 environments.
  • Check the job log window for any error messages. Correct the code if necessary and rerun the jobs
  • Double-check the input parameters and make sure that they are correct.
  • Make sure that your Python code is correct and does not contain any syntax errors. You can use the validate feature inside the workflow editor.

Conclusion

In this article, we have shown you how to set up vRealize Orchestrator to run Python scripts and provided some tips on how to troubleshoot common issues. With vRO, you can automate and orchestrate various IT processes, including the execution of Python scripts, to save time and effort. However, you need a vCloud Suite Advanced or Enterprise license to use the vRealize Orchestrator feature and run Python scripts in vRO.

I hope it helps you out. Please leave a comment if it did. Also if you want to see how we can make VMware Aria Operations execute workflows automatically to fix issues or remediate compliance issues, please let me know.

Related Post