forked from arubatrainings1/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4.2_pyaoscx_workflow.py
244 lines (144 loc) · 5.41 KB
/
lab4.2_pyaoscx_workflow.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# coding: utf-8
# # Aruba Network Automation Product Training -Lab4- Task2
# There are two Approaches to use pyaoscx: open granulated approach and Imperative Factory Approach to utilize Pyaoscx library.
#
#
# On Access switch:
# - Utilize open granulated approach to create vlan200, add a description and put Interface into the VLAN. Create Lag1, add interface1/1/8 into the lag1.
# - Utilize Imperative Factory Approach to create VLAN201 and Lag2.
# - Chanlenge: understand how to handle configurarion. Save the running-config to checkpoint and tftp server.
#
# In[11]:
get_ipython().system('pip install pyaoscx ')
# In[8]:
#get to know the pyaoscx and where it is installed.
get_ipython().system('pip show pyaoscx')
# In[7]:
from pyaoscx.session import Session
from pyaoscx.pyaoscx_factory import PyaoscxFactory
from pyaoscx.vlan import Vlan
from pyaoscx.interface import Interface
from pyaoscx.static_route import StaticRoute
from pyaoscx.vrf import Vrf
import urllib3
urllib3.disable_warnings()
# In[10]:
# There are two approaches to workflows, both using the session.
version = '10.04'
switch_ip = '<yOUR switch IP'
s = Session(switch_ip, version)
s.open('<Your switch Username', 'Your switch password')
# # APPROACH 1: OPEN GRANULATED APPROACH VLAN
# In[ ]:
# Create Vlan object -- Not yet materialized
#Vlan is Python Class has been defined in PYTHON module pycentral.vlan
#vlan200 is a object or instance of this Class
vlan200 = Vlan(s, 200, name="VLAN 200", voice=True)
# In[ ]:
# Since object is not materialized, performs a POST request -- This method internally makes a GET request right after the POST
# Obtaining all attributes VLAN related
#If you an "Internal server error", that means the vlan is already exsisted.
vlan200.apply()
# In[ ]:
#display all Vlans
# In[ ]:
Vrf.get_all(s)
# In[ ]:
#add description for vlan200
vlan200.description = "New description, changed via pyaoscx SDK1"
vlan200.apply()
# Now vlan200 contains the description attribute
print("VLAN 200 description {}".format(vlan200.description))
# In[ ]:
# Now let's create another object, that we know already exists inside of the Switch
vlan1 = Vlan(s, 1)
# Perform a GET request to obtain all data and materialize object
vlan1.get()
# In[ ]:
# Now, we are able to modify the objects internal attributes
vlan1.voice = True
# Apply changes
changed = vlan1.apply()
# If changed is True, a PUT request was done and object was modified
# In[ ]:
# More complex example using the OPEN GRANULATED APPROACH
# Create an Interface object
lag = Interface(s, 'lag1')
lag.apply()
# In[ ]:
# Create a Vlan object
vlan_1 = Vlan(s, 1)
# In this case, now that the VLAN exists within the Switch,
# a GET request is called to obtain the VLAN's information.
# The information is then added to the object as attributes.
vlan_1.get()
# In[ ]:
# Interfaces/Ports added to LAG
port_1_1_8 = Interface(s, '1/1/8')
port_1_1_8.get()
# Make changes to configure LAG as L2
lag.admin = 'down'
lag.routing = False
lag.vlan_trunks = [vlan_1]
lag.lacp = "passive"
lag.other_config["mclag_enabled"] = False
lag.other_config["lacp-fallback"] = False
lag.vlan_mode = "native-untagged"
lag.vlan_tag = vlan_1
# Add port as LAG member
lag.interfaces.append(port_1_1_8)
# Apply changes
lag.apply()
# ===========================================================
# ===========================================================
# ===========================================================
# # APPROACH 2: IMPERATIVE FACTORY APPROACH
# pyaoscx.pyaoscx_factory provide a Factory class to instantiate all pyaoscx Modules through specific methods.
# https://github.com/aruba/pyaoscx/blob/master/pyaoscx/pyaoscx_factory.py
#
# In[ ]:
# Create VLAN 201
# Create Factory object, passing the Session Object
factory = PyaoscxFactory(s)
# In[ ]:
# Create Vlan object
# If vlan is non-existent, Factory instantly creates it inside the switch device
vlan201 = factory.vlan(201, "NAME201")
# In[ ]:
# Same complex example using the IMPERATIVE FACTORY APPROACH
# PLUS USING IMPERATIVE METHODS
# Create the Interface object
lag2 = factory.interface('lag2')
modified = lag2.configure_l2(
description="Created using imperative method",
admin='up',
vlan_mode="native-untagged",
vlan_tag=1,
trunk_allowed_all=True,
native_vlan_tag=True)
# If modified is True, a PUT request was done and object was modified
# #check if the VLAN is created
# In[48]:
Vlan.get_all(s)
# In[ ]:
# At the end, the session MUST be closed
s.close()
# # Challenge:
# • Get the Access switch running-config configuration
# • Save it to a checkpoint named “checkpoint1_by_pyaoscx”
# • Backup running-config to tftp server 10.254.1.21
#
# #=======================================================================================
#
# # Solution:
#
# #You have to change the below cell to "code" type and move them to the above the "s.close" cell to run.
# #The Challege solution:
# config1=Configuration(s)
# Configuration.get_full_config(config1,"startup-config")
# Configuration.create_checkpoint(config1,"running-config","checkpoint1_by_pyaoscx11")
# Configuration.backup_configuration(config1, "running-config", output_file="test_config",
# vrf="<VRF name", config_type='json',
# remote_file_tftp_path="tftp://<TFTP server IP>/test_config")
# # You completed the LAB4 Task2