Pyats Network Framework

using pyats to automate network test and configuration

pyats

pyATS is intended for test script developers and network engineers who want to get started with pyATS and the pyATS Library (previously "Genie").

pyATS currently supports Python v3.7.x, v3.8.x, v3.9.x and v3.10.x, and is tested to work on the following platforms:

Installation

pip install "pyats[full]"

Getting started with testbed.yml

To generate a testbed file for PyATS connectivity, you can convert an existing Ansible hosts file into a testbed file, or you can create a custom testbed file by leveraging an API to pull data from a CMDB or an external database.

Using Ansible - run.yml

---
 
- name: Create Pyats Testbed From Ansible Inventory
  hosts: "{{ _devices }}"
  gather_facts: no
  vars:
    _devices:
      - cisco 
 
  tasks:
  
  - name: Flatten Group Name To Device List.
    set_fact:
      _testbed: '{{ _devices | map("extract", groups) | flatten | list }}'
 
  - name: Template Ansible Inventory Devices To Testbed.yml
    template:
      src: testbed.j2
      dest: testbed.yml
 
  
tempaltes/testbed.j2
devices:
{% for host in _testbed %}
  {{ hostvars[host]['inventory_hostname'] }}:
    connections:
      cli:
        ip: {{ hostvars[host]['ansible_host'] }}
        protocol: ssh
    credentials:
      default:
        password: {{ hostvars[inventory_hostname]['ansible_ssh_pass'] }}
        username: {{ hostvars[inventory_hostname]['ansible_user'] }}
      enable:
        password: {{ hostvars[inventory_hostname]['ansible_password'] }}
    os: {{ hostvars[inventory_hostname]['ansible_network_os'] }}
    type: {{ hostvars[inventory_hostname]['ansible_network_os'] }}
{% endfor %}
get_bandwidth.py
from pyats.topology import loader
from genie.libs.parser.utils import get_parser_exclude
 
def main():
    # Load the testbed
    testbed = loader.load('testbed.yaml')
    
    # Connect to the router
    device = testbed.devices['r1']
    device.connect(log_stdout=False)
 
    # Execute the command to get interface information
    output = device.parse('show interfaces')
 
    # Assuming the internet link is on interface GigabitEthernet0/0
    interface = output['GigabitEthernet0/0']
 
    # Print the bandwidth
    print(f"Interface: {interface['interface']}")
    print(f"Bandwidth: {interface['bandwidth']} Kbps")
 
    # Disconnect from the device
    device.disconnect()
 
if __name__ == '__main__':
    main()

Shell mode

If you want to use Python, you can use pyats shell to load the testbed API and create your testbed and device objects. Then, tell the system to connect to each device and to learn the specified features. In this example, the system stores the output as a Python dictionary in the variable learnt and displays the output:

(pyats) $ pyats shell --testbed-file mock.yaml

reference

Cisco testautomation

Getting start pyats

learning devops labs

parsers lab

ios parser

Pyats Unicorn support platform

Pyats installation

examples folder Cisco

# Example testbed