Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_storage_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_storage_policy
16 short_description: Storage Policy configuration for Cisco Intersight
17 description:
18 - Manages Storage Policy configuration on Cisco Intersight.
19 - A policy to configure storage settings and virtual drive configurations for Cisco Intersight managed servers.
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, Policies, and Pools 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 Storage Policy.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 description:
43 description:
44 - The user-defined description for the Storage Policy.
45 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
46 type: str
47 aliases: [descr]
48 tags:
49 description:
50 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
51 type: list
52 elements: dict
53 default: []
54 use_jbod_for_vd_creation:
55 description:
56 - Disks in JBOD State are used to create virtual drives.
57 - This setting must be disabled if Default Drive State is set to JBOD.
58 type: bool
59 default: false
60 unused_disks_state:
61 description:
62 - State to which drives, not used in this policy, are to be moved.
63 - NoChange will not change the drive state.
64 - No Change must be selected if Default Drive State is set to JBOD or RAID0.
65 type: str
66 choices: ['NoChange', 'UnconfiguredGood', 'Jbod']
67 default: 'NoChange'
68 default_drive_mode:
69 description:
70 - All unconfigured drives will move to the selected state on deployment.
71 - Newly inserted drives will move to the selected state.
72 - Select UnconfiguredGood option to retain the existing configuration.
73 - Select Jbod to move the unconfigured drives to JBOD state.
74 - Select RAID0 to create a RAID0 virtual drive on each of the unconfigured drives.
75 type: str
76 choices: ['UnconfiguredGood', 'Jbod', 'RAID0']
77 default: 'UnconfiguredGood'
78 secure_jbods:
79 description:
80 - JBOD drives specified in this slot range will be encrypted.
81 - Allowed values are 'ALL', or a comma or hyphen separated number range.
82 - Sample format is ALL or 1, 3 or 4-6, 8.
83 - Setting the value to 'ALL' will encrypt all the unused UnconfigureGood/JBOD disks.
84 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
85 type: str
86 m2_virtual_drive_config:
87 description:
88 - M.2 RAID virtual drive configuration.
89 type: dict
90 suboptions:
91 enable:
92 description:
93 - Enable M.2 virtual drive configuration.
94 type: bool
95 default: false
96 controller_slot:
97 description:
98 - Slot of the M.2 RAID controller for virtual drive creation.
99 - Select 'MSTOR-RAID-1' to create virtual drive on the M.2 RAID controller in the first slot.
100 - Select 'MSTOR-RAID-2' for second slot.
101 - Select 'MSTOR-RAID-1,MSTOR-RAID-2' for both slots or either slot.
102 type: str
103 choices: ['MSTOR-RAID-2', 'MSTOR-RAID-1', 'MSTOR-RAID-1,MSTOR-RAID-2']
104 default: 'MSTOR-RAID-1'
105 name:
106 description:
107 - The name of the virtual drive.
108 - The name can be between 1 and 15 alphanumeric characters.
109 - Spaces or any special characters other than - (hyphen) and _ (underscore) are not allowed.
110 type: str
111 default: 'MStorBootVd'
112 raid0_drive_config:
113 description:
114 - MRAID/RAID Single Drive RAID0 Configuration.
115 type: dict
116 suboptions:
117 enable:
118 description:
119 - Enable RAID0 drive configuration.
120 type: bool
121 default: false
122 drive_slots:
123 description:
124 - The set of drive slots where RAID0 virtual drives must be created.
125 - If not specified, it will not be added.
126 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
127 type: str
128 strip_size:
129 description:
130 - Desired strip size in KiB.
131 type: int
132 choices: [64, 128, 256, 512, 1024]
133 default: 64
134 access_policy:
135 description:
136 - Access policy that host has on this virtual drive.
137 type: str
138 choices: ['Default', 'ReadWrite', 'ReadOnly', 'Blocked']
139 default: 'Default'
140 read_policy:
141 description:
142 - Read ahead mode to be used to read data from this virtual drive.
143 type: str
144 choices: ['Default', 'ReadAhead', 'NoReadAhead']
145 default: 'Default'
146 write_policy:
147 description:
148 - Write mode to be used to write data to this virtual drive.
149 type: str
150 choices: ['Default', 'WriteThrough', 'WriteBackGoodBbu', 'AlwaysWriteBack']
151 default: 'Default'
152 disk_cache:
153 description:
154 - Disk cache policy for the virtual drive.
155 type: str
156 choices: ['Default', 'NoChange', 'Enable', 'Disable']
157 default: 'Default'
158 controller_attached_nvme_slots:
159 description:
160 - Only U.3 NVMe drives need to be specified, entered slots will be moved to controller attached mode.
161 - Allowed slots are 1-9, 21-24, 101-104.
162 - Allowed value is a comma or hyphen separated number ranges.
163 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
164 type: str
165 direct_attached_nvme_slots:
166 description:
167 - Only U.3 NVMe drives need to be specified, entered slots will be moved to Direct attached mode.
168 - Allowed slots are 1-9, 21-24, 101-104.
169 - Allowed value is a comma or hyphen separated number ranges.
170 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
171 type: str
172 drive_groups:
173 description:
174 - List of drive groups to be created and attached to the storage policy.
175 - Each drive group can contain multiple virtual drives.
176 - Leave empty to create a policy without drive groups for manual configuration later.
177 type: list
178 elements: dict
179 suboptions:
180 name:
181 description:
182 - The name of the drive group.
183 - The name can be between 1 and 64 alphanumeric characters.
184 type: str
185 required: true
186 raid_level:
187 description:
188 - The supported RAID level for the disk group.
189 type: str
190 choices: ['Raid0', 'Raid1', 'Raid5', 'Raid6', 'Raid10', 'Raid50', 'Raid60']
191 default: 'Raid0'
192 secure_drive_group:
193 description:
194 - Enables/disables the drive security on all the drives used in this policy.
195 - This flag just enables the drive security and only after Remote/Manual key setting configured, the actual security will be applied.
196 type: bool
197 default: false
198 dedicated_hot_spares:
199 description:
200 - A collection of drives to be used as hot spares for this Drive Group.
201 - Not applicable for RAID0.
202 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
203 type: str
204 span_groups:
205 description:
206 - List of span groups for the drive group.
207 - Each span group contains drive slots.
208 - Required field - must be provided for all drive groups.
209 - Single span group for RAID0, RAID1, RAID5, RAID6.
210 - Multiple span groups (2-8) for RAID10, RAID50, RAID60.
211 type: list
212 elements: dict
213 required: true
214 suboptions:
215 slots:
216 description:
217 - Drive slots for this span group.
218 - 'Slot format examples: "1,4,5", "2", "1-5", "1,2,6-8"'
219 type: str
220 required: true
221 virtual_drives:
222 description:
223 - List of virtual drives to be created in this drive group.
224 type: list
225 elements: dict
226 suboptions:
227 name:
228 description:
229 - The name of the virtual drive.
230 - The name can be between 1 and 15 alphanumeric characters.
231 - Spaces or any special characters other than - (hyphen) and _ (underscore) are not allowed.
232 type: str
233 required: true
234 size:
235 description:
236 - Virtual drive size in MebiBytes.
237 - Size is mandatory field except when the Expand to Available option is enabled.
238 type: int
239 expand_to_available:
240 description:
241 - Whether to expand the virtual drive to use all available space.
242 type: bool
243 default: false
244 boot_drive:
245 description:
246 - Whether this virtual drive is a boot drive.
247 type: bool
248 default: false
249 strip_size:
250 description:
251 - Desired strip size in KiB.
252 type: int
253 choices: [64, 128, 256, 512, 1024]
254 default: 64
255 access_policy:
256 description:
257 - Access policy that host has on this virtual drive.
258 type: str
259 choices: ['Default', 'ReadWrite', 'ReadOnly', 'Blocked']
260 default: 'Default'
261 read_policy:
262 description:
263 - Read ahead mode to be used to read data from this virtual drive.
264 type: str
265 choices: ['Default', 'ReadAhead', 'NoReadAhead']
266 default: 'Default'
267 write_policy:
268 description:
269 - Write mode to be used to write data to this virtual drive.
270 type: str
271 choices: ['Default', 'WriteThrough', 'WriteBackGoodBbu', 'AlwaysWriteBack']
272 default: 'Default'
273 disk_cache:
274 description:
275 - Disk cache policy for the virtual drive.
276 type: str
277 choices: ['Default', 'NoChange', 'Enable', 'Disable']
278 default: 'Default'
279 state:
280 description:
281 - Whether to create/update or delete the drive group.
282 type: str
283 choices: ['present', 'absent']
284 default: present
285 author:
286 - Ron Gershburg (@rgershbu)
287 '''
288
289 EXAMPLES = r'''
290 - name: Create a basic storage policy
291 cisco.intersight.intersight_storage_policy:
292 api_private_key: "{{ api_private_key }}"
293 api_key_id: "{{ api_key_id }}"
294 name: "basic-storage-policy"
295 description: "Basic storage policy configuration"
296 m2_virtual_drive_config:
297 enable: false
298 raid0_drive_config:
299 enable: false
300 tags:
301 - Key: "Environment"
302 Value: "Production"
303
304 - name: Create storage policy with M.2 virtual drive enabled
305 cisco.intersight.intersight_storage_policy:
306 api_private_key: "{{ api_private_key }}"
307 api_key_id: "{{ api_key_id }}"
308 name: "m2-enabled-storage-policy"
309 description: "Storage policy with M.2 virtual drive"
310 m2_virtual_drive_config:
311 enable: true
312 controller_slot: "MSTOR-RAID-1"
313 name: "MStorBootVd"
314 raid0_drive_config:
315 enable: false
316 tags:
317 - Key: "Site"
318 Value: "Datacenter1"
319
320 - name: Create storage policy with RAID0 configuration
321 cisco.intersight.intersight_storage_policy:
322 api_private_key: "{{ api_private_key }}"
323 api_key_id: "{{ api_key_id }}"
324 name: "raid0-storage-policy"
325 description: "Storage policy with RAID0 configuration"
326 use_jbod_for_vd_creation: true
327 default_drive_mode: "RAID0"
328 secure_jbods: "1"
329 m2_virtual_drive_config:
330 enable: false
331 raid0_drive_config:
332 enable: true
333 strip_size: 128
334 access_policy: "ReadWrite"
335 read_policy: "ReadAhead"
336 write_policy: "WriteBackGoodBbu"
337 disk_cache: "Enable"
338 controller_attached_nvme_slots: "2"
339 direct_attached_nvme_slots: "3"
340
341 - name: Create storage policy with drive groups
342 cisco.intersight.intersight_storage_policy:
343 api_private_key: "{{ api_private_key }}"
344 api_key_id: "{{ api_key_id }}"
345 name: "drive-group-storage-policy"
346 description: "Storage policy with drive groups"
347 m2_virtual_drive_config:
348 enable: false
349 raid0_drive_config:
350 enable: false
351 drive_groups:
352 - name: "raid0-group"
353 raid_level: "Raid0"
354 secure_drive_group: false
355 span_groups:
356 - slots: "1,2"
357 virtual_drives:
358 - name: "raid0-vd"
359 size: 1024
360 expand_to_available: false
361 boot_drive: true
362 strip_size: 128
363 access_policy: "ReadWrite"
364 read_policy: "ReadAhead"
365 write_policy: "WriteBackGoodBbu"
366 disk_cache: "Enable"
367 - name: "raid1-group"
368 raid_level: "Raid1"
369 secure_drive_group: true
370 dedicated_hot_spares: "3,4"
371 span_groups:
372 - slots: "5,6"
373 virtual_drives:
374 - name: "raid1-vd"
375 size: 0
376 expand_to_available: true
377 boot_drive: false
378 strip_size: 64
379 access_policy: "Default"
380 read_policy: "Default"
381 write_policy: "Default"
382 disk_cache: "Default"
383
384 - name: Create storage policy with nested RAID drive groups
385 cisco.intersight.intersight_storage_policy:
386 api_private_key: "{{ api_private_key }}"
387 api_key_id: "{{ api_key_id }}"
388 name: "nested-raid-storage-policy"
389 description: "Storage policy with nested RAID configurations"
390 m2_virtual_drive_config:
391 enable: false
392 raid0_drive_config:
393 enable: false
394 drive_groups:
395 - name: "raid60-group"
396 raid_level: "Raid60"
397 secure_drive_group: false
398 dedicated_hot_spares: "33,34,35,36"
399 span_groups:
400 - slots: "1,2,3,4"
401 - slots: "5,6,7,8"
402 - slots: "9,10,11,12"
403 - slots: "13,14,15,16"
404 - slots: "17,18,19,20"
405 - slots: "21,22,23,24"
406 - slots: "25,26,27,28"
407 - slots: "29,30,31,32"
408 virtual_drives:
409 - name: "raid60-vd1"
410 size: 1024
411 expand_to_available: false
412 boot_drive: true
413 strip_size: 64
414 access_policy: "ReadWrite"
415 read_policy: "Default"
416 write_policy: "Default"
417 disk_cache: "Default"
418 - name: "raid60-vd2"
419 size: 1024
420 expand_to_available: false
421 boot_drive: false
422 strip_size: 64
423 access_policy: "ReadWrite"
424 read_policy: "Default"
425 write_policy: "Default"
426 disk_cache: "Default"
427
428 - name: Delete a storage policy
429 cisco.intersight.intersight_storage_policy:
430 api_private_key: "{{ api_private_key }}"
431 api_key_id: "{{ api_key_id }}"
432 name: "old-storage-policy"
433 state: absent
434 '''
435
436 RETURN = r'''
437 api_response:
438 description: The API response output returned by the specified resource.
439 returned: always
440 type: dict
441 sample:
442 "api_response": {
443 "Name": "basic-storage-policy",
444 "UseJbodForVdCreation": true,
445 "UnusedDisksState": "NoChange",
446 "DefaultDriveMode": "UnconfiguredGood",
447 "M2VirtualDrive": {
448 "Enable": false,
449 "ControllerSlot": "MSTOR-RAID-1",
450 "Name": "MStorBootVd"
451 },
452 "Raid0Drive": {
453 "Enable": false,
454 "VirtualDrivePolicy": {
455 "StripSize": 64,
456 "AccessPolicy": "Default",
457 "ReadPolicy": "Default",
458 "WritePolicy": "Default",
459 "DriveCache": "Default"
460 }
461 },
462 "ObjectType": "storage.StoragePolicy",
463 "Tags": [
464 {
465 "Key": "Environment",
466 "Value": "Production"
467 }
468 ]
469 }
470 '''
471
472
473 from ansible.module_utils.basic import AnsibleModule
474 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
475
476
477 # Virtual drive policy specification
478 VIRTUAL_DRIVE_POLICY_SPEC = {
479 'strip_size': dict(type='int', choices=[64, 128, 256, 512, 1024], default=64),
480 'access_policy': dict(type='str', choices=['Default', 'ReadWrite', 'ReadOnly', 'Blocked'], default='Default'),
481 'read_policy': dict(type='str', choices=['Default', 'ReadAhead', 'NoReadAhead'], default='Default'),
482 'write_policy': dict(type='str', choices=['Default', 'WriteThrough', 'WriteBackGoodBbu', 'AlwaysWriteBack'], default='Default'),
483 'disk_cache': dict(type='str', choices=['Default', 'NoChange', 'Enable', 'Disable'], default='Default')
484 }
485
486
487 def build_virtual_drive_policy(virtual_drive_config):
488 """
489 Build VirtualDrivePolicy from virtual drive configuration.
490 Args:
491 virtual_drive_config: Dictionary containing virtual drive policy parameters
492 Returns:
493 Dictionary representing VirtualDrivePolicy
494 """
495 return {
496 'StripSize': virtual_drive_config.get('strip_size', 64),
497 'AccessPolicy': virtual_drive_config.get('access_policy', 'Default'),
498 'ReadPolicy': virtual_drive_config.get('read_policy', 'Default'),
499 'WritePolicy': virtual_drive_config.get('write_policy', 'Default'),
500 'DriveCache': virtual_drive_config.get('disk_cache', 'Default')
501 }
502
503
504 def validate_input(module):
505 """
506 Validate input parameters for the storage policy module.
507 """
508 # Validate UseJbodForVdCreation vs DefaultDriveMode compatibility
509 use_jbod = module.params.get('use_jbod_for_vd_creation')
510 default_drive_mode = module.params.get('default_drive_mode')
511 unused_disks_state = module.params.get('unused_disks_state')
512
513 # UseJbodForVdCreation must be disabled if DefaultDriveMode is JBOD
514 if default_drive_mode == 'Jbod' and use_jbod:
515 module.fail_json(msg="use_jbod_for_vd_creation must be disabled when default_drive_mode is set to 'Jbod'")
516
517 # UnusedDisksState must be NoChange if DefaultDriveMode is JBOD or RAID0
518 if default_drive_mode in ['Jbod', 'RAID0'] and unused_disks_state != 'NoChange':
519 module.fail_json(msg="unused_disks_state must be set to 'NoChange' when default_drive_mode is 'Jbod' or 'RAID0'")
520
521 # Validate M.2 virtual drive configuration
522 m2_config = module.params.get('m2_virtual_drive_config')
523 if m2_config and m2_config.get('enable'):
524 if not m2_config.get('controller_slot'):
525 module.fail_json(msg="controller_slot is required when M.2 virtual drive is enabled")
526 if not m2_config.get('name'):
527 module.fail_json(msg="name is required when M.2 virtual drive is enabled")
528
529 # Validate name format
530 name = m2_config.get('name')
531 if len(name) > 15 or len(name) < 1:
532 module.fail_json(msg="M.2 virtual drive name must be between 1 and 15 characters")
533
534 # Validate drive groups configuration
535 drive_groups = module.params.get('drive_groups', [])
536 if drive_groups:
537 for drive_group in drive_groups:
538 # Validate drive group name
539 name = drive_group.get('name', '')
540 if len(name) > 64 or len(name) < 1:
541 module.fail_json(msg="Drive group name must be between 1 and 64 characters")
542
543 # Validate dedicated hot spares for RAID0
544 raid_level = drive_group.get('raid_level')
545 dedicated_hot_spares = drive_group.get('dedicated_hot_spares')
546 if raid_level == 'Raid0' and dedicated_hot_spares:
547 module.fail_json(msg="Dedicated hot spares are not applicable for RAID0")
548
549 # Validate span groups requirements based on RAID level
550 span_groups = drive_group.get('span_groups', [])
551 if not span_groups:
552 module.fail_json(msg="span_groups is required for drive groups")
553
554 # Validate span group count based on RAID level
555 span_count = len(span_groups)
556 if raid_level in ['Raid10', 'Raid50', 'Raid60']:
557 if span_count > 8:
558 module.fail_json(msg=f"RAID level {raid_level} supports maximum 8 span groups, got {span_count}")
559 if span_count < 2:
560 module.fail_json(msg=f"RAID level {raid_level} requires minimum 2 span groups, got {span_count}")
561 elif raid_level in ['Raid0', 'Raid1', 'Raid5', 'Raid6']:
562 if span_count != 1:
563 module.fail_json(msg=f"RAID level {raid_level} requires exactly 1 span group, got {span_count}")
564
565 # Validate virtual drives
566 virtual_drives = drive_group.get('virtual_drives', [])
567 for virtual_drive in virtual_drives:
568 # Validate virtual drive name
569 vd_name = virtual_drive.get('name', '')
570 if len(vd_name) > 15 or len(vd_name) < 1:
571 module.fail_json(msg="Virtual drive name must be between 1 and 15 characters")
572
573 # Validate size requirement
574 size = virtual_drive.get('size')
575 expand_to_available = virtual_drive.get('expand_to_available', False)
576 if not expand_to_available and not size:
577 module.fail_json(msg="Virtual drive size is mandatory except when expand_to_available is enabled")
578
579
580 def main():
581 argument_spec = intersight_argument_spec.copy()
582 argument_spec.update(
583 state=dict(type='str', choices=['present', 'absent'], default='present'),
584 organization=dict(type='str', default='default'),
585 name=dict(type='str', required=True),
586 description=dict(type='str', aliases=['descr']),
587 tags=dict(type='list', elements='dict', default=[]),
588 use_jbod_for_vd_creation=dict(type='bool', default=False),
589 unused_disks_state=dict(type='str', choices=['NoChange', 'UnconfiguredGood', 'Jbod'], default='NoChange'),
590 default_drive_mode=dict(type='str', choices=['UnconfiguredGood', 'Jbod', 'RAID0'], default='UnconfiguredGood'),
591 secure_jbods=dict(type='str'),
592 m2_virtual_drive_config=dict(
593 type='dict',
594 required=False,
595 options=dict(
596 enable=dict(type='bool', default=False),
597 controller_slot=dict(type='str',
598 choices=['MSTOR-RAID-2', 'MSTOR-RAID-1', 'MSTOR-RAID-1,MSTOR-RAID-2'],
599 default='MSTOR-RAID-1'),
600 name=dict(type='str', default='MStorBootVd')
601 ),
602 ),
603 raid0_drive_config=dict(
604 type='dict',
605 required=False,
606 options=dict(
607 enable=dict(type='bool', default=False),
608 drive_slots=dict(type='str'),
609 strip_size=VIRTUAL_DRIVE_POLICY_SPEC['strip_size'],
610 access_policy=VIRTUAL_DRIVE_POLICY_SPEC['access_policy'],
611 read_policy=VIRTUAL_DRIVE_POLICY_SPEC['read_policy'],
612 write_policy=VIRTUAL_DRIVE_POLICY_SPEC['write_policy'],
613 disk_cache=VIRTUAL_DRIVE_POLICY_SPEC['disk_cache']
614 ),
615 ),
616 controller_attached_nvme_slots=dict(type='str'),
617 direct_attached_nvme_slots=dict(type='str'),
618 drive_groups=dict(
619 type='list',
620 elements='dict',
621 options=dict(
622 name=dict(type='str', required=True),
623 raid_level=dict(type='str', choices=['Raid0', 'Raid1', 'Raid5', 'Raid6', 'Raid10', 'Raid50', 'Raid60'], default='Raid0'),
624 secure_drive_group=dict(type='bool', default=False),
625 dedicated_hot_spares=dict(type='str'),
626 span_groups=dict(
627 type='list',
628 elements='dict',
629 required=True,
630 options=dict(
631 slots=dict(type='str', required=True)
632 )
633 ),
634 virtual_drives=dict(
635 type='list',
636 elements='dict',
637 options=dict(
638 name=dict(type='str', required=True),
639 size=dict(type='int'),
640 expand_to_available=dict(type='bool', default=False),
641 boot_drive=dict(type='bool', default=False),
642 strip_size=VIRTUAL_DRIVE_POLICY_SPEC['strip_size'],
643 access_policy=VIRTUAL_DRIVE_POLICY_SPEC['access_policy'],
644 read_policy=VIRTUAL_DRIVE_POLICY_SPEC['read_policy'],
645 write_policy=VIRTUAL_DRIVE_POLICY_SPEC['write_policy'],
646 disk_cache=VIRTUAL_DRIVE_POLICY_SPEC['disk_cache']
647 )
648 ),
649 state=dict(type='str', choices=['present', 'absent'], default='present')
650 )
651 ),
652 )
653
654 required_if = [
655 ['state', 'present', ['m2_virtual_drive_config', 'raid0_drive_config']],
656 ]
657
658 module = AnsibleModule(
659 argument_spec,
660 required_if=required_if,
661 supports_check_mode=True,
662 )
663
664 if module.params['state'] == 'present':
665 validate_input(module)
666
667 intersight = IntersightModule(module)
668 intersight.result['api_response'] = {}
669 intersight.result['trace_id'] = ''
670
671 # Resource path used to configure policy
672 resource_path = '/storage/StoragePolicies'
673
674 # Define API body used in compares or create
675 intersight.api_body = {
676 'Organization': {
677 'Name': intersight.module.params['organization'],
678 },
679 'Name': intersight.module.params['name']
680 }
681
682 if module.params['state'] == 'present':
683 intersight.set_tags_and_description()
684
685 # Set basic storage configuration
686 intersight.api_body['UseJbodForVdCreation'] = intersight.module.params['use_jbod_for_vd_creation']
687 intersight.api_body['UnusedDisksState'] = intersight.module.params['unused_disks_state']
688 intersight.api_body['DefaultDriveMode'] = intersight.module.params['default_drive_mode']
689
690 # Set secure JBODs if specified
691 if module.params['secure_jbods']:
692 intersight.api_body['SecureJbods'] = module.params['secure_jbods']
693
694 # Set M.2 virtual drive configuration
695 m2_config = module.params.get('m2_virtual_drive_config', {})
696 intersight.api_body['M2VirtualDrive'] = {
697 'Enable': m2_config.get('enable', False)
698 }
699 if m2_config.get('enable'):
700 intersight.api_body['M2VirtualDrive']['ControllerSlot'] = m2_config.get('controller_slot', 'MSTOR-RAID-1')
701 intersight.api_body['M2VirtualDrive']['Name'] = m2_config.get('name', 'MStorBootVd')
702
703 # Set RAID0 drive configuration
704 raid0_config = module.params.get('raid0_drive_config', {})
705 intersight.api_body['Raid0Drive'] = {
706 'Enable': raid0_config.get('enable', False)
707 }
708 if raid0_config.get('enable'):
709 if raid0_config.get('drive_slots'):
710 intersight.api_body['Raid0Drive']['DriveSlots'] = raid0_config['drive_slots']
711
712 # Set virtual drive policy using the common function
713 intersight.api_body['Raid0Drive']['VirtualDrivePolicy'] = build_virtual_drive_policy(raid0_config)
714
715 # Set NVMe slot configurations
716 if module.params['controller_attached_nvme_slots']:
717 intersight.api_body['ControllerAttachedNvmeSlots'] = module.params['controller_attached_nvme_slots']
718
719 if module.params['direct_attached_nvme_slots']:
720 intersight.api_body['DirectAttachedNvmeSlots'] = module.params['direct_attached_nvme_slots']
721
722 intersight.configure_policy_or_profile(resource_path)
723
724 # Save the storage policy response
725 storage_policy_response = intersight.result['api_response']
726 storage_policy_moid = None
727 if module.params['state'] == 'present' and storage_policy_response:
728 storage_policy_moid = storage_policy_response.get('Moid')
729
730 # Process drive groups if provided
731 drive_groups_response = []
732 if module.params['state'] == 'present' and module.params['drive_groups']:
733 for drive_group_config in module.params['drive_groups']:
734 # Build drive group API body
735 drive_group_api_body = {
736 'Name': drive_group_config['name'],
737 'RaidLevel': drive_group_config['raid_level'],
738 'SecureDriveGroup': drive_group_config.get('secure_drive_group', False),
739 'Type': 0, # Manual drive group
740 'StoragePolicy': storage_policy_moid
741 }
742
743 # Build ManualDriveGroup
744 manual_drive_group = {}
745
746 # Add dedicated hot spares if specified (not for RAID0)
747 dedicated_hot_spares = drive_group_config.get('dedicated_hot_spares')
748 if dedicated_hot_spares and drive_group_config['raid_level'] != 'Raid0':
749 manual_drive_group['DedicatedHotSpares'] = dedicated_hot_spares
750
751 # Build span groups (required field)
752 span_groups = drive_group_config.get('span_groups', [])
753 # Transform lowercase 'slots' to uppercase 'Slots' for API
754 api_span_groups = []
755 for span_group in span_groups:
756 api_span_groups.append({'Slots': span_group['slots']})
757 manual_drive_group['SpanGroups'] = api_span_groups
758
759 drive_group_api_body['ManualDriveGroup'] = manual_drive_group
760
761 # Build virtual drives
762 virtual_drives = []
763 for virtual_drive_config in drive_group_config.get('virtual_drives', []):
764 virtual_drive = {
765 'Name': virtual_drive_config['name'],
766 'ExpandToAvailable': virtual_drive_config.get('expand_to_available', False),
767 'BootDrive': virtual_drive_config.get('boot_drive', False),
768 'VirtualDrivePolicy': build_virtual_drive_policy(virtual_drive_config)
769 }
770
771 # Add size if specified and not expanding to available
772 if not virtual_drive_config.get('expand_to_available', False):
773 virtual_drive['Size'] = virtual_drive_config.get('size', 0)
774
775 virtual_drives.append(virtual_drive)
776
777 drive_group_api_body['VirtualDrives'] = virtual_drives
778
779 # Create the drive group
780 resource_path = '/storage/DriveGroups'
781 intersight.api_body = drive_group_api_body
782 # Filter by both DriveGroup name AND StoragePolicy to avoid affecting DriveGroups in other policies
783 custom_filter = f"Name eq '{drive_group_config['name']}' and StoragePolicy.Moid eq '{storage_policy_moid}'"
784 intersight.configure_secondary_resource(
785 resource_path=resource_path,
786 state=drive_group_config.get('state', 'present'),
787 custom_filter=custom_filter
788 )
789
790 # Save the drive group response
791 drive_groups_response.append(intersight.result['api_response'])
792
793 # Combine storage policy and drive groups in the main response
794 if storage_policy_response:
795 storage_policy_response['DriveGroups'] = drive_groups_response
796 intersight.result['api_response'] = storage_policy_response
797
798 module.exit_json(**intersight.result)
799
800
801 if __name__ == '__main__':
802 main()