Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_device_quick_deploy.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.3.0
7 # Copyright (C) 2022-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12 from __future__ import (absolute_import, division, print_function)
13 __metaclass__ = type
14
15 DOCUMENTATION = """
16 ---
17 module: ome_device_quick_deploy
18 short_description: Configure Quick Deploy settings on OpenManage Enterprise Modular.
19 description: This module allows to configure the Quick Deploy settings of the server or IOM
20 on OpenManage Enterprise Modular.
21 version_added: "5.0.0"
22 extends_documentation_fragment:
23 - dellemc.openmanage.omem_auth_options
24 options:
25 device_id:
26 type: int
27 description:
28 - The ID of the chassis for which the Quick Deploy settings to be deployed.
29 - If the device ID is not specified, this module updates the Quick Deploy settings for the I(hostname).
30 - I(device_id) is mutually exclusive with I(device_service_tag).
31 device_service_tag:
32 type: str
33 description:
34 - The service tag of the chassis for which the Quick Deploy settings to be deployed.
35 - If the device service tag is not specified, this module updates the Quick Deploy settings for the I(hostname).
36 - I(device_service_tag) is mutually exclusive with I(device_id).
37 setting_type:
38 type: str
39 required: true
40 choices: [ServerQuickDeploy, IOMQuickDeploy]
41 description:
42 - The type of the Quick Deploy settings to be applied.
43 - C(ServerQuickDeploy) to apply the server Quick Deploy settings.
44 - C(IOMQuickDeploy) to apply the IOM Quick Deploy settings.
45 job_wait:
46 type: bool
47 description: Determines whether to wait for the job completion or not.
48 default: true
49 job_wait_timeout:
50 type: int
51 description:
52 - The maximum wait time of I(job_wait) in seconds. The job is tracked only for this duration.
53 - This option is applicable when I(job_wait) is C(true).
54 default: 120
55 quick_deploy_options:
56 type: dict
57 required: true
58 description: The Quick Deploy settings for server and IOM quick deploy.
59 suboptions:
60 password:
61 type: str
62 description:
63 - The password to login to the server or IOM.
64 - The module will always report change when I(password) option is added.
65 ipv4_enabled:
66 type: bool
67 description: Enables or disables the IPv4 network.
68 ipv4_network_type:
69 type: str
70 choices: [Static, DHCP]
71 description:
72 - IPv4 network type.
73 - I(ipv4_network_type) is required if I(ipv4_enabled) is C(true).
74 - C(Static) to configure the static IP settings.
75 - C(DHCP) to configure the Dynamic IP settings.
76 ipv4_subnet_mask:
77 type: str
78 description:
79 - IPv4 subnet mask.
80 - I(ipv4_subnet_mask) is required if I(ipv4_network_type) is C(Static).
81 ipv4_gateway:
82 type: str
83 description:
84 - IPv4 gateway.
85 - I(ipv4_gateway) is required if I(ipv4_network_type) is C(Static).
86 ipv6_enabled:
87 type: bool
88 description: Enables or disables the IPv6 network.
89 ipv6_network_type:
90 type: str
91 choices: [Static, DHCP]
92 description:
93 - IPv6 network type.
94 - I(ipv6_network_type) is required if I(ipv6_enabled) is C(true).
95 - C(Static) to configure the static IP settings.
96 - C(DHCP) to configure the Dynamic IP settings.
97 ipv6_prefix_length:
98 type: int
99 description:
100 - IPV6 prefix length.
101 - I(ipv6_prefix_length) is required if I(ipv6_network_type) is C(Static).
102 ipv6_gateway:
103 type: str
104 description:
105 - IPv6 gateway.
106 - I(ipv6_gateway) is required if I(ipv6_network_type) is C(Static).
107 slots:
108 type: list
109 elements: dict
110 description: The slot configuration for the server or IOM.
111 suboptions:
112 slot_id:
113 type: int
114 required: true
115 description: The ID of the slot.
116 slot_ipv4_address:
117 type: str
118 description: The IPv4 address of the slot.
119 slot_ipv6_address:
120 type: str
121 description: The IPv6 address of the slot.
122 vlan_id:
123 type: int
124 description: The ID of the VLAN.
125 requirements:
126 - "python >= 3.9.6"
127 author:
128 - "Felix Stephen (@felixs88)"
129 - "Shivam Sharma (@ShivamSh3)"
130 - "Kritika Bhateja (@Kritika-Bhateja-03)"
131 notes:
132 - Run this module from a system that has direct access to OpenManage Enterprise Modular.
133 - This module supports C(check_mode).
134 - The module will always report change when I(password) option is added.
135 - If the chassis is a member of a multi-chassis group and it is assigned as a backup
136 lead chassis, the operations performed on the chassis using this module may
137 conflict with the management operations performed on the chassis through the lead chassis.
138 """
139
140 EXAMPLES = """
141 ---
142 - name: Configure server Quick Deploy settings of the chassis using device ID.
143 dellemc.openmanage.ome_device_quick_deploy:
144 hostname: "192.168.0.1"
145 username: "username"
146 password: "password"
147 device_id: 25011
148 setting_type: ServerQuickDeploy
149 ca_path: "/path/to/ca_cert.pem"
150 quick_deploy_options:
151 password: "password"
152 ipv4_enabled: true
153 ipv4_network_type: Static
154 ipv4_subnet_mask: 255.255.255.0
155 ipv4_gateway: 192.168.0.1
156 ipv6_enabled: true
157 ipv6_network_type: Static
158 ipv6_prefix_length: 1
159 ipv6_gateway: "::"
160 slots:
161 - slot_id: 1
162 slot_ipv4_address: 192.168.0.2
163 slot_ipv6_address: "::"
164 vlan_id: 1
165 - slot_id: 2
166 slot_ipv4_address: 192.168.0.3
167 slot_ipv6_address: "::"
168 vlan_id: 2
169
170 - name: Configure server Quick Deploy settings of the chassis using device service tag.
171 dellemc.openmanage.ome_device_quick_deploy:
172 hostname: "192.168.0.1"
173 username: "username"
174 password: "password"
175 device_service_tag: GHRT2RL
176 setting_type: IOMQuickDeploy
177 ca_path: "/path/to/ca_cert.pem"
178 quick_deploy_options:
179 password: "password"
180 ipv4_enabled: true
181 ipv4_network_type: Static
182 ipv4_subnet_mask: 255.255.255.0
183 ipv4_gateway: 192.168.0.1
184 ipv6_enabled: true
185 ipv6_network_type: Static
186 ipv6_prefix_length: 1
187 ipv6_gateway: "::"
188 slots:
189 - slot_id: 1
190 slot_ipv4_address: 192.168.0.2
191 slot_ipv6_address: "::"
192 vlan_id: 1
193 - slot_id: 2
194 slot_ipv4_address: 192.168.0.3
195 slot_ipv6_address: "::"
196 vlan_id: 2
197 """
198
199 RETURN = """
200 ---
201 msg:
202 type: str
203 description: Overall status of the device quick deploy settings.
204 returned: always
205 sample: "Successfully deployed the quick deploy settings."
206 job_id:
207 type: int
208 description: The job ID of the submitted quick deploy job.
209 returned: when quick deploy job is submitted.
210 sample: 1234
211 quick_deploy_settings:
212 type: dict
213 description: returned when quick deploy settings are deployed successfully.
214 returned: success
215 sample: {
216 "DeviceId": 25011,
217 "SettingType": "ServerQuickDeploy",
218 "ProtocolTypeV4": true,
219 "NetworkTypeV4": "Static",
220 "IpV4Gateway": 192.168.0.1,
221 "IpV4SubnetMask": "255.255.255.0",
222 "ProtocolTypeV6": true,
223 "NetworkTypeV6": "Static",
224 "PrefixLength": "2",
225 "IpV6Gateway": "::",
226 "slots": [
227 {
228 "DeviceId": 25011,
229 "DeviceCapabilities": [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 41, 8, 7, 4, 3, 2, 1, 31, 30],
230 "DeviceIPV4Address": "192.168.0.2",
231 "DeviceIPV6Address": "::",
232 "Dhcpipv4": "Disabled",
233 "Dhcpipv6": "Disabled",
234 "Ipv4Enabled": "Enabled",
235 "Ipv6Enabled": "Enabled",
236 "Model": "PowerEdge MX840c",
237 "SlotIPV4Address": "192.168.0.2",
238 "SlotIPV6Address": "::",
239 "SlotId": 1,
240 "SlotSelected": true,
241 "SlotSettingsApplied": true,
242 "SlotType": "2000",
243 "Type": "1000",
244 "VlanId": "1"
245 },
246 {
247 "DeviceId": 0,
248 "Model": "",
249 "SlotIPV4Address": "0.0.0.0",
250 "SlotIPV6Address": "::",
251 "SlotId": 2,
252 "SlotSelected": false,
253 "SlotSettingsApplied": false,
254 "SlotType": "2000",
255 "Type": "0"
256 },
257 {
258 "DeviceId": 0,
259 "Model": "",
260 "SlotIPV4Address": "0.0.0.0",
261 "SlotIPV6Address": "::",
262 "SlotId": 3,
263 "SlotSelected": false,
264 "SlotSettingsApplied": false,
265 "SlotType": "2000",
266 "Type": "0"
267 },
268 {
269 "DeviceId": 0,
270 "Model": "",
271 "SlotIPV4Address": "0.0.0.0",
272 "SlotIPV6Address": "::",
273 "SlotId": 4,
274 "SlotSelected": false,
275 "SlotSettingsApplied": false,
276 "SlotType": "2000",
277 "Type": "0"
278 },
279 {
280 "DeviceId": 0,
281 "Model": "",
282 "SlotIPV4Address": "0.0.0.0",
283 "SlotIPV6Address": "::",
284 "SlotId": 5,
285 "SlotSelected": false,
286 "SlotSettingsApplied": false,
287 "SlotType": "2000",
288 "Type": "0"
289 },
290 {
291 "DeviceId": 0,
292 "Model": "",
293 "SlotIPV4Address": "0.0.0.0",
294 "SlotIPV6Address": "::",
295 "SlotId": 6,
296 "SlotSelected": false,
297 "SlotSettingsApplied": false,
298 "SlotType": "2000",
299 "Type": "0"
300 },
301 {
302 "DeviceId": 0,
303 "Model": "",
304 "SlotIPV4Address": "0.0.0.0",
305 "SlotIPV6Address": "::",
306 "SlotId": 7,
307 "SlotSelected": false,
308 "SlotSettingsApplied": false,
309 "SlotType": "2000",
310 "Type": "0"
311 },
312 {
313 "DeviceId": 0,
314 "Model": "",
315 "SlotIPV4Address": "0.0.0.0",
316 "SlotIPV6Address": "::",
317 "SlotId": 8,
318 "SlotSelected": false,
319 "SlotSettingsApplied": false,
320 "SlotType": "2000",
321 "Type": "0"
322 }
323 ]
324 }
325 error_info:
326 description: Details of the HTTP Error.
327 returned: on HTTP error
328 type: dict
329 sample: {
330 "error": {
331 "code": "Base.1.0.GeneralError",
332 "message": "A general error has occurred. See ExtendedInfo for more information.",
333 "@Message.ExtendedInfo": [
334 {
335 "MessageId": "GEN1234",
336 "RelatedProperties": [],
337 "Message": "Unable to process the request because an error occurred.",
338 "MessageArgs": [],
339 "Severity": "Critical",
340 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
341 }
342 ]
343 }
344 }
345 """
346
347
348 import copy
349 import json
350 import socket
351 from ssl import SSLError
352 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
353 from ansible.module_utils.urls import ConnectionError
354 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
355
356 DOMAIN_URI = "ManagementDomainService/Domains"
357 DEVICE_URI = "DeviceService/Devices"
358 QUICK_DEPLOY_API = "DeviceService/Devices({0})/Settings('{1}')"
359
360 DOMAIN_FAIL_MSG = "The operation to configure the Quick Deploy settings is supported only on " \
361 "OpenManage Enterprise Modular."
362 IP_FAIL_MSG = "Invalid '{0}' address provided for the {1}."
363 FETCH_FAIL_MSG = "Unable to retrieve the device information."
364 DEVICE_FAIL_MSG = "Unable to complete the operation because the entered target device {0} '{1}' is invalid."
365 QUICK_DEPLOY_FAIL_MSG = "Unable to complete the operation because the {0} configuration settings " \
366 "are not supported on the specified device."
367 INVALID_SLOT_MSG = "Unable to complete the operation because the entered slot(s) '{0}' does not exist."
368 CHANGES_FOUND = "Changes found to be applied."
369 NO_CHANGES_FOUND = "No changes found to be applied."
370 SUCCESS_MSG = "Successfully deployed the Quick Deploy settings."
371 FAIL_MSG = "Unable to deploy the Quick Deploy settings."
372 QUICK_DEPLOY_JOB_DESC = "The Quick Deploy job is initiated from the OpenManage Ansible Module collections."
373 JOB_MSG = "Successfully submitted the Quick Deploy job settings."
374
375
376 def validate_ip_address(address, flag):
377 value = True
378 try:
379 if flag == "IPV4":
380 socket.inet_aton(address)
381 value = address.count('.') == 3
382 else:
383 socket.inet_pton(socket.AF_INET6, address)
384 except socket.error:
385 value = False
386 return value
387
388
389 def ip_address_field(module, field, deploy_options, slot=False):
390 module_params = deploy_options
391 if slot:
392 module_params = deploy_options
393 for val in field:
394 field_value = module_params.get(val[0])
395 if field_value is not None:
396 valid = validate_ip_address(module_params.get(val[0]), val[1])
397 if valid is False:
398 module.exit_json(msg=IP_FAIL_MSG.format(field_value, val[0]), failed=True)
399
400
401 def check_domain_service(module, rest_obj):
402 try:
403 rest_obj.invoke_request("GET", DOMAIN_URI, api_timeout=5)
404 except HTTPError as err:
405 err_message = json.load(err)
406 if err_message["error"]["@Message.ExtendedInfo"][0]["MessageId"] == "CGEN1006":
407 module.exit_json(msg=DOMAIN_FAIL_MSG, failed=True)
408
409
410 def get_ip_from_host(hostname):
411 ipaddr = hostname
412 try:
413 result = socket.getaddrinfo(hostname, None)
414 last_element = result[-1]
415 ip_address = last_element[-1][0]
416 if ip_address:
417 ipaddr = ip_address
418 except socket.gaierror:
419 ipaddr = hostname
420 except Exception:
421 ipaddr = hostname
422 return ipaddr
423
424
425 def get_chassis_device(module, rest_obj):
426 key, value = None, None
427 ipaddress = get_ip_from_host(module.params["hostname"])
428 resp = rest_obj.invoke_request("GET", DOMAIN_URI)
429 for data in resp.json_data["value"]:
430 if ipaddress in data["PublicAddress"]:
431 key, value = ("Id", data["DeviceId"])
432 break
433 else:
434 module.exit_json(msg=FETCH_FAIL_MSG, failed=True)
435 return key, value
436
437
438 def check_mode_validation(module, deploy_data):
439 deploy_options = module.params.get("quick_deploy_options")
440 req_data, req_payload = {}, {}
441 if deploy_options.get("password") is not None:
442 req_data["rootCredential"] = deploy_options.get("password")
443 ipv4_enabled = deploy_options.get("ipv4_enabled")
444 ipv4_enabled_deploy = deploy_data["ProtocolTypeV4"]
445 ipv6_enabled_deploy = deploy_data["ProtocolTypeV6"]
446 ipv4_nt_deploy = deploy_data.get("NetworkTypeV4")
447 ipv6_nt_deploy = deploy_data.get("NetworkTypeV6")
448 ipv6_enabled = deploy_options.get("ipv6_enabled")
449 update_ipv4_data(req_data, ipv4_enabled, ipv4_enabled_deploy, ipv4_nt_deploy, deploy_options)
450 update_ipv6_data(req_data, ipv6_enabled, ipv6_enabled_deploy, ipv6_nt_deploy, deploy_options)
451 resp_data = {
452 "ProtocolTypeV4": str(ipv4_enabled_deploy).lower(), "NetworkTypeV4": deploy_data.get("NetworkTypeV4"),
453 "IpV4SubnetMask": deploy_data.get("IpV4SubnetMask"), "IpV4Gateway": deploy_data.get("IpV4Gateway"),
454 "ProtocolTypeV6": str(ipv6_enabled_deploy).lower(), "NetworkTypeV6": deploy_data.get("NetworkTypeV6"),
455 "PrefixLength": deploy_data.get("PrefixLength"), "IpV6Gateway": deploy_data.get("IpV6Gateway")}
456 resp_filter_data = dict([(k, v) for k, v in resp_data.items() if v is not None])
457 req_data_filter = dict([(k, v) for k, v in req_data.items() if v is not None])
458 copy_resp_filter_data = copy.deepcopy(resp_filter_data)
459 copy_resp_filter_data.update(req_data_filter)
460 diff_changes = [bool(set(resp_filter_data.items()) ^ set(copy_resp_filter_data.items()))]
461 req_slot_payload, invalid_slot = [], []
462 slots = deploy_options.get("slots")
463 if slots is not None:
464 exist_slot = deploy_data.get("Slots")
465 for each in slots:
466 exist_filter_slot = list(filter(lambda d: d["SlotId"] in [each["slot_id"]], exist_slot))
467 if exist_filter_slot:
468 req_slot_1 = {"SlotId": each["slot_id"], "SlotIPV4Address": each.get("slot_ipv4_address"),
469 "SlotIPV6Address": each.get("slot_ipv6_address"), "VlanId": each.get("vlan_id")}
470 if each.get("vlan_id") is not None:
471 req_slot_1.update({"VlanId": str(each.get("vlan_id"))})
472 req_filter_slot = dict([(k, v) for k, v in req_slot_1.items() if v is not None])
473 exist_slot_1 = {"SlotId": exist_filter_slot[0].get("SlotId"),
474 "SlotIPV4Address": exist_filter_slot[0].get("SlotIPV4Address"),
475 "SlotIPV6Address": exist_filter_slot[0].get("SlotIPV6Address")}
476 if "VlanId" in exist_filter_slot[0]:
477 exist_slot_1.update({"VlanId": exist_filter_slot[0]["VlanId"]})
478 else:
479 exist_slot_1.update({"VlanId": ""})
480 exist_filter_slot = dict([(k, v) for k, v in exist_slot_1.items() if v is not None])
481 cp_exist_filter_slot = copy.deepcopy(exist_filter_slot)
482 cp_exist_filter_slot.update(req_filter_slot)
483 diff_changes.append(bool(set(cp_exist_filter_slot.items()) ^ set(exist_filter_slot.items())))
484 req_slot_payload.append(cp_exist_filter_slot)
485 else:
486 invalid_slot.append(each["slot_id"])
487 if invalid_slot:
488 module.exit_json(msg=INVALID_SLOT_MSG.format(", ".join(map(str, invalid_slot))), failed=True)
489 if module.check_mode and any(diff_changes) is True:
490 module.exit_json(msg=CHANGES_FOUND, changed=True, quick_deploy_settings=deploy_data)
491 elif (module.check_mode and any(diff_changes) is False) or \
492 (not module.check_mode and any(diff_changes) is False):
493 module.exit_json(msg=NO_CHANGES_FOUND, quick_deploy_settings=deploy_data)
494 req_payload.update(resp_filter_data)
495 req_payload.update(req_data_filter)
496 update_prefix_length(req_payload)
497 return req_payload, req_slot_payload
498
499
500 def update_ipv4_data(req_data, ipv4_enabled, ipv4_enabled_deploy, ipv4_nt_deploy, deploy_options):
501 if ipv4_enabled is not None and ipv4_enabled is True or \
502 ipv4_enabled_deploy is not None and ipv4_enabled_deploy is True:
503 req_data["ProtocolTypeV4"] = None
504 if ipv4_enabled is not None:
505 req_data["ProtocolTypeV4"] = str(ipv4_enabled).lower()
506 ipv4_network_type = deploy_options.get("ipv4_network_type")
507 req_data["NetworkTypeV4"] = ipv4_network_type
508 if ipv4_network_type == "Static" or ipv4_nt_deploy is not None and ipv4_nt_deploy == "Static":
509 req_data["IpV4SubnetMask"] = deploy_options.get("ipv4_subnet_mask")
510 req_data["IpV4Gateway"] = deploy_options.get("ipv4_gateway")
511 elif ipv4_enabled is not None and ipv4_enabled is False:
512 req_data["ProtocolTypeV4"] = str(ipv4_enabled).lower()
513
514
515 def update_ipv6_data(req_data, ipv6_enabled, ipv6_enabled_deploy, ipv6_nt_deploy, deploy_options):
516 if ipv6_enabled is not None and ipv6_enabled is True or \
517 ipv6_enabled_deploy is not None and ipv6_enabled_deploy is True:
518 req_data["ProtocolTypeV6"] = None
519 if ipv6_enabled is not None:
520 req_data["ProtocolTypeV6"] = str(ipv6_enabled).lower()
521 ipv6_network_type = deploy_options.get("ipv6_network_type")
522 req_data["NetworkTypeV6"] = ipv6_network_type
523 if ipv6_network_type == "Static" or ipv6_nt_deploy is not None and ipv6_nt_deploy == "Static":
524 req_data["PrefixLength"] = deploy_options.get("ipv6_prefix_length")
525 if deploy_options.get("ipv6_prefix_length") is not None:
526 req_data["PrefixLength"] = str(deploy_options.get("ipv6_prefix_length"))
527 req_data["IpV6Gateway"] = deploy_options.get("ipv6_gateway")
528 elif ipv6_enabled is not None and ipv6_enabled is False:
529 req_data["ProtocolTypeV6"] = str(ipv6_enabled).lower()
530
531
532 def update_prefix_length(req_payload):
533 prefix_length = req_payload.get("PrefixLength")
534 if prefix_length == '0':
535 req_payload["PrefixLength"] = ""
536
537
538 def job_payload_submission(rest_obj, payload, slot_payload, settings_type, device_id, resp_data):
539 job_params = []
540 job_params.append({"Key": "protocolTypeV4", "Value": payload["ProtocolTypeV4"]})
541 job_params.append({"Key": "protocolTypeV6", "Value": payload["ProtocolTypeV6"]})
542 s_type = "SERVER_QUICK_DEPLOY" if settings_type == "ServerQuickDeploy" else "IOM_QUICK_DEPLOY"
543 job_params.append({"Key": "operationName", "Value": "{0}".format(s_type)})
544 job_params.append({"Key": "deviceId", "Value": "{0}".format(device_id)})
545 if payload.get("rootCredential") is not None:
546 job_params.append({"Key": "rootCredential", "Value": payload["rootCredential"]})
547 if payload.get("NetworkTypeV4") is not None:
548 job_params.append({"Key": "networkTypeV4", "Value": payload["NetworkTypeV4"]})
549 if payload.get("IpV4SubnetMask") is not None:
550 job_params.append({"Key": "subnetMaskV4", "Value": payload["IpV4SubnetMask"]})
551 if payload.get("IpV4Gateway") is not None:
552 job_params.append({"Key": "gatewayV4", "Value": payload["IpV4Gateway"]})
553 if payload.get("NetworkTypeV6") is not None:
554 job_params.append({"Key": "networkTypeV6", "Value": payload["NetworkTypeV6"]})
555 if payload.get("PrefixLength") is not None:
556 job_params.append({"Key": "prefixLength", "Value": payload["PrefixLength"]})
557 if payload.get("IpV6Gateway") is not None:
558 job_params.append({"Key": "gatewayV6", "Value": payload["IpV6Gateway"]})
559 updated_slot = []
560 if slot_payload:
561 for each in slot_payload:
562 updated_slot.append(each.get("SlotId"))
563 job_params.append(
564 {"Key": "slotId={0}".format(each.get("SlotId")),
565 "Value": "SlotSelected=true;IPV4Address={0};IPV6Address={1};VlanId={2}".format(
566 each.get("SlotIPV4Address"), each.get("SlotIPV6Address"), each.get("VlanId"))})
567 slots = resp_data["Slots"]
568 if updated_slot is not None:
569 slots = list(filter(lambda d: d["SlotId"] not in updated_slot, slots))
570 for each in slots:
571 key = "slot_id={0}".format(each["SlotId"])
572 value = "SlotSelected={0};".format(each["SlotSelected"])
573 if each.get("SlotIPV4Address") is not None:
574 value = value + "IPV4Address={0};".format(each["SlotIPV4Address"])
575 if each.get("SlotIPV6Address") is not None:
576 value = value + "IPV6Address={0};".format(each["SlotIPV6Address"])
577 if each.get("VlanId") is not None:
578 value = value + "VlanId={0}".format(each["VlanId"])
579 job_params.append({"Key": key, "Value": value})
580 job_sub_resp = rest_obj.job_submission("Quick Deploy", QUICK_DEPLOY_JOB_DESC, [], job_params,
581 {"Id": 42, "Name": "QuickDeploy_Task"})
582 return job_sub_resp.json_data.get('Id')
583
584
585 def get_device_details(rest_obj, module):
586 job_success_data, job_id = None, None
587 device_id, tag = module.params.get("device_id"), module.params.get("device_service_tag")
588 if device_id is None and tag is None:
589 key, value = get_chassis_device(module, rest_obj)
590 device_id = value
591 else:
592 key, value = ("Id", device_id) if device_id is not None else ("DeviceServiceTag", tag)
593 param_value = "{0} eq {1}".format(key, value) if key == "Id" else "{0} eq '{1}'".format(key, value)
594 resp = rest_obj.invoke_request("GET", DEVICE_URI, query_param={"$filter": param_value})
595 resp_data = resp.json_data.get("value")
596 rename_key = "id" if key == "Id" else "service tag"
597 if not resp_data:
598 module.exit_json(msg=DEVICE_FAIL_MSG.format(rename_key, value), failed=True)
599 if key == "DeviceServiceTag" and resp_data[0]["DeviceServiceTag"] == tag:
600 device_id = resp_data[0]["Id"]
601 elif key == "Id" and resp_data[0]["Id"] == device_id:
602 device_id = resp_data[0]["Id"]
603 else:
604 module.exit_json(msg=DEVICE_FAIL_MSG.format(rename_key, value), failed=True)
605 settings_type, settings_key = "IOMQuickDeploy", "IOM Quick Deploy"
606 if module.params["setting_type"] == "ServerQuickDeploy":
607 settings_type, settings_key = "ServerQuickDeploy", "Server Quick Deploy"
608 try:
609 deploy_resp = rest_obj.invoke_request("GET", QUICK_DEPLOY_API.format(device_id, settings_type))
610 except HTTPError as err:
611 if err.status == 404:
612 module.exit_json(msg=DEVICE_FAIL_MSG.format(rename_key, value), failed=True)
613 err_message = json.load(err).get("error")
614 error_msg = err_message.get('@Message.ExtendedInfo')
615 if error_msg and error_msg[0].get("MessageId") == "CGEN1004":
616 module.exit_json(msg=QUICK_DEPLOY_FAIL_MSG.format(settings_key), failed=True)
617 else:
618 resp_data = rest_obj.strip_substr_dict(deploy_resp.json_data)
619 payload, slot_payload = check_mode_validation(module, resp_data)
620 job_id = job_payload_submission(rest_obj, payload, slot_payload, settings_type, device_id, resp_data)
621 if module.params["job_wait"]:
622 job_failed, job_msg = rest_obj.job_tracking(job_id, job_wait_sec=module.params["job_wait_timeout"])
623 if job_failed is True:
624 module.exit_json(msg=FAIL_MSG, failed=True)
625 job_success_resp = rest_obj.invoke_request("GET", QUICK_DEPLOY_API.format(device_id, settings_type))
626 job_success_data = rest_obj.strip_substr_dict(job_success_resp.json_data)
627 return job_id, job_success_data
628
629
630 def main():
631 slots = {
632 "slot_id": {"required": True, "type": "int"},
633 "slot_ipv4_address": {"type": "str"},
634 "slot_ipv6_address": {"type": "str"},
635 "vlan_id": {"type": "int"},
636 }
637 quick_deploy = {
638 "password": {"type": "str", "no_log": True},
639 "ipv4_enabled": {"type": "bool"},
640 "ipv4_network_type": {"type": "str", "choices": ["Static", "DHCP"]},
641 "ipv4_subnet_mask": {"type": "str"},
642 "ipv4_gateway": {"type": "str"},
643 "ipv6_enabled": {"type": "bool"},
644 "ipv6_network_type": {"type": "str", "choices": ["Static", "DHCP"]},
645 "ipv6_prefix_length": {"type": "int"},
646 "ipv6_gateway": {"type": "str"},
647 "slots": {"type": "list", "elements": "dict", "options": slots},
648 }
649 specs = {
650 "device_id": {"required": False, "type": "int"},
651 "device_service_tag": {"required": False, "type": "str"},
652 "setting_type": {"required": True, "choices": ["ServerQuickDeploy", "IOMQuickDeploy"]},
653 "quick_deploy_options": {
654 "type": "dict", "required": True, "options": quick_deploy,
655 "required_if": [
656 ["ipv4_enabled", True, ["ipv4_network_type"]],
657 ["ipv4_network_type", "Static", ["ipv4_subnet_mask", "ipv4_gateway"]],
658 ["ipv6_enabled", True, ["ipv6_network_type"]],
659 ["ipv6_network_type", "Static", ["ipv6_prefix_length", "ipv6_gateway"]],
660 ],
661 },
662 "job_wait": {"type": "bool", "default": True},
663 "job_wait_timeout": {"type": "int", "default": 120},
664 }
665
666 module = OmeAnsibleModule(argument_spec=specs,
667 mutually_exclusive=[('device_id', 'device_service_tag')],
668 supports_check_mode=True,)
669 if module.params["quick_deploy_options"] is None:
670 module.exit_json(msg="missing required arguments: quick_deploy_options", failed=True)
671 fields = [("ipv4_subnet_mask", "IPV4"), ("ipv4_gateway", "IPV4"), ("ipv6_gateway", "IPV6")]
672 ip_address_field(module, fields, module.params["quick_deploy_options"], slot=False)
673 slot_options = module.params["quick_deploy_options"].get("slots")
674 if slot_options is not None:
675 slot_field = [("slot_ipv4_address", "IPV4"), ("slot_ipv6_address", "IPV6")]
676 for dep_opt in slot_options:
677 ip_address_field(module, slot_field, dep_opt, slot=True)
678 try:
679 with RestOME(module.params, req_session=True) as rest_obj:
680 check_domain_service(module, rest_obj)
681 job_id, data = get_device_details(rest_obj, module)
682 if job_id is not None and data is not None:
683 module.exit_json(msg=SUCCESS_MSG, job_id=job_id, quick_deploy_settings=data, changed=True)
684 module.exit_json(msg=JOB_MSG, job_id=job_id)
685 except HTTPError as err:
686 module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
687 except URLError as err:
688 module.exit_json(msg=str(err), unreachable=True)
689 except (IOError, ValueError, SSLError, TypeError, ConnectionError,
690 AttributeError, IndexError, KeyError, OSError) as err:
691 module.exit_json(msg=str(err), failed=True)
692
693
694 if __name__ == '__main__':
695 main()