Scheduling and Approving Jobs in Nautobot

One of Nautobot’s most extensible features is Jobs, which allows users to execute custom Python scripts on demand from the Nautobot UI. These custom tasks can be related to Nautobot, but they do not have to be. These can be used to populate data in Nautobot, validate data in Nautobot, or general network automation tasks.

As Nautobot 1.2.0 approaches, we want to highlight some exciting new capabilities with the Jobs feature:

These two addtions extend Jobs’ capabilities and flexibility by allowing users to better define and control workflows. These features together also further extend Nautobot’s capabilities as a platform.

Job Scheduling

Job scheduling allows users to schedule jobs to be run in the future and/or repeated periodically on an hourly, daily, or weekly basis.

Scheduling via the UI

Jobs will be able to be scheduled from the Nautobot UI when the user creates the job:

Scheduling via the UI

Scheduling via the API

Jobs will also be able to be scheduled programmatically via the REST APIs. We’ll demonstrate this with an example.

The Scheduled Jobs screenshot below shows no scheduled jobs:

Scheduling via the API

This Python script will schedule the DeviceConnectionsReport to run weekly starting October 24, 2021:

import requests
import json

from pprint import pprint

class_path = "jobs/local/device_data_validation"

job_class_name = "DeviceConnectionsReport"

url = f"https://192.168.18.2/api/extras/{class_path}/{job_class_name}/run/"


data = {
  "schedule": {
    "name": "Weekly in the future example",
    "interval": "weekly",
    "start_time": "2021-10-24T15:00Z"
  }
}

payload = json.dumps(data)
headers = {
  'Authorization': 'Token nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn',
  'Content-Type': 'application/json'
}

response = requests.post(url, headers=headers, data=payload, verify=False)

pprint(response.json())

Once the script completes, we see the scheduled job:

scheduled jobs

Job Approval

This feature allows job scripts to be flagged to require approval; jobs with this flag will not be executed immediately. Instead, they will be put into a Jobs Approval Queue where any user other than the Job submitter can approve or deny the queued Job or approve it for dry-run. Once approved, the job will be run immediately unless it is scheduled to run at a later time.

Jobs can be set to require approval when the approval_required = True attribute is set in the job script’s Meta object:

Job Approval

Approval via UI

Jobs can be approved in the Nautobot UI on the Jobs Approval Queue page:

Approval via UI

Approval via API

Jobs can also be approved programmatically via REST calls. This example script below approves a job with a specific job id:

import requests

from pprint import pprint

job_id = "ba1852e4-10fa-4fd9-bddb-2d0bacdd21d9"

action = "approve"  # Could also be "deny" or "dry-run"

url = f"https://192.168.18.2/api/extras/scheduled-jobs/{job_id}/{action}/"

payload={}
headers = {
  'Authorization': 'Token nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn'
}

response = requests.post(url, headers=headers, data=payload, verify=False)

pprint(response.json())

Conclusion

Scheduling and approving jobs gives users better control of Job workflows initiated from Nautobot. These two features will be arriving in Nautobot 1.2.0. Stay tuned!

Happy coding!

-Tim Fiola



ntc img
ntc img

Contact Us to Learn More

Share details about yourself & someone from our team will reach out to you ASAP!

Author