Last active
November 13, 2023 16:37
-
-
Save titom73/c0d3c9fc77e4b44a2bab7ab47eed8286 to your computer and use it in GitHub Desktop.
Anta Hackathon Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test functions related to system-level features and protocols | |
""" | |
## Please refer to https://github.com/titom73/atd-anta-demo/blob/main/docs/demo-tests.md for complete description | |
from typing import Optional | |
from anta.models import AntaTest, AntaCommand | |
class VerifyLagState(AntaTest): | |
name = "VerifyLagState" | |
description = "Check LAG are running LACP and are in Collecting and Distributing" | |
categories = ["custom_system"] | |
commands = [AntaCommand(command="show lacp interface")] | |
class Input(AntaTest.Input): | |
ifd: str | |
@AntaTest.anta_test | |
def test(self) -> None: | |
ifd = self.inputs.ifd | |
command_output = self.instance_commands[0].json_output | |
self.result.is_success() | |
# Test logic | |
ifd_found = False | |
for po, po_data in command_output['portChannels'].items(): | |
if ifd in po_data['interfaces'].keys(): | |
ifd_found = True | |
data = po_data['interfaces'][ifd] | |
if not data['actorPortState']['collecting'] or not data['actorPortState']['distributing']: | |
self.result.is_failure(f'Interface {ifd} is not in correct state: {data["actorPortState"]}') | |
if not ifd_found: | |
self.result.is_failure(f'interface {ifd} was not found') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional, List | |
from pydantic import BaseModel | |
from anta.models import AntaTest, AntaCommand, AntaTemplate | |
class VerifyRouteSummary(AntaTest): | |
name = "VerifyRouteSummary" | |
description = "Check number of static routes in a given VRF" | |
categories = ["custom_system"] | |
commands = [AntaTemplate(template="show ip route vrf {vrf} summary")] | |
class Input(AntaTest.Input): | |
class Vrf(BaseModel): # BaseModel is a pydantic model | |
vrf: str | |
connected: int | |
vrfs: List[Vrf] | |
def render(self, template: AntaTemplate) -> list[AntaCommand]: | |
return [template.render(vrf=vrf.vrf) for vrf in self.inputs.vrfs] | |
@AntaTest.anta_test | |
def test(self) -> None: | |
vrfs = self.inputs.vrfs | |
command_output = self.instance_commands[0].json_output | |
self.result.is_success() | |
for vrf in self.inputs.vrfs: | |
if command_output['vrfs'][vrf.vrf]['connected'] != vrf.connected: | |
self.result.is_error(f'Device has an incorrect number of connected routes: expected: {vrf.connected} / configured: {command_output["vrfs"][vrf.vrf]["connected"]}') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
anta_inventory: | |
hosts: | |
- host: 192.168.0.10 | |
name: s1-spine1 | |
tags: ['spine'] | |
- host: 192.168.0.11 | |
name: s1-spine2 | |
tags: ['spine'] | |
- host: 192.168.0.12 | |
name: s1-leaf1 | |
tags: ['leaf'] | |
- host: 192.168.0.13 | |
name: s1-leaf2 | |
tags: ['leaf'] | |
- host: 192.168.0.14 | |
name: s1-leaf3 | |
tags: ['leaf'] | |
- host: 192.168.0.15 | |
name: s1-leaf4 | |
tags: ['leaf'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
anta.tests.interfaces: | |
- VerifyInterfaceUtilization: | |
- VerifyInterfaceErrors: | |
- VerifyInterfaceDiscards: | |
- VerifyInterfaceErrDisabled: | |
- VerifyInterfacesStatus: | |
interfaces: | |
- interface: Port-Channel4 | |
state: up | |
protocol_status: up | |
- interface: Ethernet4 | |
state: up | |
protocol_status: up | |
# filters: | |
# tags: ['leaf'] | |
# anta_custom.dummy: | |
# - VerifyLagState: | |
# ifd: Ethernet4 | |
# - VerifyLagState: | |
# ifd: Ethernet1 | |
# - VerifyRouteSummary: | |
# vrfs: | |
# - vrf: Tenant_A_OP_Zone | |
# connected: 3 | |
# filters: | |
# tags: ['leaf'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Known issue in setup
.envrc
:export ANTA_USERNAME=ansible
instead of arista