Genie
Genie is the standard Python Library System built on top of the pyATS framework, providing a rich set of parsers (Genie Ops) and configuration builders (Genie Conf) for network devices, primarily Cisco platforms. It enables network engineers and developers to automate network operations, testing, and configuration management. The current version is 26.3, and it follows the pyATS release cadence, with major updates typically aligning with major pyATS releases.
Common errors
-
ImportError: cannot import name 'Testbed' from 'genie.testbed'
cause You are trying to import `Testbed` directly from `genie.testbed`, but in current pyATS/Genie versions, the `Testbed` class is part of the `pyats.topology` module.fixChange your import statement to `from pyats.topology import Testbed`. -
AttributeError: 'Device' object has no attribute 'parse'
cause The `parse` method is added to `pyats.topology.Device` objects by Genie. This error usually means Genie is not correctly installed, or the device's OS in the testbed is not recognized by Genie Ops, preventing the extension.fixEnsure `genie` is installed (`pip install genie`) and that your testbed device `os` attribute (e.g., `os: iosxe`) is correctly specified and supported by Genie. -
pyats.exceptions.SchemaError: Testbed schema validation failed:
cause The testbed YAML file contains syntax errors, is missing required fields, or uses an outdated schema that is no longer valid for your installed pyATS/Genie version.fixReview your testbed YAML against the official pyATS documentation. Use the `pyats parse testbed <your_testbed.yaml>` command to get detailed schema validation feedback and identify the exact line/issue.
Warnings
- breaking Genie is tightly coupled with pyATS. Mismatched versions between `genie` and `pyats` core packages are a frequent cause of `ImportError`, `AttributeError`, or unexpected behavior.
- gotcha Python version compatibility. Genie (and pyATS) strictly enforce minimum Python versions. Using an unsupported Python version will lead to installation failures or runtime errors.
- breaking Genie Ops (`device.parse()`) and Conf (`device.configure()`) output/input structures can change between major releases. This can break existing automation scripts that rely on specific keys or nested data structures.
- gotcha Testbed YAML schema can evolve. Using an outdated or malformed testbed YAML file with a newer version of pyATS/Genie can lead to schema validation errors.
Install
-
pip install genie
Imports
- Testbed
from genie.testbed import Testbed
from pyats.topology import Testbed
- device.parse
output = device.parse('show version') - device.configure
device.configure('interface Loopback0\n ip address 1.1.1.1 255.255.255.255')
Quickstart
import os
from pyats.topology import Testbed
# Create a dummy testbed YAML file for the example
testbed_content = """
devices:
router1:
os: iosxe
type: router
connections:
cli:
protocol: ssh
ip: 127.0.0.1
port: 22
credentials:
default:
username: {}
password: {}
""".format(os.environ.get('GENIE_DEVICE_USERNAME', 'cisco'),
os.environ.get('GENIE_DEVICE_PASSWORD', 'cisco'))
testbed_path = "temp_genie_testbed.yaml"
with open(testbed_path, "w") as f:
f.write(testbed_content)
try:
# Load the testbed using pyATS
testbed = Testbed(testbed_path)
print(f"Successfully loaded testbed from {testbed_path}")
# Access a device defined in the testbed
device = testbed.devices['router1']
print(f"Accessed device: {device.name}")
print(f"Device OS: {device.os}")
print(f"Device Type: {device.type}")
# To perform actual operations (like device.parse() or device.connect()),
# a live connection to a network device is required.
# Example (will fail without connection, for demonstration only):
# try:
# device.connect()
# output = device.parse('show version')
# print(f"Parsed 'show version': {output.keys()}")
# finally:
# if device.is_connected():
# device.disconnect()
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up the dummy testbed file
if os.path.exists(testbed_path):
os.remove(testbed_path)