Arista Network Test Automation (ANTA) Framework
raw JSON → 1.8.0 verified Fri Apr 17 auth: no python
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.
pip install anta anta-lib-tests Common errors
error 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.
fix
Update your imports to
from anta.models import .... error ModuleNotFoundError: No module named 'anta.tests.connectivity' ↓
cause The `anta-lib-tests` package, which contains common built-in tests like `VerifyReachability`, is not installed.
fix
Install the
anta-lib-tests package: pip install anta-lib-tests. error 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.
fix
Ensure your test execution loop calls
anta_test.test(device=device, result_manager=result_manager). error 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.
fix
For 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. ↓
fix Review the official ANTA v1.0.0 migration guide and update import paths, inventory formats, and test definitions. Specifically, `AntaTest` and `AntaDevice` are now imported from `anta.models`.
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`. ↓
fix Ensure you install both `anta` and `anta-lib-tests`: `pip install anta anta-lib-tests`. If you omit `anta-lib-tests`, you will encounter `ModuleNotFoundError` when trying to import common 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. ↓
fix For programmatic execution, directly instantiate `AntaInventory`, `ResultManager`, and `AntaTest` objects. Then, iterate through your inventory, connect to devices, execute `anta_test.test(device=device, result_manager=result_manager)`, and collect results from the `ResultManager`.
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). ↓
fix Consult the latest ANTA documentation for the correct inventory YAML structure. Ensure connection parameters like `protocol` (`https_vfy` vs `https`) are correctly specified.
Imports
- AntaInventory
from anta.inventory import AntaInventory - AntaTest wrong
from anta.anta_models import AntaTestcorrectfrom anta.models import AntaTest - ResultManager
from anta.result_manager import ResultManager - VerifyReachability
from anta.tests.connectivity import VerifyReachability - main (CLI runner) wrong
import anta.runnercorrectfrom 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}")