forked from jonasstenling/iosxe-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiosxe_interface
275 lines (248 loc) · 8.98 KB
/
iosxe_interface
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
#!/usr/bin/env python
# Copyright 2015 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_interface
short_description: Manages configuration of interface
description:
- Manages attributes on interface of 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:
interface:
description:
- Full name of interface, i.e. GigabitEthernet1/1,
GigabitEthernet1.1.100
required: true
default: null
choices: []
aliases: []
admin_state:
description:
- Administrative state of the interface (shutdown or not shutdown)
required: false
default: up
choices: ['up','down']
aliases: []
state:
description:
- Specify desired state of the resource
required: true
default: present
choices: ['present','absent']
aliases: []
config:
description:
- Specify desired configuration of the interface
required: false
default: null
choices: []
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 interface description
- iosxe_interface:
interface: GigabitEthernet2
host: {{ inventory_hostname }}
username: {{ username }}
password: {{ password }}
config: |
description configured by ansible
vrf forwarding test1
ip address 10.1.1.1 255.255.255.0
'''
try:
import socket
import pyskate.utils
from pyskate.iosxe_netconf import IOSXEDevice
from pyskate.iosxe_netconf import IfMissingError, ConfigDeployError
except ImportError as e:
print '*' * 30
print e
print '*' * 30
class ConfigLineError(Exception):
'''Raise if there are invalid configuration lines.'''
def __init__(self, invalid_lines):
self.invalid_lines = invalid_lines
def check_proposed_config(config):
'''Raises an exception if an invalid configuration command is found in
*config*.'''
invalid_lines = []
for line in config:
if 'shutdown' in line:
invalid_lines.append(line)
if invalid_lines:
raise ConfigLineError(invalid_lines)
def change_admin_state(current_admin_state, admin_state):
'''Returns new expected admin state if a change is needed, otherwise
returns False'''
if current_admin_state == 'down':
if admin_state == 'up':
return 'up'
elif admin_state == 'down':
return False
elif current_admin_state == 'up':
if admin_state == 'up':
return False
elif admin_state == 'down':
return 'down'
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 main():
module = AnsibleModule(
argument_spec=dict(
state=dict(choices=['present', 'absent'], default='present'),
admin_state=dict(choices=['up', 'down'], default='up'),
interface=dict(required=True, type='str'),
config=dict(required=True, type='str'),
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'])
interface = module.params['interface']
state = module.params['state']
admin_state = module.params['admin_state']
proposed_config = module.params['config'].split('\n')
device = IOSXEDevice(host, username, password)
try:
check_proposed_config(proposed_config)
except ConfigLineError as e:
module.fail_json(msg="Invalid config line: {0}".format('; '.join(e.invalid_lines)))
try:
device.connect()
except:
module.fail_json(msg="Failed to connect to {0}".format(host))
changed = False
try:
running_config = [x.strip() for x in device.get_interface_config(interface)]
current_state = 'present'
current_admin_state = 'up'
for line in running_config:
if line.strip().startswith('shutdown'):
current_admin_state = 'down'
except IfMissingError:
current_state = 'absent'
current_admin_state = 'down'
running_config = []
# check if state and/or admin state is supposed to change
new_state = change_state(current_state, state)
new_admin_state = change_admin_state(current_admin_state, admin_state)
if new_state:
if 'absent' in new_state:
final_config = ['no interface {0}'.format(interface)]
changed = True
elif 'present' in new_state:
if new_admin_state:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if final_config:
if 'down' in new_admin_state:
final_config.insert(0, "shutdown")
elif 'up' in new_admin_state:
final_config.insert(0, "no shutdown")
final_config.insert(0, "interface {0}".format(interface))
changed = True
# when current_state is absent, current_admin_state is set to down
# which means that the call to change_admin_state() with
# admin_state set to down will return False. that case is handled below.
else:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if final_config:
if 'down' in admin_state:
final_config.insert(0, "shutdown")
elif 'up' in admin_state:
final_config.insert(0, "no shutdown")
final_config.insert(0, "interface {0}".format(interface))
changed = True
elif state == 'absent' and current_state == 'absent':
changed = False
final_config = None
elif state == 'present':
if new_admin_state:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if 'down' in new_admin_state:
final_config.insert(0, "shutdown")
elif 'up' in new_admin_state:
final_config.insert(0, "no shutdown")
final_config.insert(0, "interface {0}".format(interface))
changed = True
else:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if final_config:
if 'down' in admin_state:
final_config.insert(0, "shutdown")
elif 'up' in admin_state:
final_config.insert(0, "no shutdown")
final_config.insert(0, "interface {0}".format(interface))
changed = True
if final_config:
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))
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()