netops.playbooks — Ansible Playbook Generation¶
Composable Ansible remediation playbook generation from health-check reports.
netops.playbooks.generator¶
Generate Ansible remediation playbooks from structured health-check failure data.
CLI usage:
python -m netops.playbooks.generator --report health-report.json --output remediation.yml
python -m netops.playbooks.generator --report health-report.json --vendor cisco_ios
generator ¶
Playbook generator — auto-generate Ansible remediation playbooks from health check failures.
Reads a health check report (produced by :func:netops.check.health.run_health_check
or :func:netops.check.health.build_health_report) and generates valid Ansible
YAML playbooks for each device with active alerts.
Generated playbooks:
- Use vendor-specific Ansible collection modules (e.g.
cisco.ios.ios_command) - Include pre/post validation tasks that capture device state before and after remediation for comparison
- Wrap each remediation in a
block/rescuestructure so that therescuesection runs the rollback tasks on failure - Default to dry-run mode (
dry_run: "true"variable) — remediation tasks are guarded bywhen: not dry_run | boolso the playbook is safe to inspect in CI before live execution - Prompt for human review before executing remediation (unless
--no-pauseis passed to the CLI)
Usage::
# Generate playbooks from a saved health report (dry-run, print to stdout)
python -m netops.playbooks.generator generate \\
--from-health-report health_report.json
# Write playbooks to a directory (one file per device with failures)
python -m netops.playbooks.generator generate \\
--from-health-report health_report.json \\
--output-dir ./remediation-playbooks/
# Override vendor when the report does not carry device_type
python -m netops.playbooks.generator generate \\
--from-health-report health_report.json \\
--vendor cisco_ios_xr
# Mark playbook ready for live execution (dry_run=false in the vars block):
python -m netops.playbooks.generator generate \\
--from-health-report health_report.json \\
--live
Public API::
from netops.playbooks.generator import (
FailureType,
GeneratedPlaybook,
extract_failures,
generate_playbook,
generate_playbooks_from_report,
)
Classes¶
FailureType ¶
Bases: str, Enum
Health-check failure categories that can be remediated.
GeneratedPlaybook
dataclass
¶
GeneratedPlaybook(playbook_id: str, host: str, vendor: str, failure_types: list[FailureType], description: str, plays: list[dict], dry_run: bool = True, created_at: str = '', source_report_timestamp: str = '')
A complete Ansible playbook generated from health-check failures.
Attributes:
| Name | Type | Description |
|---|---|---|
playbook_id |
str
|
UUID string for correlation with health report entries. |
host |
str
|
Ansible inventory hostname / IP targeted by the playbook. |
vendor |
str
|
Device type string (e.g. |
failure_types |
list[FailureType]
|
List of :class: |
description |
str
|
Human-readable summary used in the top-level play name. |
plays |
list[dict]
|
List of Ansible play dicts ready for serialisation to YAML. |
dry_run |
bool
|
When |
created_at |
str
|
ISO-8601 UTC timestamp of generation. |
source_report_timestamp |
str
|
Timestamp from the originating health check result. |
Functions:¶
extract_failures ¶
Extract alerting checks from a single device health-check result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
health_result
|
dict
|
A result dict as returned by :func: |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[FailureType, dict]]
|
Ordered list of |
generate_playbook ¶
generate_playbook(health_result: dict, vendor: str | None = None, dry_run: bool = True, include_pause: bool = True) -> GeneratedPlaybook | None
Generate a remediation playbook from a single device health-check result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
health_result
|
dict
|
A result dict as returned by :func: |
required |
vendor
|
str | None
|
Override the device vendor string. When |
None
|
dry_run
|
bool
|
When |
True
|
include_pause
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
GeneratedPlaybook | None
|
A :class: |
generate_playbooks_from_report ¶
generate_playbooks_from_report(health_report: dict, vendor: str | None = None, dry_run: bool = True, include_pause: bool = True, host_filter: str | None = None) -> list[GeneratedPlaybook]
Generate remediation playbooks from an aggregated health report.
Accepts the dict returned by :func:netops.check.health.build_health_report
(which contains a "results" list) or a bare list of per-device
health results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
health_report
|
dict
|
Dict with a |
required |
vendor
|
str | None
|
Global vendor override applied to all devices. Per-device
|
None
|
dry_run
|
bool
|
Passed to :func: |
True
|
include_pause
|
bool
|
Passed to :func: |
True
|
host_filter
|
str | None
|
When given, only generate a playbook for the device whose |
None
|
Returns:
| Type | Description |
|---|---|
list[GeneratedPlaybook]
|
One playbook per device that has at least one active alert. |
netops.playbooks.templates.remediation¶
Built-in remediation templates for common network failure types.
Provides REMEDIATION_TEMPLATES — a dict mapping FailureType to
RemediationTemplate — and helper functions for looking up and rendering
templates.
remediation ¶
Vendor-specific remediation templates for playbook generation.
Each :class:RemediationTemplate encapsulates the commands needed to:
- Pre-validate the device state before remediation
- Remediate the failure condition
- Post-validate that the remediation succeeded
- Rollback (undo) the remediation when possible
Vendor command modules are mapped by the VENDOR_COMMAND_MODULE and
VENDOR_CONFIG_MODULE dicts so that the generator can pick the correct
Ansible collection for each platform.
Public API::
from netops.playbooks.templates.remediation import (
RemediationTemplate,
REMEDIATION_TEMPLATES,
VENDOR_COMMAND_MODULE,
VENDOR_CONFIG_MODULE,
get_template,
)
Classes¶
RemediationTemplate
dataclass
¶
RemediationTemplate(failure_type: str, description: str, pre_commands: dict[str, list[str]] = dict(), remediation_commands: dict[str, list[str]] = dict(), post_commands: dict[str, list[str]] = dict(), rollback_commands: dict[str, list[str]] = dict(), rollback_note: str = '')
Vendor-specific command sets for a single remediation action.
Attributes:
| Name | Type | Description |
|---|---|---|
failure_type |
str
|
The :class: |
description |
str
|
Human-readable description shown in generated playbook task names. |
pre_commands |
dict[str, list[str]]
|
Dict mapping vendor |
remediation_commands |
dict[str, list[str]]
|
Dict mapping vendor to remediation commands. |
post_commands |
dict[str, list[str]]
|
Dict mapping vendor to post-validation commands (same shape as pre). |
rollback_commands |
dict[str, list[str]]
|
Dict mapping vendor to rollback/undo commands. Empty when the action cannot be rolled back (e.g. counter clearing). |
rollback_note |
str
|
Human-readable note explaining rollback behaviour or limitations. |
Functions:¶
get_template ¶
Return the :class:RemediationTemplate for failure_type, or None.