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
|
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
DOCUMENTATION = '''
---
module: nxos_gir_profile_management
extends_documentation_fragment: nxos
version_added: "2.2"
short_description: Create a maintenance-mode or normal-mode profile for GIR.
description:
- Manage a maintenance-mode or normal-mode profile with configuration
commands that can be applied during graceful removal
or graceful insertion.
author:
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- C(state=absent) removes the whole profile.
options:
commands:
description:
- List of commands to be included into the profile.
required: false
default: null
mode:
description:
- Configure the profile as Maintenance or Normal mode.
required: true
choices: ['maintenance', 'normal']
state:
description:
- Specify desired state of the resource.
required: false
default: present
choices: ['present','absent']
'''
EXAMPLES = '''
# Create a maintenance-mode profile
- nxos_gir_profile_management:
mode: maintenance
commands:
- router eigrp 11
- isolate
# Remove the maintenance-mode profile
- nxos_gir_profile_management:
mode: maintenance
state: absent
'''
RETURN = '''
proposed:
description: list of commands passed into module.
returned: verbose mode
type: list
sample: ["router eigrp 11", "isolate"]
existing:
description: list of existing profile commands.
returned: verbose mode
type: list
sample: ["router bgp 65535","isolate","router eigrp 10","isolate",
"diagnostic bootup level complete"]
end_state:
description: list of profile entries after module execution.
returned: verbose mode
type: list
sample: ["router bgp 65535","isolate","router eigrp 10","isolate",
"diagnostic bootup level complete","router eigrp 11", "isolate"]
updates:
description: commands sent to the device
returned: always
type: list
sample: ["configure maintenance profile maintenance-mode",
"router eigrp 11","isolate"]
changed:
description: check to see if a change was made on the device
returned: always
type: boolean
sample: true
'''
import re
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.common.config import CustomNetworkConfig
def get_existing(module):
existing = []
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
if module.params['mode'] == 'maintenance':
parents = ['configure maintenance profile maintenance-mode']
else:
parents = ['configure maintenance profile normal-mode']
config = netcfg.get_section(parents)
if config:
existing = config.splitlines()
existing = [cmd.strip() for cmd in existing]
existing.pop(0)
return existing
def state_present(module, existing, commands):
cmds = list()
if existing == commands:
# Idempotent case
return cmds
cmds.extend(commands)
if module.params['mode'] == 'maintenance':
cmds.insert(0, 'configure maintenance profile maintenance-mode')
else:
cmds.insert(0, 'configure maintenance profile normal-mode')
return cmds
def state_absent(module, existing, commands):
if module.params['mode'] == 'maintenance':
cmds = ['no configure maintenance profile maintenance-mode']
else:
cmds = ['no configure maintenance profile normal-mode']
return cmds
def invoke(name, *args, **kwargs):
func = globals().get(name)
if func:
return func(*args, **kwargs)
def main():
argument_spec = dict(
commands=dict(required=False, type='list'),
mode=dict(required=True, choices=['maintenance', 'normal']),
state=dict(choices=['absent', 'present'], default='present')
)
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
warnings = list()
state = module.params['state']
commands = module.params['commands'] or []
if state == 'absent' and commands:
module.fail_json(msg='when state is absent, no command can be used.')
existing = invoke('get_existing', module)
end_state = existing
changed = False
result = {}
cmds = []
if state == 'present' or (state == 'absent' and existing):
cmds = invoke('state_%s' % state, module, existing, commands)
if module.check_mode:
module.exit_json(changed=True, commands=cmds)
else:
if cmds:
load_config(module, cmds)
changed = True
end_state = invoke('get_existing', module)
result['changed'] = changed
if module._verbosity > 0:
end_state = invoke('get_existing', module)
result['end_state'] = end_state
result['existing'] = existing
result['proposed'] = commands
result['updates'] = cmds
result['warnings'] = warnings
module.exit_json(**result)
if __name__ == '__main__':
main()
|