How to use Python to start an Orchestrator Workflow

In a previous article, I taught you how to explore and use the REST API to start a Workflow using a generic browser based REST Client. In this article, I will provide a Python based example of running the “Create a Record” workflow that was created in Part 2 of my SQL Plug-in Dynamic Types Simple CMDB for vCACarticle. Since I’m not even close to being proficient with Python, this will be a very short article! You may download the script in this article from my vroClientScripts Repository on GitHub. Be sure to check out that repo because my colleague has provided a better written Python module there for calling workflows.

Getting Started

In my efforts to figure out how to consume a REST API using Python, I found the following sites helpful:

In particular, be sure you have a functional Python install with the “requests” module installed and ready to use. From what I can tell, the “json” module is part of the recent Python versions so no extra step should be required to install that.

The Script

The following code should be saved as something like runWorkflow.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Update for your environment here. Use single quotes around each defined value.
usr = {REPLACE WITH YOUR VRO USERNAME}
pwd = {REPLACE WITH YOUR VRO PASSWORD}
wfid = {REPLACE WITH YOUR VRO WORKFLOW ID}
#NOTE: use double \\ or single / when specifying file path
jsonFile = {REPLACE WITH PATH TO JSON BODY FILE}
vroServer = {REPLACE WITH VRO URL:PORT}

##### Make no changes below this line ####
# Import the modules to handle HTTP calls and work with json:
#
# requests: http://docs.python-requests.org/en/latest/user/install/
# To install the "requests" module, python -m pip install requests
# json (http://docs.python.org/2/library/json.html)
#
#####
import requests, json
requests.packages.urllib3.disable_warnings()

# Create basic authorization for API
vroAuth = requests.auth.HTTPBasicAuth(usr,pwd)
# Set headers to allow for json format
headers = {'Content-Type':'application/json','Accept':'application/json'}
url = 'https://' + vroServer + '/vco/api/workflows/' + wfid + '/executions'
data = open(jsonFile).read()
# NOTE: verify=False tells Python to ignore SSL Cert issues
# Execute a workflow using a json file for the body:
r = requests.post(url, data=data, verify=False, auth=vroAuth, headers=headers)

Note: Before attempting to run the script, be sure to modify the parameters at the beginning of the script to reflect YOUR workflow.

I have kept the script as simple as I can, including the option to NOT Verify the SSL Certificate so that self-signed certs do not prevent the script from running.

The json file

While testing this, my example workflow was the “Create a Record” workflow as noted earlier. That workflow had a number of inputs so these were loaded into a json file. In order to learn more about how to get the correct input json for your workflow, please reference my article: How to use the REST API to Start a Workflow.

Here’s my example json file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
    "parameters": [
        {
            "type": "string",
            "scope": "local",
            "name": "ServerName",
            "value": {
                "string": {
                    "value": "vcac-iaas-01c"
                }
            }
        },
        {
            "type": "string",
            "scope": "local",
            "name": "ServerIP",
            "value": {
                "string": {
                    "value": "192.168.110.89"
                }
            }
        },
        {
            "type": "string",
            "scope": "local",
            "name": "ServerDNS",
            "value": {
                "string": {
                    "value": "corp.local"
                }
            }
        },
        {
            "type": "string",
            "scope": "local",
            "name": "ServerID",
            "value": {
                "string": {
                    "value": "vcac-iaas-01d.corp.local"
                }
            }
        },
        {
            "type": "string",
            "scope": "local",
            "name": "ServerOwner",
            "value": {
                "string": {
                    "value": "administrator"
                }
            }
        }
    ]
}

Example Run

python_example_run.png

I ran the following from my Windows desktop command prompt:

1
2
3
C:\tools\python>python e:\runWorkflow.py
C:\tools\python\lib\site-packages\requests\packages\urllib3\connectionpool.py:734: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)

When I check the Orchestrator client, I can see that the workflow has run and completed successfully (See screenshot above!)

Help Me Make This Sample Better!

As noted at the beginning of this article, I don’t know Python but I managed to get a very simple example script working here. Please submit your comments and/or improvement suggestions to help increase the value and usefulness of this simple script.

Improvements needed

  • Get credentials from a hidden config file
  • Pass in all parameters as command line arguments
  • Display/parse output

Thanks!