Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_disk_group_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
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14 DOCUMENTATION = r'''
15 ---
16 module: ucs_disk_group_policy
17 short_description: Configures disk group policies on Cisco UCS Manager
18 description:
19 - Configures disk group policies on Cisco UCS Manager.
20 extends_documentation_fragment: cisco.ucs.ucs
21 options:
22 state:
23 description:
24 - Desired state of the disk group policy.
25 - If C(present), will verify that the disk group policy is present and will create if needed.
26 - If C(absent), will verify that the disk group policy is absent and will delete if needed.
27 choices: [present, absent]
28 default: present
29 type: str
30 name:
31 description:
32 - The name of the disk group policy.
33 - This name can be between 1 and 16 alphanumeric characters.
34 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
35 - You cannot change this name after the policy is created.
36 required: yes
37 type: str
38 description:
39 description:
40 - The user-defined description of the storage profile.
41 - Enter up to 256 characters.
42 - "You can use any characters or spaces except the following:"
43 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
44 aliases: [ descr ]
45 type: str
46 raid_level:
47 description:
48 - "The RAID level for the disk group policy. This can be one of the following:"
49 - "stripe - UCS Manager shows RAID 0 Striped"
50 - "mirror - RAID 1 Mirrored"
51 - "mirror-stripe - RAID 10 Mirrored and Striped"
52 - "stripe-parity - RAID 5 Striped Parity"
53 - "stripe-dual-parity - RAID 6 Striped Dual Parity"
54 - "stripe-parity-stripe - RAID 50 Striped Parity and Striped"
55 - "stripe-dual-parity-stripe - RAID 60 Striped Dual Parity and Striped"
56 choices: [stripe, mirror, mirror-stripe, stripe-parity, stripe-dual-parity, stripe-parity-stripe, stripe-dual-parity-stripe]
57 default: stripe
58 type: str
59 configuration_mode:
60 description:
61 - "Disk group configuration mode. Choose one of the following:"
62 - "automatic - Automatically configures the disks in the disk group."
63 - "manual - Enables you to manually configure the disks in the disk group."
64 choices: [automatic, manual]
65 default: automatic
66 type: str
67 num_drives:
68 description:
69 - Specify the number of drives for the disk group.
70 - This can be from 0 to 24.
71 - Option only applies when configuration mode is automatic.
72 default: '1'
73 type: str
74 drive_type:
75 description:
76 - Specify the drive type to use in the drive group.
77 - "This can be one of the following:"
78 - "unspecified — Selects the first available drive type, and applies that to all drives in the group."
79 - "HDD — Hard disk drive"
80 - "SSD — Solid state drive"
81 - Option only applies when configuration mode is automatic.
82 choices: [unspecified, HDD, SSD]
83 default: unspecified
84 type: str
85 num_ded_hot_spares:
86 description:
87 - Specify the number of hot spares for the disk group.
88 - This can be from 0 to 24.
89 - Option only applies when configuration mode is automatic.
90 default: unspecified
91 type: str
92 num_glob_hot_spares:
93 description:
94 - Specify the number of global hot spares for the disk group.
95 - This can be from 0 to 24.
96 - Option only applies when configuration mode is automatic.
97 default: unspecified
98 type: str
99 min_drive_size:
100 description:
101 - Specify the minimum drive size or unspecified to allow all drive sizes.
102 - This can be from 0 to 10240 GB.
103 - Option only applies when configuration mode is automatic.
104 default: 'unspecified'
105 type: str
106 use_remaining_disks:
107 description:
108 - Specifies whether you can use all the remaining disks in the disk group or not.
109 - Option only applies when configuration mode is automatic.
110 choices: ['yes', 'no']
111 default: 'no'
112 type: str
113 manual_disks:
114 description:
115 - List of manually configured disks.
116 - Options are only used when you choose manual configuration_mode.
117 type: list
118 elements: dict
119 suboptions:
120 slot_num:
121 description:
122 - The slot number of the specific disk.
123 type: str
124 required: yes
125 role:
126 description:
127 - "The role of the disk. This can be one of the following:"
128 - "normal - Normal"
129 - "ded-hot-spare - Dedicated Hot Spare"
130 - "glob-hot-spare - Glob Hot Spare"
131 choices: [normal, ded-hot-spare, glob-hot-spare]
132 default: normal
133 type: str
134 span_id:
135 description:
136 - The Span ID of the specific disk.
137 default: 'unspecified'
138 type: str
139 state:
140 description:
141 - If C(present), will verify disk slot is configured within policy.
142 If C(absent), will verify disk slot is absent from policy.
143 choices: [ present, absent ]
144 default: present
145 type: str
146 virtual_drive:
147 description:
148 - Configuration of virtual drive options.
149 type: dict
150 suboptions:
151 access_policy:
152 description:
153 - Configure access policy to virtual drive.
154 choices: [blocked, hidden, platform-default, read-only, read-write, transport-ready]
155 default: platform-default
156 type: str
157 drive_cache:
158 description:
159 - Configure drive caching.
160 choices: [disable, enable, no-change, platform-default]
161 default: platform-default
162 type: str
163 io_policy:
164 description:
165 - Direct or Cached IO path.
166 choices: [cached, direct, platform-default]
167 default: platform-default
168 type: str
169 read_policy:
170 description:
171 - Read access policy to virtual drive.
172 choices: [normal, platform-default, read-ahead]
173 default: platform-default
174 type: str
175 strip_size:
176 description:
177 - Virtual drive strip size.
178 choices: [1024KB, 128KB, 16KB, 256KB, 32KB, 512KB, 64KB, 8KB, platform-default]
179 default: platform-default
180 type: str
181 write_cache_policy:
182 description:
183 - Write back cache policy.
184 choices: [always-write-back, platform-default, write-back-good-bbu, write-through]
185 default: platform-default
186 type: str
187 org_dn:
188 description:
189 - The distinguished name (dn) of the organization where the resource is assigned.
190 default: org-root
191 type: str
192 requirements:
193 - ucsmsdk
194 author:
195 - Sindhu Sudhir (@sisudhir)
196 - David Soper (@dsoper2)
197 - CiscoUcs (@CiscoUcs)
198 - Brett Johnson (@sdbrett)
199 - John McDonough (@movinalot)
200 '''
201
202 EXAMPLES = r'''
203 - name: Configure Disk Group Policy
204 cisco.ucs.ucs_disk_group_policy:
205 hostname: 172.16.143.150
206 username: admin
207 password: password
208 name: DEE-DG
209 raid_level: mirror
210 configuration_mode: manual
211 manual_disks:
212 - slot_num: '1'
213 role: normal
214 - slot_num: '2'
215 role: normal
216
217 - name: Remove Disk Group Policy
218 cisco.ucs.ucs_disk_group_policy:
219 name: DEE-DG
220 hostname: 172.16.143.150
221 username: admin
222 password: password
223 state: absent
224
225 - name: Remove Disk from Policy
226 cisco.ucs.ucs_disk_group_policy:
227 hostname: 172.16.143.150
228 username: admin
229 password: password
230 name: DEE-DG
231 description: Testing Ansible
232 raid_level: stripe
233 configuration_mode: manual
234 manual_disks:
235 - slot_num: '1'
236 role: normal
237 - slot_num: '2'
238 role: normal
239 state: absent
240 virtual_drive:
241 access_policy: platform-default
242 io_policy: direct
243 strip_size: 64KB
244 '''
245
246 RETURN = r'''
247 #
248 '''
249
250
251 from ansible.module_utils.basic import AnsibleModule
252 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
253
254
255 def configure_disk_policy(ucs, module, dn):
256 from ucsmsdk.mometa.lstorage.LstorageDiskGroupConfigPolicy import LstorageDiskGroupConfigPolicy
257 from ucsmsdk.mometa.lstorage.LstorageDiskGroupQualifier import LstorageDiskGroupQualifier
258 from ucsmsdk.mometa.lstorage.LstorageLocalDiskConfigRef import LstorageLocalDiskConfigRef
259
260 if not module.check_mode:
261 try:
262 # create if mo does not already exist
263 mo = LstorageDiskGroupConfigPolicy(
264 parent_mo_or_dn=module.params['org_dn'],
265 name=module.params['name'],
266 descr=module.params['description'],
267 raid_level=module.params['raid_level'],
268 )
269 if module.params['configuration_mode'] == 'automatic':
270 LstorageDiskGroupQualifier(
271 parent_mo_or_dn=mo,
272 num_drives=module.params['num_drives'],
273 drive_type=module.params['drive_type'],
274 use_remaining_disks=module.params['use_remaining_disks'],
275 num_ded_hot_spares=module.params['num_ded_hot_spares'],
276 num_glob_hot_spares=module.params['num_glob_hot_spares'],
277 min_drive_size=module.params['min_drive_size'],
278 )
279 else: # configuration_mode == 'manual'
280 for disk in module.params['manual_disks']:
281 if disk['state'] == 'absent':
282 child_dn = dn + '/slot-' + disk['slot_num']
283 mo_1 = ucs.login_handle.query_dn(child_dn)
284 if mo_1:
285 ucs.login_handle.remove_mo(mo_1)
286 else: # state == 'present'
287 LstorageLocalDiskConfigRef(
288 parent_mo_or_dn=mo,
289 slot_num=disk['slot_num'],
290 role=disk['role'],
291 span_id=disk['span_id'],
292 )
293
294 if module.params['virtual_drive']:
295 _configure_virtual_drive(module, mo)
296
297 ucs.login_handle.add_mo(mo, True)
298 ucs.login_handle.commit()
299 except Exception as e: # generic Exception handling because SDK can throw a variety
300 ucs.result['msg'] = "setup error: %s " % str(e)
301 module.fail_json(**ucs.result)
302
303 ucs.result['changed'] = True
304
305
306 def check_disk_policy_props(ucs, module, mo, dn):
307 props_match = True
308
309 # check top-level mo props
310 kwargs = dict(descr=module.params['description'])
311 kwargs['raid_level'] = module.params['raid_level']
312 if mo.check_prop_match(**kwargs):
313 # top-level props match, check next level mo/props
314 if module.params['configuration_mode'] == 'automatic':
315 child_dn = dn + '/disk-group-qual'
316 mo_1 = ucs.login_handle.query_dn(child_dn)
317 if mo_1:
318 kwargs = dict(num_drives=module.params['num_drives'])
319 kwargs['drive_type'] = module.params['drive_type']
320 kwargs['use_remaining_disks'] = module.params['use_remaining_disks']
321 kwargs['num_ded_hot_spares'] = module.params['num_ded_hot_spares']
322 kwargs['num_glob_hot_spares'] = module.params['num_glob_hot_spares']
323 kwargs['min_drive_size'] = module.params['min_drive_size']
324 props_match = mo_1.check_prop_match(**kwargs)
325
326 else: # configuration_mode == 'manual'
327 for disk in module.params['manual_disks']:
328 child_dn = dn + '/slot-' + disk['slot_num']
329 mo_1 = ucs.login_handle.query_dn(child_dn)
330 if mo_1:
331 if disk['state'] == 'absent':
332 props_match = False
333 else: # state == 'present'
334 kwargs = dict(slot_num=disk['slot_num'])
335 kwargs['role'] = disk['role']
336 kwargs['span_id'] = disk['span_id']
337 if not mo_1.check_prop_match(**kwargs):
338 props_match = False
339 break
340 if props_match:
341 if module.params['virtual_drive']:
342 props_match = check_virtual_drive_props(ucs, module, dn)
343 else:
344 props_match = False
345 return props_match
346
347
348 def check_virtual_drive_props(ucs, module, dn):
349 child_dn = dn + '/virtual-drive-def'
350 mo_1 = ucs.login_handle.query_dn(child_dn)
351 return mo_1.check_prop_match(**module.params['virtual_drive'])
352
353
354 def _configure_virtual_drive(module, mo):
355 from ucsmsdk.mometa.lstorage.LstorageVirtualDriveDef import LstorageVirtualDriveDef
356
357 LstorageVirtualDriveDef(parent_mo_or_dn=mo, **module.params['virtual_drive'])
358
359
360 def _virtual_drive_argument_spec():
361 return dict(
362 access_policy=dict(type='str', default='platform-default',
363 choices=["blocked", "hidden", "platform-default", "read-only", "read-write",
364 "transport-ready"]),
365 drive_cache=dict(type='str', default='platform-default',
366 choices=["disable", "enable", "no-change", "platform-default"]),
367 io_policy=dict(type='str', default='platform-default',
368 choices=["cached", "direct", "platform-default"]),
369 read_policy=dict(type='str', default='platform-default',
370 choices=["normal", "platform-default", "read-ahead"]),
371 strip_size=dict(type='str', default='platform-default',
372 choices=["1024KB", "128KB", "16KB", "256KB", "32KB", "512KB", "64KB", "8KB",
373 "platform-default"]),
374 write_cache_policy=dict(type='str', default='platform-default',
375 choices=["always-write-back", "platform-default", "write-back-good-bbu",
376 "write-through"]),
377 )
378
379
380 def main():
381 manual_disk = dict(
382 slot_num=dict(type='str', required=True),
383 role=dict(type='str', default='normal', choices=['normal', 'ded-hot-spare', 'glob-hot-spare']),
384 span_id=dict(type='str', default='unspecified'),
385 state=dict(type='str', default='present', choices=['present', 'absent']),
386 )
387
388 argument_spec = ucs_argument_spec.copy()
389 argument_spec.update(
390 org_dn=dict(type='str', default='org-root'),
391 name=dict(type='str', required=True),
392 description=dict(type='str', aliases=['descr']),
393 raid_level=dict(
394 type='str',
395 default='stripe',
396 choices=[
397 'stripe',
398 'mirror',
399 'mirror-stripe',
400 'stripe-parity',
401 'stripe-dual-parity',
402 'stripe-parity-stripe',
403 'stripe-dual-parity-stripe',
404 ],
405 ),
406 num_drives=dict(type='str', default='1'),
407 configuration_mode=dict(type='str', default='automatic', choices=['automatic', 'manual']),
408 num_ded_hot_spares=dict(type='str', default='unspecified'),
409 num_glob_hot_spares=dict(type='str', default='unspecified'),
410 drive_type=dict(type='str', default='unspecified', choices=['unspecified', 'HDD', 'SSD']),
411 use_remaining_disks=dict(type='str', default='no', choices=['yes', 'no']),
412 min_drive_size=dict(type='str', default='unspecified'),
413 manual_disks=dict(type='list', elements='dict', options=manual_disk),
414 state=dict(type='str', default='present', choices=['present', 'absent']),
415 virtual_drive=dict(type='dict', options=_virtual_drive_argument_spec()),
416 )
417 module = AnsibleModule(
418 argument_spec,
419 supports_check_mode=True,
420 )
421 ucs = UCSModule(module)
422 # UCSModule creation above verifies ucsmsdk is present and exits on failure.
423 # Additional imports are done below or in called functions.
424
425 ucs.result['changed'] = False
426 props_match = False
427 # dn is <org_dn>/disk-group-config-<name>
428 dn = module.params['org_dn'] + '/disk-group-config-' + module.params['name']
429
430 mo = ucs.login_handle.query_dn(dn)
431 if mo:
432 if module.params['state'] == 'absent':
433 # mo must exist but all properties do not have to match
434 if not module.check_mode:
435 ucs.login_handle.remove_mo(mo)
436 ucs.login_handle.commit()
437 ucs.result['changed'] = True
438 else: # state == 'present'
439 props_match = check_disk_policy_props(ucs, module, mo, dn)
440
441 if module.params['state'] == 'present' and not props_match:
442 configure_disk_policy(ucs, module, dn)
443
444 module.exit_json(**ucs.result)
445
446
447 if __name__ == '__main__':
448 main()