Arista Network Test Automation (ANTA) Framework
The Arista Network Test Automation (ANTA) Framework is a Python library designed for automating network device testing, specifically targeting Arista EOS devices. It provides a structured way to define, execute, and report on various network tests, supporting both CLI and programmatic workflows. As of this entry, the current version is 1.8.0, with active development and regular feature and maintenance releases.
Common errors
-
ModuleNotFoundError: No module named 'anta.anta_models'
cause Attempting to import models from the old `anta.anta_models` path, which was deprecated in v1.0.0.fixUpdate your imports to `from anta.models import ...`. -
ModuleNotFoundError: No module named 'anta.tests.connectivity'
cause The `anta-lib-tests` package, which contains common built-in tests like `VerifyReachability`, is not installed.fixInstall the `anta-lib-tests` package: `pip install anta-lib-tests`. -
TypeError: AntaTest.test() missing 1 required positional argument: 'device'
cause When running tests programmatically, you must explicitly pass the `device` object and `result_manager` to the test's `test` method.fixEnsure your test execution loop calls `anta_test.test(device=device, result_manager=result_manager)`. -
Failed to establish connection to ...: SSLError('hostname '...' doesn't match '...'), (certificate verify failed: Hostname mismatch)cause The device's certificate hostname does not match the hostname used for connection, or a self-signed certificate is not trusted.fixFor lab environments, you can often disable certificate verification by setting `protocol: https` (without `_vfy`) in your inventory, or properly configure certificate trust. Always understand security implications before disabling verification.
Warnings
- breaking ANTA v1.0.0 introduced significant API changes and refactoring. Key modules like `anta.anta_models` were renamed to `anta.models`, and the overall test execution flow was redesigned.
- gotcha Built-in ANTA tests (e.g., `VerifyReachability`) are no longer part of the core `anta` package. They have been moved to a separate package, `anta-lib-tests`.
- gotcha ANTA's programmatic runner (`anta.runner.main`) is primarily designed for internal CLI use with `argparse`. Direct programmatic execution requires a more manual loop over inventory devices and tests.
- gotcha The inventory format, especially for device connection parameters, has evolved. Older YAML inventory files might require updates, particularly for protocol specification (`https_vfy` for verified HTTPS).
Install
-
pip install anta anta-lib-tests
Imports
- AntaInventory
from anta.inventory import AntaInventory
- AntaTest
from anta.anta_models import AntaTest
from anta.models import AntaTest
- ResultManager
from anta.result_manager import ResultManager
- VerifyReachability
from anta.tests.connectivity import VerifyReachability
- main (CLI runner)
import anta.runner
from anta.runner import main
Quickstart
import os
from anta.inventory import AntaInventory
from anta.models import AntaTest
from anta.tests.connectivity import VerifyReachability # Requires anta-lib-tests
from anta.result_manager import ResultManager
# 1. Define your inventory programmatically or load from a file
# This example uses a simple in-memory inventory for a single device.
# Replace '127.0.0.1' with your actual device's IP/hostname.
inventory_data = {
"lab": {
"hosts": {
"veos-01": {
"host": "127.0.0.1",
"port": 8443,
"username": os.environ.get('ANTA_USERNAME', 'admin'),
"password": os.environ.get('ANTA_PASSWORD', 'arista'),
"protocol": "https_vfy"
}
}
}
}
inventory = AntaInventory(inventory_data=inventory_data)
# 2. Define your tests using AntaTest objects
# This uses a built-in test from anta-lib-tests. Adjust 'hosts' for your network.
# Note: '1.1.1.1' and '2.2.2.2' are example unreachable hosts for a quick fail result.
# If you have real devices, replace with actual reachable/unreachable IPs.
tests_to_run = [
AntaTest(test=VerifyReachability, args={"hosts": ["1.1.1.1", "2.2.2.2"]})
]
# 3. Instantiate a ResultManager to collect test outcomes
result_manager = ResultManager()
# 4. Iterate over inventory devices and execute tests
print("--- Starting ANTA tests ---")
for device_name, device in inventory.get_devices().items():
print(f"\nRunning tests on device: {device_name} ({device.host})")
try:
device.connect() # Establish connection to the device
for anta_test in tests_to_run:
# Execute each test and record its result
print(f" Executing test: {anta_test.test.__name__}...")
anta_test.test(device=device, result_manager=result_manager) # Pass device and result_manager
device.close() # Close connection after tests
except Exception as e:
# Handle connection errors or general exceptions during test execution
print(f" ERROR during tests on {device_name}: {e}")
# Add a skipped/failed result for the first test if connection failed
if tests_to_run:
result_manager.add_failure(device=device, test=tests_to_run[0].test, result="SKIPPED", messages=[f"Could not connect or run tests: {e}"])
else:
print(f" No tests defined for {device_name}, but connection failed: {e}")
# 5. Print the aggregated test results
print("\n--- ANTA Test Results Summary ---")
for result in result_manager.get_results():
print(f"Device: {result.name}, Test: {result.test}, Result: {result.result}")
if result.messages:
for message in result.messages:
print(f" - {message}")