Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_service_profile_template.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: ucs_service_profile_template
16 short_description: Configures Service Profile Templates on Cisco UCS Manager
17 description:
18 - Configures Service Profile Templates on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify Service Profile Templates are present and will create if needed.
24 - If C(absent), will verify Service Profile Templates are absent and will delete if needed.
25 choices: [present, absent]
26 default: present
27 type: str
28 name:
29 description:
30 - The name of the service profile template.
31 - This name can be between 2 and 32 alphanumeric characters.
32 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
33 - This name must be unique across all service profiles and service profile templates within the same organization.
34 required: yes
35 type: str
36 template_type:
37 description:
38 - "The template type field which can be one of the following:"
39 - "initial-template — Any service profiles created from this template are not updated if the template changes."
40 - "updating-template — Any service profiles created from this template are updated if the template changes."
41 choices: [initial-template, updating-template]
42 default: initial-template
43 type: str
44 uuid_pool:
45 description:
46 - Specifies how the UUID will be set on a server associated with a service profile created from this template.
47 - "The uuid_pool option can be the name of the UUID pool to use or '' (the empty string)."
48 - An empty string will use the UUID assigned to the server by the manufacturer, and the
49 - UUID remains unassigned until a service profile created from this template is associated with a server. At that point,
50 - the UUID is set to the UUID value assigned to the server by the manufacturer. If the service profile is later moved to
51 - a different server, the UUID is changed to match the new server."
52 default: default
53 type: str
54 description:
55 description:
56 - A user-defined description of the service profile template.
57 - Enter up to 256 characters.
58 - "You can use any characters or spaces except the following:"
59 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
60 aliases: [ descr ]
61 type: str
62 storage_profile:
63 description:
64 - The name of the storage profile you want to associate with service profiles created from this template
65 type: str
66 local_disk_policy:
67 description:
68 - The name of the local disk policy you want to associate with service profiles created from this template.
69 type: str
70 lan_connectivity_policy:
71 description:
72 - The name of the LAN connectivity policy you want to associate with service profiles created from this template.
73 type: str
74 iqn_pool:
75 description:
76 - The name of the IQN pool (initiator) you want to apply to all iSCSI vNICs for service profiles created from this template.
77 type: str
78 san_connectivity_policy:
79 description:
80 - The name of the SAN connectivity policy you want to associate with service profiles created from this template.
81 type: str
82 vmedia_policy:
83 description:
84 - The name of the vMedia policy you want to associate with service profiles created from this template.
85 type: str
86 boot_policy:
87 description:
88 - The name of the boot order policy you want to associate with service profiles created from this template.
89 default: default
90 type: str
91 maintenance_policy:
92 description:
93 - The name of the maintenance policy you want to associate with service profiles created from this template.
94 type: str
95 server_pool:
96 description:
97 - The name of the server pool you want to associate with this service profile template.
98 type: str
99 server_pool_qualification:
100 description:
101 - The name of the server pool policy qualificaiton you want to use for this service profile template.
102 type: str
103 power_state:
104 description:
105 - The power state to be applied when a service profile created from this template is associated with a server.
106 choices: [up, down]
107 default: up
108 type: str
109 host_firmware_package:
110 description:
111 - The name of the host firmware package you want to associate with service profiles created from this template.
112 type: str
113 bios_policy:
114 description:
115 - The name of the BIOS policy you want to associate with service profiles created from this template.
116 type: str
117 ipmi_access_profile:
118 description:
119 - The name of the IPMI access profile you want to associate with service profiles created from this template.
120 type: str
121 sol_policy:
122 description:
123 - The name of the Serial over LAN (SoL) policy you want to associate with service profiles created from this template.
124 type: str
125 mgmt_ip_state:
126 description:
127 - The state for the Outband Management IP pool you want to use with service profiles created from this template.
128 - Choices are none or pooled.
129 default: pooled
130 type: str
131 mgmt_ip_pool:
132 description:
133 - The name of the Outband Management IP pool you want to use with service profiles created from this template.
134 default: ext-mgmt
135 type: str
136 power_control_policy:
137 description:
138 - The name of the power control policy you want to associate with service profiles created from this template.
139 default: default
140 type: str
141 power_sync_policy:
142 description:
143 - The name of the power sync policy you want to associate with service profiles created from this template.
144 type: str
145 scrub_policy:
146 description:
147 - The name of the scrub policy you want to associate with service profiles created from this template.
148 type: str
149 kvm_mgmt_policy:
150 description:
151 - The name of the KVM management policy you want to associate with service profiles created from this template.
152 type: str
153 graphics_card_policy:
154 description:
155 - The name of the graphics card policy you want to associate with service profiles created from this template.
156 type: str
157 threshold_policy:
158 description:
159 - The name of the threshold policy you want to associate with service profiles created from this template.
160 default: default
161 type: str
162 user_label:
163 description:
164 - The User Label you want to assign to service profiles created from this template.
165 type: str
166 mgmt_interface_mode:
167 description:
168 - The Management Interface you want to assign to service profiles created from this template.
169 choices: ['', in-band]
170 type: str
171 mgmt_vnet_name:
172 description:
173 - A VLAN selected from the associated VLAN group.
174 type: str
175 mgmt_inband_pool_name:
176 description:
177 - How the inband management IPv4 address is derived for the server associated with this service profile.
178 type: str
179 org_dn:
180 description:
181 - Org dn (distinguished name)
182 default: org-root
183 type: str
184 requirements:
185 - ucsmsdk
186 author:
187 - David Soper (@dsoper2)
188 - CiscoUcs (@CiscoUcs)
189 '''
190
191 EXAMPLES = r'''
192 - name: Configure Service Profile Template with LAN/SAN Connectivity and all other options defaulted
193 cisco.ucs.ucs_service_profile_template:
194 hostname: 172.16.143.150
195 username: admin
196 password: password
197 name: DEE-Ctrl
198 template_type: updating-template
199 uuid_pool: UUID-Pool
200 storage_profile: DEE-StgProf
201 lan_connectivity_policy: Cntr-FC-Boot
202 iqn_pool: iSCSI-Boot-A
203 san_connectivity_policy: Cntr-FC-Boot
204 boot_policy: DEE-vMedia
205 maintenance_policy: default
206 server_pool: Container-Pool
207 host_firmware_package: 3.1.2b
208 bios_policy: Docker
209
210 - name: Remove Service Profile Template
211 cisco.ucs.ucs_service_profile_template:
212 hostname: 172.16.143.150
213 username: admin
214 password: password
215 name: DEE-Ctrl
216 state: absent
217 '''
218
219 RETURN = r'''
220 #
221 '''
222
223 from ansible.module_utils.basic import AnsibleModule
224 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
225
226
227 def configure_service_profile_template(ucs, module):
228 from ucsmsdk.mometa.ls.LsServer import LsServer
229 from ucsmsdk.mometa.vnic.VnicConnDef import VnicConnDef
230 from ucsmsdk.mometa.vnic.VnicIScsiNode import VnicIScsiNode
231 from ucsmsdk.mometa.ls.LsRequirement import LsRequirement
232 from ucsmsdk.mometa.ls.LsPower import LsPower
233 from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding
234 from ucsmsdk.mometa.mgmt.MgmtInterface import MgmtInterface
235 from ucsmsdk.mometa.mgmt.MgmtVnet import MgmtVnet
236 from ucsmsdk.mometa.vnic.VnicIpV4MgmtPooledAddr import VnicIpV4MgmtPooledAddr
237
238 if not module.check_mode:
239 try:
240 # create if mo does not already exist
241 mo = LsServer(
242 parent_mo_or_dn=module.params['org_dn'],
243 bios_profile_name=module.params['bios_policy'],
244 boot_policy_name=module.params['boot_policy'],
245 descr=module.params['description'],
246 ext_ip_state=module.params['mgmt_ip_state'],
247 ext_ip_pool_name=module.params['mgmt_ip_pool'],
248 # graphics_card_policy_name=module.params['graphics_card_policy'],
249 host_fw_policy_name=module.params['host_firmware_package'],
250 ident_pool_name=module.params['uuid_pool'],
251 kvm_mgmt_policy_name=module.params['kvm_mgmt_policy'],
252 local_disk_policy_name=module.params['local_disk_policy'],
253 maint_policy_name=module.params['maintenance_policy'],
254 mgmt_access_policy_name=module.params['ipmi_access_profile'],
255 name=module.params['name'],
256 power_policy_name=module.params['power_control_policy'],
257 power_sync_policy_name=module.params['power_sync_policy'],
258 scrub_policy_name=module.params['scrub_policy'],
259 sol_policy_name=module.params['sol_policy'],
260 stats_policy_name=module.params['threshold_policy'],
261 type=module.params['template_type'],
262 usr_lbl=module.params['user_label'],
263 vmedia_policy_name=module.params['vmedia_policy'],
264 )
265
266 if module.params['storage_profile']:
267 # Storage profile
268 mo_1 = LstorageProfileBinding(
269 parent_mo_or_dn=mo,
270 storage_profile_name=module.params['storage_profile'],
271 )
272
273 if module.params['mgmt_interface_mode']:
274 # Management Interface
275 mo_1 = MgmtInterface(
276 parent_mo_or_dn=mo,
277 mode=module.params['mgmt_interface_mode'],
278 ip_v4_state='pooled',
279 )
280 mo_2 = MgmtVnet(
281 parent_mo_or_dn=mo_1,
282 id='1',
283 name=module.params['mgmt_vnet_name'],
284 )
285 VnicIpV4MgmtPooledAddr(
286 parent_mo_or_dn=mo_2,
287 name=module.params['mgmt_inband_pool_name'],
288 )
289
290 # LAN/SAN connectivity policy
291 mo_1 = VnicConnDef(
292 parent_mo_or_dn=mo,
293 lan_conn_policy_name=module.params['lan_connectivity_policy'],
294 san_conn_policy_name=module.params['san_connectivity_policy'],
295 )
296
297 if module.params['iqn_pool']:
298 # IQN pool
299 mo_1 = VnicIScsiNode(
300 parent_mo_or_dn=mo,
301 iqn_ident_pool_name=module.params['iqn_pool']
302 )
303
304 # power state
305 admin_state = 'admin-' + module.params['power_state']
306 mo_1 = LsPower(
307 parent_mo_or_dn=mo,
308 state=admin_state,
309 )
310
311 if module.params['server_pool']:
312 # server pool
313 mo_1 = LsRequirement(
314 parent_mo_or_dn=mo,
315 name=module.params['server_pool'],
316 qualifier=module.params['server_pool_qualification'],
317 )
318
319 ucs.login_handle.add_mo(mo, True)
320 ucs.login_handle.commit()
321 except Exception as e: # generic Exception handling because SDK can throw a variety of exceptions
322 ucs.result['msg'] = "setup error: %s " % str(e)
323 module.fail_json(**ucs.result)
324
325 ucs.result['changed'] = True
326
327
328 def check_storage_profile_props(ucs, module, dn):
329 props_match = False
330
331 child_dn = dn + '/profile-binding'
332 mo_1 = ucs.login_handle.query_dn(child_dn)
333 if mo_1:
334 kwargs = dict(storage_profile_name=module.params['storage_profile'])
335 if mo_1.check_prop_match(**kwargs):
336 props_match = True
337 elif not module.params['storage_profile']:
338 # no stroage profile mo or desired state
339 props_match = True
340
341 return props_match
342
343
344 def check_connectivity_policy_props(ucs, module, dn):
345 props_match = False
346
347 child_dn = dn + '/conn-def'
348 mo_1 = ucs.login_handle.query_dn(child_dn)
349 if mo_1:
350 kwargs = dict(lan_conn_policy_name=module.params['lan_connectivity_policy'])
351 kwargs['san_conn_policy_name'] = module.params['san_connectivity_policy']
352 if mo_1.check_prop_match(**kwargs):
353 props_match = True
354 elif not module.params['lan_connectivity_policy'] and not module.params['san_connectivity_policy']:
355 # no mo and no desired state
356 props_match = True
357
358 return props_match
359
360
361 def check_iqn_pool_props(ucs, module, dn):
362 props_match = False
363
364 child_dn = dn + '/iscsi-node'
365 mo_1 = ucs.login_handle.query_dn(child_dn)
366 if mo_1:
367 kwargs = dict(iqn_ident_pool_name=module.params['iqn_pool'])
368 if mo_1.check_prop_match(**kwargs):
369 props_match = True
370 elif not module.params['iqn_pool']:
371 # no mo and no desired state
372 props_match = True
373
374 return props_match
375
376
377 def check_inband_management_props(ucs, module, dn):
378 props_match = False
379
380 child_dn = dn + '/iface-in-band'
381 mo_1 = ucs.login_handle.query_dn(child_dn)
382 if mo_1:
383 kwargs = dict(mode=module.params['mgmt_interface_mode'])
384 if mo_1.check_prop_match(**kwargs):
385 child_dn = child_dn + '/network'
386 mo_2 = ucs.login_handle.query_dn(child_dn)
387 if mo_2:
388 kwargs = dict(name=module.params['mgmt_vnet_name'])
389 if mo_2.check_prop_match(**kwargs):
390 child_dn = child_dn + '/ipv4-pooled-addr'
391 mo_3 = ucs.login_handle.query_dn(child_dn)
392 if mo_3:
393 kwargs = dict(name=module.params['mgmt_inband_pool_name'])
394 if mo_3.check_prop_match(**kwargs):
395 props_match = True
396 elif not module.params['mgmt_interface_mode']:
397 # no mo and no desired state
398 props_match = True
399
400 return props_match
401
402
403 def check_power_props(ucs, module, dn):
404 props_match = False
405
406 child_dn = dn + '/power'
407 mo_1 = ucs.login_handle.query_dn(child_dn)
408 if mo_1:
409 kwargs = dict(state=module.params['power_state'])
410 if mo_1.check_prop_match(**kwargs):
411 props_match = True
412 elif not module.params['power_state']:
413 # no mo and no desired state
414 props_match = True
415
416 return props_match
417
418
419 def check_server_pool(ucs, module, dn):
420 props_match = False
421
422 child_dn = dn + '/pn-req'
423 mo_1 = ucs.login_handle.query_dn(child_dn)
424 if mo_1:
425 kwargs = dict(name=module.params['server_pool'])
426 kwargs['qualifier'] = module.params['server_pool_qualification']
427 if mo_1.check_prop_match(**kwargs):
428 props_match = True
429 elif not module.params['server_pool']:
430 # no pn-req object and no server pool name provided
431 props_match = True
432
433 return props_match
434
435
436 def check_serivce_profile_templates_props(ucs, module, mo, dn):
437 props_match = False
438
439 # check top-level mo props
440 kwargs = dict(bios_profile_name=module.params['bios_policy'])
441 kwargs['boot_policy_name'] = module.params['boot_policy']
442 kwargs['descr'] = module.params['description']
443 kwargs['ext_ip_state'] = module.params['mgmt_ip_state']
444 kwargs['ext_ip_pool_name'] = module.params['mgmt_ip_pool']
445 # kwargs['graphics_card_policy_name'] = module.params['graphics_card_policy']
446 kwargs['host_fw_policy_name'] = module.params['host_firmware_package']
447 kwargs['ident_pool_name'] = module.params['uuid_pool']
448 kwargs['kvm_mgmt_policy_name'] = module.params['kvm_mgmt_policy']
449 kwargs['local_disk_policy_name'] = module.params['local_disk_policy']
450 kwargs['maint_policy_name'] = module.params['maintenance_policy']
451 kwargs['mgmt_access_policy_name'] = module.params['ipmi_access_profile']
452 kwargs['power_policy_name'] = module.params['power_control_policy']
453 kwargs['power_sync_policy_name'] = module.params['power_sync_policy']
454 kwargs['scrub_policy_name'] = module.params['scrub_policy']
455 kwargs['sol_policy_name'] = module.params['sol_policy']
456 kwargs['stats_policy_name'] = module.params['threshold_policy']
457 kwargs['type'] = module.params['template_type']
458 kwargs['usr_lbl'] = module.params['user_label']
459 kwargs['vmedia_policy_name'] = module.params['vmedia_policy']
460
461 if mo.check_prop_match(**kwargs):
462 # top-level props match, check next level mo/props
463 # code below should discontinue checks once any mismatch is found
464
465 # check storage profile 1st
466 props_match = check_storage_profile_props(ucs, module, dn)
467
468 if props_match:
469 props_match = check_connectivity_policy_props(ucs, module, dn)
470
471 if props_match:
472 props_match = check_iqn_pool_props(ucs, module, dn)
473
474 if props_match:
475 props_match = check_inband_management_props(ucs, module, dn)
476
477 if props_match:
478 props_match = check_power_props(ucs, module, dn)
479
480 if props_match:
481 props_match = check_server_pool(ucs, module, dn)
482
483 return props_match
484
485
486 def main():
487 argument_spec = ucs_argument_spec.copy()
488 argument_spec.update(
489 org_dn=dict(type='str', default='org-root'),
490 name=dict(type='str', required=True),
491 bios_policy=dict(type='str'),
492 boot_policy=dict(type='str', default='default'),
493 description=dict(type='str', aliases=['descr']),
494 mgmt_ip_state=dict(type='str', default='pooled'),
495 mgmt_ip_pool=dict(type='str', default='ext-mgmt'),
496 graphics_card_policy=dict(type='str'),
497 host_firmware_package=dict(type='str'),
498 uuid_pool=dict(type='str', default='default'),
499 kvm_mgmt_policy=dict(type='str'),
500 local_disk_policy=dict(type='str'),
501 maintenance_policy=dict(type='str'),
502 ipmi_access_profile=dict(type='str'),
503 power_control_policy=dict(type='str', default='default'),
504 power_sync_policy=dict(type='str'),
505 scrub_policy=dict(type='str'),
506 sol_policy=dict(type='str'),
507 threshold_policy=dict(type='str', default='default'),
508 template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
509 user_label=dict(type='str'),
510 vmedia_policy=dict(type='str'),
511 storage_profile=dict(type='str'),
512 lan_connectivity_policy=dict(type='str'),
513 iqn_pool=dict(type='str'),
514 san_connectivity_policy=dict(type='str'),
515 server_pool=dict(type='str'),
516 server_pool_qualification=dict(type='str'),
517 power_state=dict(type='str', default='up', choices=['up', 'down']),
518 mgmt_interface_mode=dict(type='str', choices=['', 'in-band']),
519 mgmt_vnet_name=dict(type='str'),
520 mgmt_inband_pool_name=dict(type='str'),
521 state=dict(type='str', default='present', choices=['present', 'absent']),
522 )
523
524 module = AnsibleModule(
525 argument_spec,
526 supports_check_mode=True,
527 )
528 ucs = UCSModule(module)
529 # UCSModule creation above verifies ucsmsdk is present and exits on failure.
530 # Additional imports are done below or in called functions.
531
532 ucs.result['changed'] = False
533 props_match = False
534 # dn is <org_dn>/ls-<name>
535 dn = module.params['org_dn'] + '/ls-' + module.params['name']
536
537 mo = ucs.login_handle.query_dn(dn)
538 if mo:
539 if module.params['state'] == 'absent':
540 # mo must exist but all properties do not have to match
541 if not module.check_mode:
542 ucs.login_handle.remove_mo(mo)
543 ucs.login_handle.commit()
544 ucs.result['changed'] = True
545 else: # state == 'present'
546 props_match = check_serivce_profile_templates_props(ucs, module, mo, dn)
547
548 if module.params['state'] == 'present' and not props_match:
549 configure_service_profile_template(ucs, module)
550
551 module.exit_json(**ucs.result)
552
553
554 if __name__ == '__main__':
555 main()