• Pushing a Python Script to Devices Using CVP (Configlet)

 
 
Print Friendly, PDF & Email

Introduction

This method is useful for pushing simple scripts to managed devices in CVP. This is not suitable for long scripts or scripts with dependencies. It may work, but creating extensions is a far more structured approach which I talk about here.

 

Creating the Configlet

We will use an event-handler to assist us in creating the script on the switch due to its ability to execute multiple line bash commands. We are not able to use bash commands natively in  CVP configlets. To start the simple method, create the configlet based on the below example from Provisionsing>Configlets.

event-handler hello
trigger on-startup-config
action bash
cd /mnt/flash/
printf "%s\n" $'#!/usr/bin/env python' >> hello.py
printf "%s\n" $'print(\'Hello World\')' >> hello.py
chmod 755 hello.py
FastCli -p 15 -c "config
no event-handler hello"
EOF

I am using a short Hello World script for simplicity. However, I have tested this method with a 250 line script and it executed correctly. The event-handler will work as follows:it will be triggered immediately due to the on-startup-config trigger, drop into bash, execute the commands in order, and then finally delete itself so that it won’t run again.

Note: /mnt/flash is a persistent directory that can survive reboots, be careful to escape characters as needed for the printf string, and be sure to make the file executable. I also recommend testing this on a single device before pushing it to multiples to ensure syntax is correct.

Now assign the configlet to a your device in Provisioning>Network Provisioning by right-clicking the device and selecting Manage>Configlet. Then, select the newly created configlet and save the changes. This will generate a task that we can execute with a change control.

Before executing the change control, let’s show that hello.py does not exist in flash.

Arista#show clock
Tue Jan 26 10:07:11 2021
Timezone: America/Denver
Clock source: local

Arista#dir flash:
Directory of flash:/

-rw- 4620 Jan 20 16:38 AsuFastPktTransmit.log
drwx 4096 Jan 12 20:07 Fossil
-rw- 2840 Jan 20 16:38 SsuRestore.log
-rw- 2840 Jan 20 16:38 SsuRestoreLegacy.log
drwx 4096 Jan 20 16:38 archive
drwx 4096 Jan 12 10:57 debug
drwx 4096 Jan 12 10:57 fastpkttx.backup
-rw- 180 Jan 20 16:38 kickstart-config
drwx 4096 Jan 26 10:06 persist
drwx 4096 Jan 12 10:58 schedule
-rw- 3486 Jan 20 13:34 startup-config
-rw- 13 Nov 30 2020 zerotouch-config

514982985728 bytes total (466870452224 bytes free)

 

Executing the Configlet and Verification

Now we are ready to execute the change control. We can see that this is new configuration from the Review and Approve step of change control. We can now execute the change.

We can see from the switch logs below that the change was successful and triggered the event-handler. We can also see that it successfully deleted the event-handler from the last 2 lines in the log.

Arista#show logging
<removed>
Jan 26 10:07:28 Arista ConfigAgent: %SYS-5-CONFIG_SESSION_ENTERED: User cvpadmin entered configuration session CVP-configtask-8721f0a1-6c0b-4ffc-8d89-99bac8617119 on command-api (192.168.56.200)
Jan 26 10:07:41 Arista ConfigAgent: %SYS-5-CONFIG_SESSION_EXITED: User cvpadmin exited configuration session CVP-configtask-8721f0a1-6c0b-4ffc-8d89-99bac8617119 on command-api (192.168.56.200)
Jan 26 10:07:42 Arista ConfigAgent: %SYS-5-CONFIG_SESSION_COMMIT_SUCCESS: User cvpadmin committed configuration session CVP-configtask-8721f0a1-6c0b-4ffc-8d89-99bac8617119 successfully on command-api (192.168.56.200)
Jan 26 10:07:43 Arista ConfigAgent: %SYS-5-CONFIG_STARTUP: Startup config saved from system:/running-config by cvpadmin on command-api (192.168.56.200).
Jan 26 10:08:02 Arista EventMgr: %SYS-6-EVENT_TRIGGERED: Event handler hello was activated
Jan 26 10:08:04 Arista ConfigAgent: %SYS-5-CONFIG_E: Enter configuration mode from console by root on UnknownTty (UnknownIpAddr)
Jan 26 10:08:04 Arista ConfigAgent: %SYS-5-CONFIG_I: Configured from console by root on UnknownTty (UnknownIpAddr)

Upon checking flash, we see that hello.py has been created and is executable. We also can confirm that the event-handler has been deleted.

Arista#show clock
Tue Jan 26 10:08:32 2021
Timezone: America/Denver
Clock source: local

Arista#dir flash:
Directory of flash:/

-rw- 4620 Jan 20 16:38 AsuFastPktTransmit.log
drwx 4096 Jan 12 20:07 Fossil
-rw- 2840 Jan 20 16:38 SsuRestore.log
-rw- 2840 Jan 20 16:38 SsuRestoreLegacy.log
drwx 4096 Jan 20 16:38 archive
-rw- 0 Jan 26 10:07 boot-config
drwx 4096 Jan 12 10:57 debug
drwx 4096 Jan 12 10:57 fastpkttx.backup
-rwx 43 Jan 26 10:08 hello.py
-rw- 180 Jan 20 16:38 kickstart-config
drwx 4096 Jan 26 10:06 persist
drwx 4096 Jan 12 10:58 schedule
-rw- 3772 Jan 26 10:07 startup-config
-rw- 13 Nov 30 2020 zerotouch-config

514982985728 bytes total (466870390784 bytes free)

Arista#show event-handler hello
% Invalid event-handler name entered

 

Let’s test the script to be sure it works and check the code.

Arista#bash /mnt/flash/hello.py
Hello World

Arista#bash cat /mnt/flash/hello.py
#!/usr/bin/env python
print('Hello World')

 

Removing the Used Configlet

CVP will now show our device is out of sync because it expects to see the event-handler in the config. We can now remove the configlet from our device to make CVP happy.

Right-click the device from network provisioning and select Manage>Configlet. Click the X to remove the configlet and hit Validate.

 

This will generate a designed config with “00” differences which will only update CVP without affecting the switch. Once we verify that the configuration has been removed from the Designed Configuration, we can save the change.

 

CVP is now be back to its original state and the switch has the desired python script. CVP does not monitor the files in flash:, so we won’t need to worry about any compliance checks due to the newly added script.

From here you can use the script as needed whether that is in an event handler, alias, schedule etc.

Remember: This method is really only useful for shorter scripts or those that don’t have dependencies. Otherwise, we would want to push the script though CVP in a SWIX which I discuss in another post here.

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: