-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiosxe_static_route
322 lines (287 loc) · 9.71 KB
/
iosxe_static_route
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
# Copyright 2016 Jonas Stenling <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to 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.
# See the License for the specific language governing permissions and
# limitations under the License.
DOCUMENTATION = '''
---
module: iosxe_static_route
short_description: Manages configuration of static routes
description:
- Manages static routes in IOS-XE Netconf enabled devices
author: Jonas Stenling
requirements:
- IOS XE with Netconf enabled
- pyskate
notes:
- The module tries to be idempotent, but it is up to the user to verify
that the resulting configuration is correct.
options:
ipv4_network:
description:
- IPv4 network address of static route
required: true
default: null
choices: []
aliases: []
ipv4_netmask:
description:
- IPv4 netmask of static route
required: true
default: null
choices: []
aliases: []
ipv4_nexthop:
description:
- IPv4 next hop of static route
required: true
default: null
choices: []
aliases: []
vrf:
description:
- VRF where route shall be installed
required: false
default: null
choices: []
aliases: []
name:
name:
- Description of static route
required: false
default: null
choices: []
aliases: []
ignore_name:
name:
- Do not perform matching of route name when adding or
removing a route.
required: false
default: null
choices: ['true', 'false']
aliases: []
state:
description:
- Specify desired state of the resource
required: false
default: present
choices: ['present','absent']
aliases: []
host:
description:
- IP Address or hostname (resolvable by Ansible control host)
of the target NX-API enabled switch
required: true
default: null
choices: []
aliases: []
username:
description:
- Username used to login to the router
required: true
default: null
choices: []
aliases: []
password:
description:
- Password used to login to the router
required: true
default: null
choices: []
aliases: []
'''
EXAMPLES = '''
# Configure static route
- iosxe_static_route:
ipv4_network: 10.1.0.0
ipv4_netmask: 255.255.255.0
ipv4_nexthop: 192.168.1.1
vrf: test3
name: Internal server network
host: {{ inventory_hostname }}
username: {{ username }}
password: {{ password }}
'''
try:
import socket
import pyskate.utils
from pyskate.iosxe_netconf import IOSXEDevice, ConfigDeployError
except ImportError as e:
print '*' * 30
print e
print '*' * 30
def change_state(current_state, state):
'''Returns new expected state if a change is needed, otherwise
returns False'''
if current_state == 'absent':
if state == 'present':
return 'present'
elif state == 'absent':
return False
elif current_state == 'present':
if state == 'present':
return False
elif state == 'absent':
return 'absent'
def compare_proposed_to_running_wo_name(proposed_config, running_config, vrf):
'''Compare running_config to proposed_config and ignore name field.
Used when ignore_name == True'''
if vrf is not None:
proposed_config_wo_name = ' '.join(proposed_config[0].split()[0:7])
commands = [proposed_config_wo_name]
for line in running_config:
running_config_wo_name = ' '.join(line.split()[0:7])
if running_config_wo_name in commands:
commands.remove(running_config_wo_name)
return commands
else:
proposed_config_wo_name = ' '.join(proposed_config[0].split()[0:5])
commands = [proposed_config_wo_name]
for line in running_config:
running_config_wo_name = ' '.join(line.split()[0:5])
if running_config_wo_name in commands:
commands.remove(running_config_wo_name)
return commands
def main():
module = AnsibleModule(
argument_spec=dict(
ipv4_network=dict(required=True, type='str'),
ipv4_netmask=dict(required=True, type='str'),
ipv4_nexthop=dict(required=True, type='str'),
vrf=dict(required=False, type='str', default=None),
name=dict(required=False, type='str', default=None),
ignore_name=dict(required=False, type='bool'),
state=dict(choices=['present', 'absent'], default='present'),
host=dict(required=True),
username=dict(type='str'),
password=dict(type='str'),
),
supports_check_mode=True
)
username = module.params['username']
password = module.params['password']
host = socket.gethostbyname(module.params['host'])
state = module.params['state']
ipv4_network = module.params['ipv4_network']
ipv4_netmask = module.params['ipv4_netmask']
ipv4_nexthop = module.params['ipv4_nexthop']
ignore_name = module.params['ignore_name']
vrf = module.params['vrf']
name = module.params['name']
device = IOSXEDevice(host, username, password)
try:
device.connect()
except:
module.fail_json(msg="Failed to connect to {0}".format(host))
changed = False
#
# This part should be refactored to have all args in a list and
# a function to apply the correct config, such as:
#
# args = [vrf, ipv4_network, ipv4_netmask, ipv4_nexthop, name]
#
# map = { 'vrf': [0, 'vrf'], 'ipv4_network': [1, None] }
#
# Where first item in list is positional parameter and the second if any
# additional configuration shall be applied.
#
# cmd = ''
# for i in args:
# cmd = cmd + apply_map(i)
#
# and the apply_map function will check if the args is not None and in that case
# add the config to cmd.
if name is not None:
if len(name.split()) > 1: # name contains spaces, need to wrap in quotes
if vrf is not None:
proposed_config = ['ip route vrf {0} {1} {2} {3} name "{4}"'.format(
vrf,
ipv4_network,
ipv4_netmask,
ipv4_nexthop,
name)]
else:
proposed_config = ['ip route {0} {1} {2} name "{3}"'.format(
ipv4_network,
ipv4_netmask,
ipv4_nexthop,
name)]
else:
if vrf is not None:
proposed_config = ['ip route vrf {0} {1} {2} {3} name {4}'.format(
vrf,
ipv4_network,
ipv4_netmask,
ipv4_nexthop,
name)]
else:
proposed_config = ['ip route {0} {1} {2} name {3}'.format(
ipv4_network,
ipv4_netmask,
ipv4_nexthop,
name)]
else:
if vrf is not None:
proposed_config = ['ip route vrf {0} {1} {2} {3}'.format(
vrf,
ipv4_network,
ipv4_netmask,
ipv4_nexthop)]
else:
proposed_config = ['ip route {0} {1} {2}'.format(
ipv4_network,
ipv4_netmask,
ipv4_nexthop)]
running_config = [x.strip() for x in device.get_config()]
running_config = [x for x in running_config if x.startswith('ip route')]
if ignore_name:
final_config = compare_proposed_to_running_wo_name(proposed_config,
running_config, vrf)
else:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if len(final_config) == 0: # route already exists in router
current_state = 'present'
else:
current_state = 'absent'
# check if state is supposed to change
new_state = change_state(current_state, state)
if new_state:
if 'absent' in new_state:
if ignore_name:
if vrf is not None:
final_config = ['no ' + ' '.join(proposed_config[0].split()[0:7])]
else:
final_config = ['no ' + ' '.join(proposed_config[0].split()[0:5])]
else:
final_config = ['no ' + proposed_config[0]]
changed = True
elif 'present' in new_state:
changed = True
if module.check_mode:
module.exit_json(changed=True, commands='\n'.join(final_config))
try:
device.edit_config('\n'.join(final_config))
changed = True
except ConfigDeployError:
changed = False
module.fail_json(msg="Failed to configure {0}".format(host))
else:
# if state is not changed, set correct final_config
final_config = []
results = {}
results['proposed'] = proposed_config
results['final'] = final_config
results['changed'] = changed
device.disconnect()
module.exit_json(**results)
from ansible.module_utils.basic import *
main()