-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiosxe_vrf
218 lines (195 loc) · 6.3 KB
/
iosxe_vrf
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
#!/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_vrf
short_description: Manages configuration of VRF definition
description:
- Manages configuration of VRF definition configuration in Netconf
enabled IOS-XE router.
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:
vrf:
description:
- Full name of VRF
required: true
default: null
choices: []
aliases: []
state:
description:
- Specify desired state of the resource
required: false
default: present
choices: ['present','absent']
aliases: []
config:
description:
- Specify desired configuration of the VRF definition
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_vrf:
vrf: test1
host: {{ inventory_hostname }}
username: {{ username }}
password: {{ password }}
state: present
config: |
rd 1:3
route-target export 1:3
route-target import 1:3
!
address-family ipv4
exit-address-family
!
address-family ipv6
exit-address-family
'''
try:
import socket
import pyskate.utils
import re
from pyskate.iosxe_netconf import IOSXEDevice
from pyskate.iosxe_netconf import VRFMissingError, ConfigDeployError
except ImportError as e:
print '*' * 30
print e
print '*' * 30
class ConfigLineError(Exception):
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*. Currently used to safeguard from exit to global configuration
mode.'''
invalid_lines = []
for line in config:
if re.match('^exit$', line):
invalid_lines.append(line)
if invalid_lines:
raise ConfigLineError(invalid_lines)
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'),
vrf=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'])
vrf = module.params['vrf']
state = module.params['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_vrf_definition_config(vrf)]
current_state = 'present'
except VRFMissingError:
current_state = 'absent'
running_config = []
# check if state is supposed to change
new_state = change_state(current_state, state)
final_config = None
if new_state:
if 'absent' in new_state:
final_config = ['no vrf definition {0}'.format(vrf)]
changed = True
elif 'present' in new_state:
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
final_config.insert(0, "vrf definition {0}".format(vrf))
changed = True
elif state == 'present':
final_config = pyskate.utils.compare_proposed_to_running(proposed_config, running_config)
if final_config:
final_config.insert(0, "vrf definition {0}".format(vrf))
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()