Genie Libs Ops

26.3 · active · verified Thu Apr 16

Genie Libs Ops (Operational State) is a core sub-component of the Cisco pyATS and Genie framework, current version 26.3. It provides a robust, network OS-agnostic abstraction layer to retrieve and model the operational state of network devices. This library is crucial for network test automation, offering structured data representation of device operational states (e.g., BGP, OSPF, interfaces) that can be learned, compared, and validated. Releases are frequent, typically monthly or bi-monthly, aligning with the broader pyATS/Genie ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `genie.libs.ops` to load a testbed, instantiate an operational state object (e.g., BGP for IOS-XE), learn the device's operational state, and access the structured data. A `testbed.yaml` file is expected for device connection details. For actual execution, ensure device connectivity or mock the device behavior.

import os
from pyats.topology import loader
from genie.libs.ops.bgp.iosxe.bgp import Bgp # Example: import BGP ops for IOS-XE
import pprint

# Ensure a testbed.yaml exists or create a dummy one for demonstration
# In a real scenario, this would connect to a live device.
# For local testing, mock the connection or use a local testbed file.
# Example testbed.yaml content (place in same directory as script):
# devices:
#   Router1:
#     os: iosxe
#     type: router
#     connections:
#       cli:
#         protocol: ssh
#         ip: 10.1.1.1 # Replace with actual device IP
#         port: 22
#     credentials:
#       default:
#         username: cisco
#         password: cisco

try:
    testbed = loader.load(os.environ.get('TESTBED_FILE', 'testbed.yaml'))
    device = testbed.devices.get('Router1')
    if not device:
        raise ValueError("Device 'Router1' not found in testbed.yaml")

    # In a real scenario, device.connect() would be called here:
    # device.connect(log_stdout=False)

    # Instantiate the Ops object for BGP on the device
    bgp_ops = Bgp(device=device)

    # Learn the operational state of BGP
    # This would execute 'show' commands on the device and parse output
    # For this example, it will run against a mocked device or fail if no connection
    bgp_ops.learn()

    print(f"\nLearned BGP operational state for {device.name}:")
    pprint.pprint(bgp_ops.info)

    # Example: Check if BGP is configured in VRF default
    if 'vrf' in bgp_ops.info and 'default' in bgp_ops.info['vrf']:
        print("\nBGP is configured in VRF 'default'.")
    else:
        print("\nBGP not found in VRF 'default' or not configured.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("\nEnsure 'testbed.yaml' is correctly configured and device is reachable, or mock device interaction for testing.")

view raw JSON →