Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_gir.py

raw
1 #!/usr/bin/python
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18 from __future__ import absolute_import, division, print_function
19
20
21 __metaclass__ = type
22
23
24 DOCUMENTATION = """
25 module: nxos_gir
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Trigger a graceful removal or insertion (GIR) of the switch.
29 description:
30 - Trigger a graceful removal or insertion (GIR) of the switch.
31 - GIR processing may take more than 2 minutes. Timeout settings are automatically
32 extended to 200s when user timeout settings are insufficient.
33 version_added: 1.0.0
34 author:
35 - Gabriele Gerbino (@GGabriele)
36 notes:
37 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
38 - Unsupported for Cisco MDS
39 - C(state) has effect only in combination with C(system_mode_maintenance_timeout)
40 or C(system_mode_maintenance_on_reload_reset_reason).
41 - Using C(system_mode_maintenance) and C(system_mode_maintenance_dont_generate_profile)
42 would make the module fail, but the system mode will be triggered anyway.
43 options:
44 system_mode_maintenance:
45 description:
46 - When C(system_mode_maintenance=true) it puts all enabled protocols in maintenance
47 mode (using the isolate command). When C(system_mode_maintenance=false) it puts
48 all enabled protocols in normal mode (using the no isolate command).
49 type: bool
50 system_mode_maintenance_dont_generate_profile:
51 description:
52 - When C(system_mode_maintenance_dont_generate_profile=true) it prevents the dynamic
53 searching of enabled protocols and executes commands configured in a maintenance-mode
54 profile. Use this option if you want the system to use a maintenance-mode profile
55 that you have created. When C(system_mode_maintenance_dont_generate_profile=false)
56 it prevents the dynamic searching of enabled protocols and executes commands
57 configured in a normal-mode profile. Use this option if you want the system
58 to use a normal-mode profile that you have created.
59 type: bool
60 system_mode_maintenance_timeout:
61 description:
62 - Keeps the switch in maintenance mode for a specified number of minutes. Range
63 is 5-65535.
64 type: str
65 system_mode_maintenance_shutdown:
66 description:
67 - Shuts down all protocols, vPC domains, and interfaces except the management
68 interface (using the shutdown command). This option is disruptive while C(system_mode_maintenance)
69 (which uses the isolate command) is not.
70 type: bool
71 system_mode_maintenance_on_reload_reset_reason:
72 description:
73 - Boots the switch into maintenance mode automatically in the event of a specified
74 system crash. Note that not all reset reasons are applicable for all platforms.
75 Also if reset reason is set to match_any, it is not idempotent as it turns on
76 all reset reasons. If reset reason is match_any and state is absent, it turns
77 off all the reset reasons.
78 choices:
79 - hw_error
80 - svc_failure
81 - kern_failure
82 - wdog_timeout
83 - fatal_error
84 - lc_failure
85 - match_any
86 - manual_reload
87 - any_other
88 - maintenance
89 type: str
90 state:
91 description:
92 - Specify desired state of the resource.
93 default: present
94 choices:
95 - present
96 - absent
97 type: str
98 """
99
100 EXAMPLES = """
101 # Trigger system maintenance mode
102 - cisco.nxos.nxos_gir:
103 system_mode_maintenance: true
104 host: '{{ inventory_hostname }}'
105 username: '{{ un }}'
106 password: '{{ pwd }}'
107 # Trigger system normal mode
108 - cisco.nxos.nxos_gir:
109 system_mode_maintenance: false
110 host: '{{ inventory_hostname }}'
111 username: '{{ un }}'
112 password: '{{ pwd }}'
113 # Configure on-reload reset-reason for maintenance mode
114 - cisco.nxos.nxos_gir:
115 system_mode_maintenance_on_reload_reset_reason: manual_reload
116 state: present
117 host: '{{ inventory_hostname }}'
118 username: '{{ un }}'
119 password: '{{ pwd }}'
120 # Add on-reload reset-reason for maintenance mode
121 - cisco.nxos.nxos_gir:
122 system_mode_maintenance_on_reload_reset_reason: hw_error
123 state: present
124 host: '{{ inventory_hostname }}'
125 username: '{{ un }}'
126 password: '{{ pwd }}'
127 # Remove on-reload reset-reason for maintenance mode
128 - cisco.nxos.nxos_gir:
129 system_mode_maintenance_on_reload_reset_reason: manual_reload
130 state: absent
131 host: '{{ inventory_hostname }}'
132 username: '{{ un }}'
133 password: '{{ pwd }}'
134 # Set timeout for maintenance mode
135 - cisco.nxos.nxos_gir:
136 system_mode_maintenance_timeout: 30
137 state: present
138 host: '{{ inventory_hostname }}'
139 username: '{{ un }}'
140 password: '{{ pwd }}'
141 # Remove timeout for maintenance mode
142 - cisco.nxos.nxos_gir:
143 system_mode_maintenance_timeout: 30
144 state: absent
145 host: '{{ inventory_hostname }}'
146 username: '{{ un }}'
147 password: '{{ pwd }}'
148 """
149
150 RETURN = """
151 final_system_mode:
152 description: describe the last system mode
153 returned: verbose mode
154 type: str
155 sample: normal
156 updates:
157 description: commands sent to the device
158 returned: verbose mode
159 type: list
160 sample: ["terminal dont-ask", "system mode maintenance timeout 10"]
161 changed:
162 description: check to see if a change was made on the device
163 returned: always
164 type: bool
165 sample: true
166 """
167
168 from ansible.module_utils.basic import AnsibleModule
169
170 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
171 load_config,
172 run_commands,
173 )
174
175
176 def get_system_mode(module):
177 command = {"command": "show system mode", "output": "text"}
178 body = run_commands(module, [command])[0]
179 if body and "normal" in body.lower():
180 mode = "normal"
181 else:
182 mode = "maintenance"
183 return mode
184
185
186 def get_maintenance_timeout(module):
187 command = {"command": "show maintenance timeout", "output": "text"}
188 body = run_commands(module, [command])[0]
189 timeout = body.split()[4]
190 return timeout
191
192
193 def get_reset_reasons(module):
194 command = {
195 "command": "show maintenance on-reload reset-reasons",
196 "output": "text",
197 }
198 body = run_commands(module, [command])[0]
199 return body
200
201
202 def get_commands(module, state, mode):
203 commands = list()
204 if module.params["system_mode_maintenance"] is True and mode == "normal":
205 commands.append("system mode maintenance")
206 elif module.params["system_mode_maintenance"] is False and mode == "maintenance":
207 commands.append("no system mode maintenance")
208
209 elif (
210 module.params["system_mode_maintenance_dont_generate_profile"] is True and mode == "normal"
211 ):
212 commands.append("system mode maintenance dont-generate-profile")
213 elif (
214 module.params["system_mode_maintenance_dont_generate_profile"] is False
215 and mode == "maintenance"
216 ):
217 commands.append("no system mode maintenance dont-generate-profile")
218
219 elif module.params["system_mode_maintenance_timeout"]:
220 timeout = get_maintenance_timeout(module)
221 if state == "present" and timeout != module.params["system_mode_maintenance_timeout"]:
222 commands.append(
223 "system mode maintenance timeout {0}".format(
224 module.params["system_mode_maintenance_timeout"],
225 ),
226 )
227 elif state == "absent" and timeout == module.params["system_mode_maintenance_timeout"]:
228 commands.append(
229 "no system mode maintenance timeout {0}".format(
230 module.params["system_mode_maintenance_timeout"],
231 ),
232 )
233
234 elif module.params["system_mode_maintenance_shutdown"] and mode == "normal":
235 commands.append("system mode maintenance shutdown")
236 elif module.params["system_mode_maintenance_shutdown"] is False and mode == "maintenance":
237 commands.append("no system mode maintenance")
238
239 elif module.params["system_mode_maintenance_on_reload_reset_reason"]:
240 reset_reasons = get_reset_reasons(module)
241 if (
242 state == "present"
243 and module.params["system_mode_maintenance_on_reload_reset_reason"].lower()
244 not in reset_reasons.lower()
245 ):
246 commands.append(
247 "system mode maintenance on-reload "
248 "reset-reason {0}".format(
249 module.params["system_mode_maintenance_on_reload_reset_reason"],
250 ),
251 )
252 elif (
253 state == "absent"
254 and module.params["system_mode_maintenance_on_reload_reset_reason"].lower()
255 in reset_reasons.lower()
256 ):
257 commands.append(
258 "no system mode maintenance on-reload "
259 "reset-reason {0}".format(
260 module.params["system_mode_maintenance_on_reload_reset_reason"],
261 ),
262 )
263
264 if commands:
265 commands.insert(0, "terminal dont-ask")
266 return commands
267
268
269 def main():
270 argument_spec = dict(
271 system_mode_maintenance=dict(required=False, type="bool"),
272 system_mode_maintenance_dont_generate_profile=dict(required=False, type="bool"),
273 system_mode_maintenance_timeout=dict(required=False, type="str"),
274 system_mode_maintenance_shutdown=dict(required=False, type="bool"),
275 system_mode_maintenance_on_reload_reset_reason=dict(
276 required=False,
277 choices=[
278 "hw_error",
279 "svc_failure",
280 "kern_failure",
281 "wdog_timeout",
282 "fatal_error",
283 "lc_failure",
284 "match_any",
285 "manual_reload",
286 "any_other",
287 "maintenance",
288 ],
289 ),
290 state=dict(choices=["absent", "present"], default="present", required=False),
291 )
292
293 module = AnsibleModule(
294 argument_spec=argument_spec,
295 mutually_exclusive=[
296 [
297 "system_mode_maintenance",
298 "system_mode_maintenance_dont_generate_profile",
299 "system_mode_maintenance_timeout",
300 "system_mode_maintenance_shutdown",
301 "system_mode_maintenance_on_reload_reset_reason",
302 ],
303 ],
304 required_one_of=[
305 [
306 "system_mode_maintenance",
307 "system_mode_maintenance_dont_generate_profile",
308 "system_mode_maintenance_timeout",
309 "system_mode_maintenance_shutdown",
310 "system_mode_maintenance_on_reload_reset_reason",
311 ],
312 ],
313 supports_check_mode=True,
314 )
315
316 warnings = list()
317
318 state = module.params["state"]
319 mode = get_system_mode(module)
320 commands = get_commands(module, state, mode)
321 changed = False
322 if commands:
323 if module.check_mode:
324 module.exit_json(changed=True, commands=commands)
325 else:
326 load_config(module, commands)
327 changed = True
328
329 result = {}
330 result["changed"] = changed
331 if module._verbosity > 0:
332 final_system_mode = get_system_mode(module)
333 result["final_system_mode"] = final_system_mode
334 result["updates"] = commands
335
336 result["warnings"] = warnings
337
338 module.exit_json(**result)
339
340
341 if __name__ == "__main__":
342 main()