I've been evaluating the Versa Director API for automating some of our network configuration management workflows. The built-in diff tools are useful for manual review, but I needed a programmatic way to capture configuration deltas between templates or device states for logging and analysis.
The script below uses the `/api/config/v1/config/diff` endpoint. It authenticates, fetches the diff between two specified config paths (e.g., two template revisions or a template and a device), and parses the JSON output into a more readable, structured format. I've found it particularly useful for pre-deployment validation in our CI/CD pipeline for network changes.
```python
import requests
import json
import sys
def get_config_diff(controller_ip, username, password, path_a, path_b):
"""Fetches and formats config diff from Versa Director API."""
base_url = f"https://{controller_ip}"
auth_endpoint = f"{base_url}/api/auth/v1/login"
diff_endpoint = f"{base_url}/api/config/v1/config/diff"
# Authenticate and obtain session token
auth_payload = {"username": username, "password": password}
session = requests.Session()
auth_response = session.post(auth_endpoint, json=auth_payload, verify=False)
auth_response.raise_for_status()
# Prepare diff request payload
diff_payload = {
"path1": path_a,
"path2": path_b,
"format": "json"
}
diff_response = session.post(diff_endpoint, json=diff_payload, verify=False)
diff_response.raise_for_status()
diff_data = diff_response.json()
# Parse and print structured diff
if 'diffs' in diff_data and diff_data['diffs']:
print(f"Diff between {path_a} and {path_b}:")
for entry in diff_data['diffs']:
print(f" Path: {entry.get('path', 'N/A')}")
print(f" Change Type: {entry.get('change-type', 'N/A')}")
if entry.get('change-type') == 'modified':
print(f" Old Value: {entry.get('old-value', 'N/A')}")
print(f" New Value: {entry.get('new-value', 'N/A')}")
print("-" * 40)
else:
print("No differences found.")
session.close()
# Example usage
if __name__ == "__main__":
# Replace with your controller details and config paths
get_config_diff(
"192.168.1.10",
"api-user",
"secure-password",
"/templates/branch-template/1",
"/devices/branch-nyc-01/running-config"
)
```
Key observations from my testing:
* The API response time for diffs averages around 1.2 seconds for moderately complex configurations (~5000 lines).
* The `change-type` field reliably identifies additions, deletions, and modifications.
* For large-scale comparisons, I recommend implementing pagination or asynchronous requests, as the payload can become substantial.
I'm curious if others have built similar tooling. Specifically:
* Have you encountered any limitations with the diff endpoint's depth or accuracy?
* What patterns are you using to integrate these diffs into larger data pipelines (e.g., streaming to Kafka for audit trails)?
* Any recommendations for handling hierarchical configs where a single change might cascade through multiple reported diffs?