| 1 |
# Copyright (c) 2022 Arista Networks, Inc. |
| 2 |
# Use of this source code is governed by the Apache License 2.0 |
| 3 |
# that can be found in the COPYING file. |
| 4 |
|
| 5 |
# This script can be run as a cronjob to periodically reconcile all devices |
| 6 |
# that are out of configuration compliance in environments where the running-config |
| 7 |
# is still modified via the CLI often. |
| 8 |
from cvprac.cvp_client import CvpClient |
| 9 |
import ssl |
| 10 |
from datetime import datetime |
| 11 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 12 |
import requests.packages.urllib3 |
| 13 |
requests.packages.urllib3.disable_warnings() |
| 14 |
clnt = CvpClient() |
| 15 |
clnt.set_log_level(log_level='WARNING') |
| 16 |
|
| 17 |
# Reading the service account token from a file |
| 18 |
with open("token.tok") as f: |
| 19 |
token = f.read().strip('\n') |
| 20 |
|
| 21 |
clnt = CvpClient() |
| 22 |
clnt.connect(nodes=['cvp1'], username='',password='',api_token=token) |
| 23 |
|
| 24 |
inventory = clnt.api.get_inventory() |
| 25 |
|
| 26 |
compliance = {"0001": "Config is out of sync", |
| 27 |
"0003": "Config & image out of sync", |
| 28 |
"0004": "Config, Image and Device time are in sync", |
| 29 |
"0005": "Device is not reachable", |
| 30 |
"0008": "Config, Image and Extensions are out of sync", |
| 31 |
"0009": "Config and Extensions are out of sync", |
| 32 |
"0012": "Config, Image, Extension and Device time are out of sync", |
| 33 |
"0013": "Config, Image and Device time are out of sync", |
| 34 |
"0014": "Config, Extensions and Device time are out of sync", |
| 35 |
"0016": "Config and Device time are out of sync" |
| 36 |
} |
| 37 |
|
| 38 |
non_compliants = [] |
| 39 |
taskIds = [] |
| 40 |
for device in inventory: |
| 41 |
if device['complianceCode'] in compliance.keys(): |
| 42 |
# create a list of non-compliant devices for reporting purposes |
| 43 |
non_compliants.append(device['hostname']) |
| 44 |
dev_mac = device['systemMacAddress'] |
| 45 |
# check if device already has reconciled config and save the key if it does |
| 46 |
try: |
| 47 |
configlets = clnt.api.get_configlets_by_device_id(dev_mac) |
| 48 |
for configlet in configlets: |
| 49 |
if configlet['reconciled'] == True: |
| 50 |
configlet_key = configlet['key'] |
| 51 |
break |
| 52 |
else: |
| 53 |
configlet_key = "" |
| 54 |
rc = clnt.api.get_device_configuration(dev_mac) |
| 55 |
name = 'RECONCILE_' + device['serialNumber'] |
| 56 |
update = clnt.api.update_reconcile_configlet(dev_mac, rc, configlet_key, name, True) |
| 57 |
# if the device had no reconciled config, it means we need to append the reconciled |
| 58 |
# configlet to the list of applied configlets on the device |
| 59 |
if configlet_key == "": |
| 60 |
addcfg = clnt.api.apply_configlets_to_device("auto-reconciling",device,[update['data']]) |
| 61 |
clnt.api.cancel_task(addcfg['data']['taskIds'][0]) |
| 62 |
except Exception as e: |
| 63 |
continue |
| 64 |
print(f"The non compliant devices were: {str(non_compliants)}") |