Genie Libs Ops
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
-
ImportError: cannot import name 'Csr1000vPatterns'
cause This error typically occurs after an upgrade of pyATS/Genie, indicating that internal plugin structures or symbols have changed, and cached or older modules are being used.fixPerform a clean re-installation: `pip uninstall -y pyats genie genie.libs.ops` followed by `pip install --no-cache-dir genie` (or `pyats[full]`). Ensure you are in a clean virtual environment. -
genie.metaparser.util.exceptions.SchemaMissingKeyError: Show Command: show spanning-tree
cause This error means the parsed output from the device did not contain an expected key or structure defined in the Genie parser's schema for that specific command. This can happen due to variations in device OS versions, unexpected output, or bugs in the parser itself.fixVerify the exact output of the `show` command on the device. Check the Genie Models documentation if there's an updated parser or if your device's output deviates significantly. You may need to create a custom parser or report the issue. -
Exception: Could not find parser for 'show interface status' or Search for 'show ip int br' is ambiguous.
cause Genie's parsing mechanism could not find a matching parser for the provided command string or found multiple ambiguous matches. This is common with shorthand commands or commands not explicitly supported by a parser.fixUse the full, precise command string as documented in the Genie Parsers list. Avoid common CLI shorthands if they are not explicitly handled by the Genie parser.
Warnings
- breaking Python 3.6 support officially ended after April 2022. Users on older Python versions (3.6 and below) must upgrade to Python 3.7+ (preferably 3.8+) to use recent versions of Genie Libs Ops and the broader pyATS/Genie framework.
- gotcha While `pip install genie-libs-ops` is possible, it is highly recommended to install the full `genie` or `pyats[full]` package. `genie.libs.ops` relies heavily on other components of the `genie` and `pyats` ecosystem (e.g., connection handlers, parsers). Installing only `genie-libs-ops` can lead to missing functionalities and runtime errors if dependencies are not met.
- gotcha When using `device.parse()` or `genie learn` for network commands, shorthand or non-standard command variations might not be recognized by built-in parsers, resulting in 'Could not find parser' or 'ambiguous' errors.
- deprecated Older versions of Genie (pre-19.9) had issues with multi-threaded execution, particularly when integrated with frameworks like Nornir using multiple workers. This could lead to parsing or operational state learning failures.
Install
-
pip install genie-libs-ops -
pip install genie -
pip install pyats[full]
Imports
- Bgp
from genie.libs.ops.bgp.iosxe.bgp import Bgp
- loader
from pyats.topology import loader
Quickstart
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.")