Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_boot_order_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_boot_order_policy
16 short_description: Boot Order policy configuration for Cisco Intersight
17 description:
18 - Boot Order policy configuration for Cisco Intersight.
19 - Used to configure Boot Order servers and timezone settings on Cisco Intersight managed devices.
20 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
21 extends_documentation_fragment: intersight
22 options:
23 state:
24 description:
25 - If C(present), will verify the resource is present and will create if needed.
26 - If C(absent), will verify the resource is absent and will delete if needed.
27 type: str
28 choices: [present, absent]
29 default: present
30 organization:
31 description:
32 - The name of the Organization this resource is assigned to.
33 - Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization.
34 type: str
35 default: default
36 name:
37 description:
38 - The name assigned to the Boot Order policy.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 tags:
43 description:
44 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
45 type: list
46 elements: dict
47 description:
48 description:
49 - The user-defined description of the Boot Order policy.
50 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
51 type: str
52 aliases: [descr]
53 configured_boot_mode:
54 description:
55 - Sets the BIOS boot mode.
56 - UEFI uses the GUID Partition Table (GPT) whereas Legacy mode uses the Master Boot Record (MBR) partitioning scheme.
57 type: str
58 choices: [Legacy, Uefi]
59 default: Legacy
60 uefi_enable_secure_boot:
61 description:
62 - Secure boot enforces that device boots using only software that is trusted by the Original Equipment Manufacturer (OEM).
63 - Option is only used if configured_boot_mode is set to Uefi.
64 type: bool
65 default: false
66 boot_devices:
67 description:
68 - List of Boot Devices configured on the endpoint.
69 type: list
70 elements: dict
71 suboptions:
72 enabled:
73 description:
74 - Specifies if the boot device is enabled or disabled.
75 type: bool
76 default: true
77 device_type:
78 description:
79 - Device type used with this boot option.
80 - Choices are based on each device title in the API schema.
81 type: str
82 choices: [iSCSI, Local CDD, Local Disk, NVMe, PCH Storage, PXE, SAN, SD Card, UEFI Shell, USB, Virtual Media]
83 required: true
84 device_name:
85 description:
86 - A name that helps identify a boot device.
87 - It can be any string that adheres to the following constraints.
88 - It should start and end with an alphanumeric character.
89 - It can have underscores and hyphens.
90 - It cannot be more than 30 characters.
91 type: str
92 required: true
93 network_slot:
94 description:
95 - The slot id of the controller for the iscsi and pxe device.
96 - Option is used when device_type is iscsi and pxe.
97 type: str
98 choices: [1 - 255, MLOM, L, L1, L2, OCP]
99 port:
100 description:
101 - The port id of the controller for the iscsi and pxe device.
102 - Option is used when device_type is iscsi and pxe.
103 - The port id need to be an integer from 0 to 255.
104 type: int
105 controller_slot:
106 description:
107 - The slot id of the controller for the local disk device.
108 - Option is used when device_type is local_disk.
109 type: str
110 choices: [1-255, M, HBA, SAS, RAID, MRAID, MSTOR-RAID]
111 bootloader_name:
112 description:
113 - Details of the bootloader to be used during boot from local disk.
114 - Option is used when device_type is local_disk and configured_boot_mode is Uefi.
115 type: str
116 default: ''
117 bootloader_description:
118 description:
119 - Details of the bootloader to be used during boot from local disk.
120 - Option is used when device_type is local_disk and configured_boot_mode is Uefi.
121 type: str
122 default: ''
123 bootloader_path:
124 description:
125 - Details of the bootloader to be used during boot from local disk.
126 - Option is used when device_type is local_disk and configured_boot_mode is Uefi.
127 type: str
128 default: ''
129 ip_type:
130 description:
131 - The IP Address family type to use during the PXE Boot process.
132 - Option is used when device_type is pxe.
133 type: str
134 choices: [None, IPv4, IPv6]
135 default: None
136 interface_source:
137 description:
138 - Lists the supported Interface Source for PXE device.
139 - Option is used when device_type is pxe.
140 type: str
141 choices: [name, mac, port]
142 default: name
143 interface_name:
144 description:
145 - The name of the underlying virtual ethernet interface used by the PXE boot device and SAN boot device.
146 - Option is used when device_type is pxe interface_source is name or san.
147 type: str
148 mac_address:
149 description:
150 - The MAC Address of the underlying virtual ethernet interface used by the PXE boot device.
151 - Option is used when device_type is pxe and interface_source is mac.
152 type: str
153 sd_card_subtype:
154 description:
155 - The subtype for the selected device type.
156 - Option is used when device_type is sd_card.
157 type: str
158 choices: [None, flex-util, flex-flash, SDCARD]
159 default: None
160 lun:
161 description:
162 - The Logical Unit Number (LUN) of the device.
163 - Option is used when device_type is pch, san and sd_card.
164 - The LUN need to be an integer from 0 to 255.
165 type: int
166 wwpn:
167 description:
168 - The WWPN Address of the underlying fibre channel interface used by the SAN boot device.
169 - Value must be in hexadecimal format xx:xx:xx:xx:xx:xx:xx:xx.
170 - Option is required when device_type is san.
171 type: str
172 usb_subtype:
173 description:
174 - The subtype for the selected device type.
175 - Option is used when device_type is usb.
176 type: str
177 choices: [None, usb-cd, usb-fdd, usb-hdd]
178 default: None
179 virtual_media_subtype:
180 description:
181 - The subtype for the selected device type.
182 - Option is used when device_type is virtual_media.
183 type: str
184 choices: [None, cimc-mapped-dvd, cimc-mapped-hdd, kvm-mapped-dvd, kvm-mapped-hdd, kvm-mapped-fdd]
185 default: None
186 author:
187 - Tse Kai "Kevin" Chan (@BrightScale)
188 '''
189
190 EXAMPLES = r'''
191 - name: Configure Boot Order Policy
192 cisco.intersight.intersight_boot_order_policy:
193 api_private_key: "{{ api_private_key }}"
194 api_key_id: "{{ api_key_id }}"
195 organization: DevNet
196 name: COS-Boot
197 description: Boot Order policy for COS
198 tags:
199 - Key: Site
200 Value: RCDN
201 configured_boot_mode: legacy
202 boot_devices:
203 - device_type: Local Disk
204 device_name: Boot-Lun
205 controller_slot: MRAID
206
207 - name: Configure Boot Order Policy with SAN Boot
208 cisco.intersight.intersight_boot_order_policy:
209 api_private_key: "{{ api_private_key }}"
210 api_key_id: "{{ api_key_id }}"
211 organization: DevNet
212 name: SAN-Boot
213 description: Boot Order policy with SAN boot device
214 configured_boot_mode: Uefi
215 boot_devices:
216 - device_type: SAN
217 device_name: SAN-Primary
218 interface_name: fc0
219 network_slot: MLOM
220 lun: 0
221 wwpn: "50:00:00:25:B5:00:00:01"
222
223 - name: Delete Boot Order Policy
224 cisco.intersight.intersight_boot_policy:
225 api_private_key: "{{ api_private_key }}"
226 api_key_id: "{{ api_key_id }}"
227 organization: DevNet
228 name: COS-Boot
229 state: absent
230 '''
231
232 RETURN = r'''
233 api_response:
234 description: The API response output returned by the specified resource.
235 returned: always
236 type: dict
237 sample:
238 "api_response": {
239 "Name": "COS-Boot",
240 "ObjectType": "boot.Policy",
241 "Tags": [
242 {
243 "Key": "Site",
244 "Value": "RCDN"
245 }
246 ]
247 }
248 '''
249
250
251 from ansible.module_utils.basic import AnsibleModule
252 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
253
254
255 def main():
256 boot_device = dict(
257 enabled=dict(type='bool', default=True),
258 device_type=dict(
259 type='str',
260 choices=[
261 'iSCSI',
262 'Local CDD',
263 'Local Disk',
264 'NVMe',
265 'PCH Storage',
266 'PXE',
267 'SAN',
268 'SD Card',
269 'UEFI Shell',
270 'USB',
271 'Virtual Media',
272 ],
273 required=True,
274 ),
275 device_name=dict(type='str', required=True),
276 # iscsi and pxe options
277 network_slot=dict(type='str', choices=['1 - 255', 'MLOM', 'L', 'L1', 'L2', 'OCP']),
278 port=dict(type='int'),
279 # local disk options
280 controller_slot=dict(type='str', choices=['1-255', 'M', 'HBA', 'SAS', 'RAID', 'MRAID', 'MSTOR-RAID']),
281 # bootloader options
282 bootloader_name=dict(type='str', default=''),
283 bootloader_description=dict(type='str', default=''),
284 bootloader_path=dict(type='str', default=''),
285 # pxe only options
286 ip_type=dict(
287 type='str',
288 choices=[
289 'None',
290 'IPv4',
291 'IPv6'
292 ],
293 default='None'
294 ),
295 interface_source=dict(
296 type='str',
297 choices=[
298 'name',
299 'mac',
300 'port'
301 ],
302 default='name'
303 ),
304 interface_name=dict(type='str'),
305 mac_address=dict(type='str'),
306 # sd card options
307 sd_card_subtype=dict(
308 type='str',
309 choices=[
310 'None',
311 'flex-util',
312 'flex-flash',
313 'SDCARD'
314 ],
315 default='None',
316 ),
317 # lun for pch, san, sd_card
318 lun=dict(type='int'),
319 # san options
320 wwpn=dict(type='str'),
321 # usb options
322 usb_subtype=dict(
323 type='str',
324 choices=[
325 'None',
326 'usb-cd',
327 'usb-fdd',
328 'usb-hdd'
329 ],
330 default='None',
331 ),
332 # virtual media options
333 virtual_media_subtype=dict(
334 type='str',
335 choices=[
336 'None',
337 'cimc-mapped-dvd',
338 'cimc-mapped-hdd',
339 'kvm-mapped-dvd',
340 'kvm-mapped-hdd',
341 'kvm-mapped-fdd'
342 ],
343 default='None',
344 ),
345 )
346 argument_spec = intersight_argument_spec.copy()
347 argument_spec.update(
348 state=dict(type='str', choices=['present', 'absent'], default='present'),
349 organization=dict(type='str', default='default'),
350 name=dict(type='str', required=True),
351 description=dict(type='str', aliases=['descr']),
352 tags=dict(type='list', elements='dict'),
353 configured_boot_mode=dict(type='str', choices=['Legacy', 'Uefi'], default='Legacy'),
354 uefi_enable_secure_boot=dict(type='bool', default=False),
355 boot_devices=dict(type='list', elements='dict', options=boot_device),
356 )
357
358 module = AnsibleModule(
359 argument_spec,
360 supports_check_mode=True,
361 )
362
363 intersight = IntersightModule(module)
364 intersight.result['api_response'] = {}
365 intersight.result['trace_id'] = ''
366
367 # Resource path used to configure policy
368 resource_path = '/boot/PrecisionPolicies'
369 # Define API body used in compares or create
370 intersight.api_body = {
371 'Organization': {
372 'Name': intersight.module.params['organization'],
373 },
374 'Name': intersight.module.params['name'],
375 }
376
377 # Only set these fields when creating or updating (state == present)
378 if intersight.module.params['state'] == 'present':
379 intersight.api_body['ConfiguredBootMode'] = intersight.module.params['configured_boot_mode']
380 intersight.api_body['EnforceUefiSecureBoot'] = intersight.module.params['uefi_enable_secure_boot']
381 intersight.api_body['BootDevices'] = []
382 intersight.set_tags_and_description()
383
384 if intersight.module.params['state'] == 'present' and intersight.module.params.get('boot_devices'):
385 for device in intersight.module.params['boot_devices']:
386 if device['device_type'] == 'iSCSI':
387 iscsi_device = {
388 "ClassId": "boot.Iscsi",
389 "ObjectType": "boot.Iscsi",
390 "Enabled": device['enabled'],
391 "Name": device['device_name'],
392 }
393 if device.get('network_slot'):
394 iscsi_device['Slot'] = device['network_slot']
395 if device.get('port') is not None:
396 iscsi_device['Port'] = device['port']
397 intersight.api_body['BootDevices'].append(iscsi_device)
398 elif device['device_type'] == 'Local CDD':
399 intersight.api_body['BootDevices'].append(
400 {
401 "ClassId": "boot.LocalCdd",
402 "ObjectType": "boot.LocalCdd",
403 "Enabled": device['enabled'],
404 "Name": device['device_name'],
405 }
406 )
407 elif device['device_type'] == 'Local Disk':
408 local_disk_device = {
409 "ClassId": "boot.LocalDisk",
410 "ObjectType": "boot.LocalDisk",
411 "Enabled": device['enabled'],
412 "Name": device['device_name'],
413 }
414 if device.get('controller_slot'):
415 local_disk_device['Slot'] = device['controller_slot']
416 # Only include bootloader if at least one bootloader field is specified
417 if device.get('bootloader_name') or device.get('bootloader_path') or device.get('bootloader_description'):
418 local_disk_device['Bootloader'] = {
419 "ClassId": "boot.Bootloader",
420 "ObjectType": "boot.Bootloader",
421 "Description": device.get('bootloader_description', ''),
422 "Name": device.get('bootloader_name', ''),
423 "Path": device.get('bootloader_path', ''),
424 }
425 intersight.api_body['BootDevices'].append(local_disk_device)
426 elif device['device_type'] == 'NVMe':
427 nvme_device = {
428 "ClassId": "boot.Nvme",
429 "ObjectType": "boot.Nvme",
430 "Enabled": device['enabled'],
431 "Name": device['device_name'],
432 }
433 # Only include bootloader if at least one bootloader field is specified
434 if device.get('bootloader_name') or device.get('bootloader_path') or device.get('bootloader_description'):
435 nvme_device['Bootloader'] = {
436 "ClassId": "boot.Bootloader",
437 "ObjectType": "boot.Bootloader",
438 "Description": device.get('bootloader_description', ''),
439 "Name": device.get('bootloader_name', ''),
440 "Path": device.get('bootloader_path', ''),
441 }
442 intersight.api_body['BootDevices'].append(nvme_device)
443 elif device['device_type'] == 'PCH Storage':
444 pch_storage_device = {
445 "ClassId": "boot.PchStorage",
446 "ObjectType": "boot.PchStorage",
447 "Enabled": device['enabled'],
448 "Name": device['device_name'],
449 }
450 # Only include bootloader if at least one bootloader field is specified
451 if device.get('bootloader_name') or device.get('bootloader_path') or device.get('bootloader_description'):
452 pch_storage_device['Bootloader'] = {
453 "ClassId": "boot.Bootloader",
454 "ObjectType": "boot.Bootloader",
455 "Description": device.get('bootloader_description', ''),
456 "Name": device.get('bootloader_name', ''),
457 "Path": device.get('bootloader_path', ''),
458 }
459 if device.get('lun') is not None:
460 pch_storage_device['Lun'] = device['lun']
461 intersight.api_body['BootDevices'].append(pch_storage_device)
462 elif device['device_type'] == 'PXE':
463 pxe_device = {
464 "ClassId": "boot.Pxe",
465 "ObjectType": "boot.Pxe",
466 "Enabled": device['enabled'],
467 "Name": device['device_name'],
468 "IpType": device['ip_type'],
469 "InterfaceSource": device['interface_source'],
470 }
471 # Add optional fields based on configuration
472 if device.get('network_slot'):
473 pxe_device['Slot'] = device['network_slot']
474 if device.get('port') is not None:
475 pxe_device['Port'] = device['port']
476 # Include InterfaceName only when interface_source is 'name'
477 if device['interface_source'] == 'name' and device.get('interface_name'):
478 pxe_device['InterfaceName'] = device['interface_name']
479 # Include MacAddress only when interface_source is 'mac'
480 if device['interface_source'] == 'mac' and device.get('mac_address'):
481 pxe_device['MacAddress'] = device['mac_address']
482 intersight.api_body['BootDevices'].append(pxe_device)
483 elif device['device_type'] == 'SAN':
484 san_device = {
485 "ClassId": "boot.San",
486 "ObjectType": "boot.San",
487 "Enabled": device['enabled'],
488 "Name": device['device_name'],
489 }
490 if device.get('interface_name'):
491 san_device['InterfaceName'] = device['interface_name']
492 if device.get('lun') is not None:
493 san_device['Lun'] = device['lun']
494 if device.get('network_slot'):
495 san_device['Slot'] = device['network_slot']
496 if device.get('wwpn'):
497 san_device['Wwpn'] = device['wwpn']
498 # Only include bootloader if at least one bootloader field is specified
499 if device.get('bootloader_name') or device.get('bootloader_path') or device.get('bootloader_description'):
500 san_device['Bootloader'] = {
501 "ClassId": "boot.Bootloader",
502 "ObjectType": "boot.Bootloader",
503 "Description": device.get('bootloader_description', ''),
504 "Name": device.get('bootloader_name', ''),
505 "Path": device.get('bootloader_path', ''),
506 }
507 intersight.api_body['BootDevices'].append(san_device)
508 elif device['device_type'] == 'SD Card':
509 sd_card_device = {
510 "ClassId": "boot.SdCard",
511 "ObjectType": "boot.SdCard",
512 "Enabled": device['enabled'],
513 "Name": device['device_name'],
514 }
515 if device.get('lun') is not None:
516 sd_card_device['Lun'] = device['lun']
517 if device.get('sd_card_subtype') and device['sd_card_subtype'] != 'None':
518 sd_card_device['SubType'] = device['sd_card_subtype']
519 # Only include bootloader if at least one bootloader field is specified
520 if device.get('bootloader_name') or device.get('bootloader_path') or device.get('bootloader_description'):
521 sd_card_device['Bootloader'] = {
522 "ClassId": "boot.Bootloader",
523 "ObjectType": "boot.Bootloader",
524 "Description": device.get('bootloader_description', ''),
525 "Name": device.get('bootloader_name', ''),
526 "Path": device.get('bootloader_path', ''),
527 }
528 intersight.api_body['BootDevices'].append(sd_card_device)
529 elif device['device_type'] == 'UEFI Shell':
530 intersight.api_body['BootDevices'].append(
531 {
532 "ClassId": "boot.UefiShell",
533 "ObjectType": "boot.UefiShell",
534 "Enabled": device['enabled'],
535 "Name": device['device_name'],
536 }
537 )
538 elif device['device_type'] == 'USB':
539 usb_device = {
540 "ClassId": "boot.Usb",
541 "ObjectType": "boot.Usb",
542 "Enabled": device['enabled'],
543 "Name": device['device_name'],
544 }
545 if device.get('usb_subtype') and device['usb_subtype'] != 'None':
546 usb_device['SubType'] = device['usb_subtype']
547 intersight.api_body['BootDevices'].append(usb_device)
548 elif device['device_type'] == 'Virtual Media':
549 virtual_media_device = {
550 "ClassId": "boot.VirtualMedia",
551 "ObjectType": "boot.VirtualMedia",
552 "Enabled": device['enabled'],
553 "Name": device['device_name'],
554 }
555 if device.get('virtual_media_subtype') and device['virtual_media_subtype'] != 'None':
556 virtual_media_device['SubType'] = device['virtual_media_subtype']
557 intersight.api_body['BootDevices'].append(virtual_media_device)
558
559 # Configure the policy
560 intersight.configure_policy_or_profile(resource_path=resource_path)
561
562 module.exit_json(**intersight.result)
563
564
565 if __name__ == '__main__':
566 main()