-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy_configs.py
166 lines (124 loc) · 6.2 KB
/
deploy_configs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Cisco DNA Center Jinja2 Configuration Templates
Copyright (c) 2020 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
__author__ = "Gabriel Zapodeanu TME, ENB"
__email__ = "gzapodea@cisco.com"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2020 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
import datetime
import time
import json
import csv
import urllib3
from requests.auth import HTTPBasicAuth # for Basic Auth
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
import dnac_apis
from config import DNAC_PASS, DNAC_USER
from config import DEPLOY_PROJECT, DEPLOY_TEMPLATE, DEVICE_TYPES
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
DNAC_AUTH = HTTPBasicAuth(DNAC_USER, DNAC_PASS)
def pprint(json_data):
"""
Pretty print JSON formatted data
:param json_data:
:return:
"""
print(json.dumps(json_data, indent=4, separators=(' , ', ' : ')))
def main():
"""
This script will deploy a config file to a number of devices based on device family.
The device family is defined by a list "DEVICE_TYPES"
It will collect all the devices that match the device types, identify those that are reachable, and those that are
not reachable.
The script will deploy the configuration template to each reachable device.
There are some optional commands included that will allow to test the template deployment to a small number of
devices first.
"""
# the local date and time when the code will start execution
date_time = str(datetime.datetime.now().replace(microsecond=0))
print('\n\nApplication "deploy_configs.py" Run Started: ' + date_time)
# get a Cisco DNA Center auth token
dnac_auth = dnac_apis.get_dnac_jwt_token(DNAC_AUTH)
# verify if existing template in the project
template_id = dnac_apis.get_template_id(DEPLOY_TEMPLATE, DEPLOY_PROJECT, dnac_auth)
print('\nThe template "' + DEPLOY_TEMPLATE + '" id is: ', template_id)
# find all devices managed by Cisco DNA C, that are "switches and hubs"
device_list = dnac_apis.get_all_device_list(500, dnac_auth)
# create the switches list
switch_list_reachable = []
switch_list_unreachable = []
# identify all devices that match the device type
for device in device_list:
device_type = device['type']
if device_type in DEVICE_TYPES:
hostname = device['hostname']
if device['reachabilityStatus'] == 'Reachable':
switch_list_reachable.append(hostname)
else:
switch_list_unreachable.append(hostname)
print('\nThe unreachable devices to which the template will not be deployed are:', switch_list_unreachable, '\n')
print('\nThe devices to which the template will be deployed are:', switch_list_reachable)
total_number_devices = len(switch_list_reachable)
print('\nThe number of devices to deploy the template to is: ', total_number_devices)
# we will configure a number of devices equal with "device_count" starting with the device from the list
# identified with "first_record"
first_record = int(input('\nWhat is the device index you want to start with ? (integer between 0 and ' + str(
total_number_devices) + ') '))
device_count = int(input('How many devices do you want to configure ? '))
if device_count + first_record >= total_number_devices:
device_count = total_number_devices - first_record
print('Changed the number of the devices to maximum allowed: ', device_count)
device_index = first_record
# create a list with the structure [[device hostname, deployment status},...]
deployment_report = []
for switch in switch_list_reachable[first_record:first_record+device_count]:
# deploy the template
# get a Cisco DNA Center auth token, required for mass device configs, script running will take longer than
# 60 min.
dnac_auth = dnac_apis.get_dnac_jwt_token(DNAC_AUTH)
deployment_id = dnac_apis.send_deploy_template_no_params(DEPLOY_TEMPLATE, DEPLOY_PROJECT, switch, dnac_auth)
print('\nTemplate "' + DEPLOY_TEMPLATE + '" started, task id: "' + deployment_id + '"')
time.sleep(5) # wait for the deployment task to be created
deployment_status = dnac_apis.check_template_deployment_status(deployment_id, dnac_auth)
print('Deployment task result for switch: ', switch, ' is: ', deployment_status, ', device index: ',
device_index)
device_index += 1
deployment_report.append([switch, deployment_id, deployment_status])
# optional for manual deployment to test
# value = input('Input y/n to continue ')
# if value == 'n':
# break
print('\nThe deployment report:\n')
for item in deployment_report:
print(item)
date_time = str(datetime.datetime.now().replace(microsecond=0))
# save information to file
file_name = 'deployment_report-' + str(date_time) + '.csv'
file_name = file_name.replace(' ', '-')
output_file = open(file_name, 'w', newline='')
output_writer = csv.writer(output_file)
# loop through all devices and deployment status to collect the information needed in the report
for device in deployment_report:
device_info = [device[0], device[1], device[2]]
output_writer.writerow(device_info)
output_file.close()
print('\n\nFile ' + file_name + ' saved')
date_time = str(datetime.datetime.now().replace(microsecond=0))
print('\n\nEnd of Application "deploy_configs.py" Run: ' + date_time)
return
if __name__ == "__main__":
main()