Automation Python Runbook Job

The Automation Python Runbook job type runs a published Python runbook (Python 3.10 is the supported runtime) in an Azure Automation account. Each Polysync execution creates one Automation job, polls it to completion, and reads the runbook's result back as output parameters. The runbook is identified by its name — Polysync stores that in the Job's External Id.

This job type is supported on the Azure Automation platform. PowerShell runbooks use the Automation Runbook job type.

Required job fields

  • External Id — the runbook name (e.g. export_daily_files). Set automatically on import.
  • Job Type — Automation Python Runbook (set automatically on import).

Optional job setting:

  • Hybrid Worker Group — the Hybrid Runbook Worker group the job runs on (the Automation job's runOn). Leave it blank to run in the Azure sandbox. To let each task choose its own group, also add a parameter named Hybrid Worker Group to the Job: a task value wins over the Job setting and is never passed to the script.

Job discovery

Python runbooks take no named parameters — Azure Automation passes arguments to the script as sys.argv[1:]. Discovery therefore imports the runbook with no parameters. Add the arguments on the Job's Parameters tab, in the order the script reads them.

Parameter handling

Polysync passes the job's Input and Input & Output parameters as positional arguments, in the order the parameters are listed on the Job (top to bottom). The parameter names are for you; the script only sees the values:

import sys

source_folder = sys.argv[1]   # first parameter on the Job
run_date = sys.argv[2]        # second parameter on the Job

Rules:

  • Every argument is a string. Convert it in the script (int(sys.argv[2]), json.loads(...)).
  • A blank parameter is still sent, as an empty string, so later arguments keep their positions.
  • Spaces inside a value are kept (hello world arrives as one argument).
  • Double quotes inside a value are removed by Azure Automation (say "hi" arrives as say hi, {"a":1} as {a:1}). To pass JSON, base64-encode it or use single quotes.
  • Output parameters are not sent.

Values are stored with the job. Azure Automation keeps each job's input for 30 days and shows it in the portal. Do not pass secrets as arguments.

Output parameters

Print a JSON object as the last line of output:

import json
print(json.dumps({"filesExported": 12, "resultNote": "done"}))

When the job completes, Polysync reads the job's output stream and takes the last line that is a JSON object; earlier lines are ignored. Each top-level property fills the Output or Input & Output parameter of the same name (matched case-insensitively). Child tasks can then map them through task dependencies.

Execution flow

  1. Polysync reads the runbook: a runbook that has never been published is refused with an instructive message.
  2. It creates an Automation job — PUT …/automationAccounts/{account}/jobs/{guid} with the runbook name, the arguments and runOn (the Hybrid Worker Group, or blank for the Azure sandbox). Azure Automation orders the arguments by their key, so Polysync keys them 001, 002, … in Job order.
  3. The dispatcher polls GET …/jobs/{guid} and maps the job status exactly as for the Automation Runbook job type (Completed → Success, Failed → Failed with the exception text, Stopped → Cancelled, or Failed when a cloud job reached the three-hour fair-share limit).
  4. On Completed, Polysync reads GET …/jobs/{guid}/output and fills the output parameters.
  5. Cancel sends POST …/jobs/{guid}/stop; the run is Cancelled.

Monitor URL

The run links to the job in the Azure portal: https://portal.azure.com/#@/resource/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Automation/automationAccounts/{account}/jobs/{jobId}

Troubleshooting

Symptom Likely cause Fix
Runbook … has never been published The runbook is still a draft Publish it in Azure Automation
IndexError: list index out of range in the job's exception The script reads more arguments than the Job has parameters Add the missing parameters on the Job, in order
Arguments arrive in the wrong order The Job's parameters are listed in a different order from the script Reorder the parameters on the Job
JSON argument fails to parse Azure Automation removed the double quotes Base64-encode the JSON, or use single quotes
Output parameters stay empty The script did not print a JSON object last, or the names differ Print json.dumps(...) last; match the parameter names