| 1 | --- |
| 2 | title: ANTA as a Python Library |
| 3 | hide: |
| 4 | - tags |
| 5 | tags: |
| 6 | - Python |
| 7 | - API |
| 8 | --- |
| 9 | |
| 10 | <!-- |
| 11 | ~ Copyright (c) 2023-2026 Arista Networks, Inc. |
| 12 | ~ Use of this source code is governed by the Apache License 2.0 |
| 13 | ~ that can be found in the LICENSE file. |
| 14 | --> |
| 15 | |
| 16 | ANTA is a Python library that can be used in user applications. This section describes how you can leverage ANTA Python modules to help you create your own NRFU solution. |
| 17 | |
| 18 | !!! tip |
| 19 | If you are unfamiliar with asyncio, refer to the Python documentation relevant to your Python version - <https://docs.python.org/3/library/asyncio.html> |
| 20 | |
| 21 | ## [AntaDevice](../api/device.md#anta.device.AntaDevice) Abstract Class |
| 22 | |
| 23 | A device is represented in ANTA as an instance of a subclass of the [AntaDevice](../api/device.md#anta.device.AntaDevice) abstract class. |
| 24 | There are a few abstract methods that need to be implemented by child classes: |
| 25 | |
| 26 | - The [collect()](../api/device.md#anta.device.AntaDevice.collect) coroutine is in charge of collecting outputs of [AntaCommand](../api/commands.md#anta.models.AntaCommand) instances. |
| 27 | - The [refresh()](../api/device.md#anta.device.AntaDevice.refresh) coroutine is in charge of updating attributes of the [AntaDevice](../api/device.md#anta.device.AntaDevice) instance. These attributes are used by [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) to filter out unreachable devices or by [AntaTest](../api/tests/anta_test.md#anta.models.AntaTest) to skip devices based on their hardware models. |
| 28 | |
| 29 | The [copy()](../api/device.md#anta.device.AntaDevice.copy) coroutine is used to copy files to and from the device. It does not need to be implemented if tests are not using it. |
| 30 | |
| 31 | ### [AsyncEOSDevice](../api/device.md#anta.device.AsyncEOSDevice) Class |
| 32 | |
| 33 | The [AsyncEOSDevice](../api/device.md#anta.device.AsyncEOSDevice) class is an implementation of [AntaDevice](../api/device.md#anta.device.AntaDevice) for Arista EOS. |
| 34 | It uses `asynceapi` for eAPI and the [AsyncSSH](https://github.com/ronf/asyncssh) library for SCP. `asynceapi` is ANTA's fork and packaged client derived from [aio-eapi](https://github.com/jeremyschulman/aio-eapi). |
| 35 | |
| 36 | - The [\_collect()](../api/device.md#anta.device.AsyncEOSDevice._collect) coroutine collects [AntaCommand](../api/commands.md#anta.models.AntaCommand) outputs using eAPI. |
| 37 | - The [refresh()](../api/device.md#anta.device.AsyncEOSDevice.refresh) coroutine tries to open a TCP connection on the eAPI port and update the `is_online` attribute accordingly. If the TCP connection succeeds, it sends a `show version` command to gather the hardware model of the device and updates the `established` and `hw_model` attributes. |
| 38 | - The [copy()](../api/device.md#anta.device.AsyncEOSDevice.copy) coroutine copies files to and from the device using the SCP protocol. |
| 39 | |
| 40 | ANTA uses the term `use_session_auth` as a device-agnostic concept: it signals that the device should maintain an authenticated session across multiple requests rather than re-authenticating on every call. [AsyncEOSDevice](../api/device.md#anta.device.AsyncEOSDevice) implements this via eAPI cookie-session authentication — when `use_session_auth` is set to `True` in the constructor (default: `False`), `asynceapi` establishes an HTTP session and reuses the session cookie across eAPI calls, reducing authentication overhead. |
| 41 | |
| 42 | ## [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) Class |
| 43 | |
| 44 | The [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) class is a subclass of the standard Python type [dict](https://docs.python.org/3/library/stdtypes.html#dict). The keys of this dictionary are the device names, the values are [AntaDevice](../api/device.md#anta.device.AntaDevice) instances. |
| 45 | |
| 46 | [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) provides methods to interact with the ANTA inventory: |
| 47 | |
| 48 | - The [add_device()](../api/inventory.md#anta.inventory.AntaInventory.add_device) method adds an [AntaDevice](../api/device.md#anta.device.AntaDevice) instance to the inventory. Adding an entry to [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) with a key different from the device name is not allowed. |
| 49 | - The [get_inventory()](../api/inventory.md#anta.inventory.AntaInventory.get_inventory) returns a new [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) instance with filtered out devices based on the method inputs. |
| 50 | - The [connect_inventory()](../api/inventory.md#anta.inventory.AntaInventory.connect_inventory) coroutine will execute the [refresh()](../api/device.md#anta.device.AntaDevice.refresh) coroutines of all the devices in the inventory. |
| 51 | - The [disconnect_inventory()](../api/inventory.md#anta.inventory.AntaInventory.disconnect_inventory) coroutine will execute the [disconnect()](../api/device.md#anta.device.AntaDevice.disconnect) coroutines of all the devices in the inventory. |
| 52 | - The [parse()](../api/inventory.md#anta.inventory.AntaInventory.parse) static method creates an [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) instance from a YAML file and returns it. The devices are [AsyncEOSDevice](../api/device.md#anta.device.AsyncEOSDevice) instances. |
| 53 | |
| 54 | !!! warning |
| 55 | When using ANTA as a Python library, inventory and device objects are owned by the caller. If you reuse the same [AntaInventory](../api/inventory.md#anta.inventory.AntaInventory) or [AntaDevice](../api/device.md#anta.device.AntaDevice) objects across multiple runs or runners, do not enable `disconnect=True` on a run that overlaps with other work using those objects. Instead, call [disconnect_inventory()](../api/inventory.md#anta.inventory.AntaInventory.disconnect_inventory) once all runs using that inventory are complete. The `disconnect=True` runner option is intended for cases where a single run owns the inventory lifecycle. |
| 56 | |
| 57 | ## Examples |
| 58 | |
| 59 | ### Parse an ANTA inventory file |
| 60 | |
| 61 | ```python |
| 62 | --8<-- "parse_anta_inventory_file.py" |
| 63 | ``` |
| 64 | |
| 65 | !!! note |
| 66 | **How to create your inventory file** |
| 67 | |
| 68 | Please visit this [dedicated section](../usage-inventory-catalog.md) for how to use inventory and catalog files. |
| 69 | |
| 70 | ### Run EOS commands |
| 71 | |
| 72 | ```python |
| 73 | --8<-- "run_eos_commands.py" |
| 74 | ``` |