7 months, 2 weeks

Automating Your Network Tests and Deployments with pyATS and Genie




 

While pyATS and Genie are technically two libraries that pyATS is responsible for device connectivity, test definition, and reporting, and Genie is responsible for high-level capabilities such as parsing command-line interface (CLI) output to structured Python data or defining test cases.


When developing software, most engineers go through four basic phases, outlined as follows:
1. The development stage is where the code is first designed and then written.
2. The testing stage is where the code that has been written is tested extensively.
3. The deployment stage is where the code that has been developed and tested is deployed to the servers.
4. The monitoring stage is where data such as access times, occurring errors, and load on the system of the new change is closely monitored. Any insights that have been identified in the monitoring stage (such as the need to optimize a certain portion of the code) are fed back into the next development stage, leading to an infinite cycle of developing, testing, deploying, and monitoring your software.

Going back to infrastructure engineering, we can identify a similar cycle, outlined as follows:
1. The design phase is where changes to our infrastructure are designed and then implemented in the form of a change of configuration.
2. The testing phase is where the changes are usually applied in a lab environment to troubleshoot and identify any potential issues.
3. The deployment phase is where, during a maintenance window, the different devices are being updated with the new configuration.
4. The monitoring phase is where issues that may have occurred are monitored.

Creating a pyATS testbed file
 pyATS relies on a text file, written in YAML Ain't Markup Language (YAML) and commonly referred to as a testbed, that specifies the different attributes such as device type, hostname, and username/password for our device.

Follow these steps to define a testbed for your devices to connect to:
1. Open your testbed.yaml file and define a devices list and a hostname for your device—in this case, device-1:
devices:
   device-1:
2. Next, define the device type (in this example, router) and the operating system  running on the device, as well as the connection credentials:
 type: router
 os: iosxe
 credentials:
   default:
     username: <insert your username>
     password: <insert your password>
3. Next, we need to define the connection details to our management interface.
connections:
  mgmt:
    protocol: ssh
    host: <insert your host here>
    port: <insert your port here>
4. With the testbed file created, we can go ahead and use the pyATS shell to connect to our device. Open your console in the same directory as the testbed file and type the following command:
pyats shell –testbed-file testbed.yaml
5. After the shell has loaded and you have an interactive Python shell, type the following command to connect your device and open a device shell within the Python session:
testbed.devices['device-1'].connect()
We specify the authentication details such as username and password, as well as the connection details to our management interface. pyATS supports SSH, RESTCONF (which stands for REpresentational State Transfer (REST) configuration), and NETCONF(which stands for network configuration).

Connecting to your device and issuing commands using pyATS
Follow these steps to connect to a device and issue a command using pyATS:
1. Open your testbed.yaml file and define a devices list and a hostname for your device—in this case, device-1:
devices:
 device-1:
2. Next, define the device type (in this example, router) and the operating system (in this example, iosxe) running on the device, as well as the connection credentials:
 type: router
 os: iosxe
 credentials:
   default:
       username: <insert your username>
       password: <insert your password>
3. Next, we need to define the connection details to our management interface.
connections:
 mgmt:
   protocol: ssh
   host: <insert your host here>
   port: <insert your port here>
4. With the testbed file created, we can go ahead and open the connect.py file. We first need to import the required pyATS library to load the testbed file:
from pyats.topology import loader
5. Next, load the testbed file and retrieve the device by its name (in this example, device-1), and issue a command:
testbed = loader.load('testbed.yaml')
device = testbed.devices['device-1']
device.connect()

6. With the device connection established, we can issue our command and retrieve the output as unstructured data:
out = device.execute('show ip interface brief')
print(out)
Retrieving your device's current state using pyATS
Follow these steps to learn the current state of a network device automatically using pyATS: 
1. Open your testbed.yaml file and define a devices list and a hostname for 
your device—in this case, device-1:
devices:
 device-1:
2. Next, define the device type (in this example, router) and the operating system (in this example, iosxe) running on the device, as well as the connection credentials:
 type: router
 os: iosxe
 credentials:
   default:
     username: <insert your username>
     password: <insert your password>
3. Next, we need to define the connection details to our management interface.
connections:
 mgmt:
   protocol: ssh
   host: <insert your host here>
   port: <insert your port here>
4. With the testbed file created, we can go ahead and open the learn.py file. We first need to import the required pyATS library to load the testbed file. We will also import the built-in JavaScript Object Notation (JSON) library to save the output of our learning to a file:
from genie.testbed import load
import json
5. Next, load the testbed file and retrieve the device by its name (in this example, device-1), and issue a command:
testbed = load('testbed.yaml')
device = testbed.devices['device-1']
device.connect()
6. With our device connected, we can initiate the learning. 
output = device.learn('all')
7. Lastly, we'll have to write the data stored in our output variable to a file for us to later retrieve it:
with open("backup.json, "w") as fh:
  json.dump(output.to dict(), fh, indent=2)
The learn() function, depending on which model you are learning, will invoke multiple show commands but, more importantly, the learn() function also ensures a consistent set of keys. This means that we can write one script that works across different devices. Once we have retrieved this output, we can then go ahead and save it to a file using the JSON module. 

Using Genie Conf objects to create a portable configuration script
Follow these steps to change the configuration of an interface using pyATS:
1. Open your testbed.yaml file and define a devices list and a hostname for your device—in this case, device-1:
devices:
  device-1:
2. Next, define the device type (in this example, router) and the operating system running on the device, as well as the connection credentials:
 type: router
 os: iosxe
 credentials:
   default:
     username: <insert your username>
     password: <insert your password>

3. We define the connection details to our management interface. In this example, we are going to connect to the device via SSH. If you want to use an IP address instead of a host, use ip: <insert your ip here> instead of the host part shown here:
 connections:
   mgmt:
       protocol: ssh
       host: <insert your host here>
       port: <insert your port here>
4. With the testbed file created, we can go ahead and open the change.py file.
from pyats.topology import loader
from genie.conf.base import Interface
5. We can load our device and connect to it:
testbed = loader.load('testbed.yaml')
device = testbed.devices['device-1']
device.connect()
6. Now, we can use the previously imported Interface object to create a new interface configuration:
intf = Interface(device=device, name='GigabitEthernet2')
7. With this Interface object, we can now specify the properties we want:
intf.ipv4 = '10.10.10.2'
intf.ipv4.netmask = '255.255.255.0'
intf.shutdown = False
8. Finally, we can apply the configuration to our interface by issuing a build_config() command:
intf.build_config()

Comparing your device's current state to a previously learned state
Follow these steps to get the differences between a previously saved state and the current state of your device:
1. Open your testbed.yaml file and define a devices list and a hostname for your device—in this case, device-1:
devices:
 device-1:
2. Next, define the device type (in this example, router) and the operating system  running on the device, as well as the connection credentials:
 type: router
 os: iosxe
 credentials:
   default:
     username: <insert your username>
     password: <insert your password>
3. We need to define the connection details to our management interface.
connections:
 mgmt:
   protocol: ssh
   host: <insert your host here>
   port: <insert your port here>
4. With the testbed file created, we can go ahead and open the compare.py file. In it, we are going to first import the required libraries:
from genie.testbed import load
from genie.utils.diff import Diff
import json
5. Next, load the device from the testbed and connect to it:
testbed = load('testbed.yaml')
device = testbed.devices['device-1']
device.connect()
6. With the device loaded, we can learn the current state of this device:
current = device.learn('all')
7. After pyATS has learned the current state of the device, it's time to load the previously saved snapshot. In this example, we are assuming that the snapshot was saved to a file called backup.txt:
with open('backup.txt') as fh:
 snapshot = json.load(fh)

8. We can generate the difference between the previous state, retrieved from the loaded snapshot, and the current state of our device and print the difference back to the user:
diff = Diff(snapshot, current.to_dict())
diff.findDiff()
print(diff)


Responses(0)







Related