Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_server_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: intersight_server_profile_template
16 short_description: Manage Server Profile Templates in Cisco Intersight
17 description:
18 - Create, update, and delete Server Profile Templates on Cisco Intersight.
19 - Server Profile Templates define a reusable configuration that can be used to derive multiple Server Profiles.
20 - Policies are attached to the template via the PolicyBucket sub-resource endpoint.
21 - To derive Server Profiles from a template, use the derive_profiles playbook included in this collection.
22 See U(https://github.com/CiscoDevNet/intersight-ansible/blob/master/playbooks/derive_profiles.yml) for details.
23 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/server/ProfileTemplate/post/).
24 extends_documentation_fragment: intersight
25 options:
26 state:
27 description:
28 - If C(present), will verify the resource is present and will create if needed.
29 - If C(absent), will verify the resource is absent and will delete if needed.
30 type: str
31 choices: [present, absent]
32 default: present
33 organization:
34 description:
35 - The name of the Organization this resource is assigned to.
36 - Templates and Policies that are created within a Custom Organization are applicable only to devices in the same Organization.
37 type: str
38 default: default
39 name:
40 description:
41 - The name assigned to the Server Profile Template.
42 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
43 type: str
44 required: true
45 description:
46 description:
47 - The user-defined description of the Server Profile Template.
48 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
49 type: str
50 aliases: [descr]
51 tags:
52 description:
53 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
54 type: list
55 elements: dict
56 target_platform:
57 description:
58 - The platform for which the server profile template is applicable.
59 - C(standalone) for servers operating in Standalone mode.
60 - C(fi-attached) for servers attached to a Fabric Interconnect managed by Intersight.
61 - C(unified-edge) for Unified Edge servers.
62 type: str
63 choices: [standalone, fi-attached, unified-edge]
64 default: standalone
65 adapter_policy:
66 description:
67 - Name of Adapter Configuration Policy to associate with this template.
68 type: str
69 bios_policy:
70 description:
71 - Name of BIOS Policy to associate with this template.
72 type: str
73 boot_order_policy:
74 description:
75 - Name of Boot Order Policy to associate with this template.
76 type: str
77 certificate_policy:
78 description:
79 - Name of Certificate Management Policy to associate with this template.
80 type: str
81 device_connector_policy:
82 description:
83 - Name of Device Connector Policy to associate with this template.
84 type: str
85 drive_security_policy:
86 description:
87 - Name of Drive Security Policy to associate with this template.
88 type: str
89 firmware_policy:
90 description:
91 - Name of Firmware Policy to associate with this template.
92 type: str
93 ipmi_over_lan_policy:
94 description:
95 - Name of IPMI over LAN Policy to associate with this template.
96 type: str
97 lan_connectivity_policy:
98 description:
99 - Name of LAN Connectivity Policy to associate with this template.
100 type: str
101 ldap_policy:
102 description:
103 - Name of LDAP Policy to associate with this template.
104 type: str
105 local_user_policy:
106 description:
107 - Name of Local User Policy to associate with this template.
108 type: str
109 memory_policy:
110 description:
111 - Name of Memory Policy to associate with this template.
112 type: str
113 network_connectivity_policy:
114 description:
115 - Name of Network Connectivity Policy to associate with this template.
116 type: str
117 ntp_policy:
118 description:
119 - Name of NTP Policy to associate with this template.
120 type: str
121 persistent_memory_policy:
122 description:
123 - Name of Persistent Memory Policy to associate with this template.
124 type: str
125 power_policy:
126 description:
127 - Name of Power Policy to associate with this template.
128 type: str
129 san_connectivity_policy:
130 description:
131 - Name of SAN Connectivity Policy to associate with this template.
132 type: str
133 sd_card_policy:
134 description:
135 - Name of SD Card Policy to associate with this template.
136 type: str
137 serial_over_lan_policy:
138 description:
139 - Name of Serial over LAN Policy to associate with this template.
140 type: str
141 smtp_policy:
142 description:
143 - Name of SMTP Policy to associate with this template.
144 type: str
145 snmp_policy:
146 description:
147 - Name of SNMP Policy to associate with this template.
148 type: str
149 ssh_policy:
150 description:
151 - Name of SSH Policy to associate with this template.
152 type: str
153 storage_policy:
154 description:
155 - Name of Storage Policy to associate with this template.
156 type: str
157 syslog_policy:
158 description:
159 - Name of Syslog Policy to associate with this template.
160 type: str
161 thermal_policy:
162 description:
163 - Name of Thermal Policy to associate with this template.
164 type: str
165 virtual_kvm_policy:
166 description:
167 - Name of Virtual KVM Policy to associate with this template.
168 type: str
169 virtual_media_policy:
170 description:
171 - Name of Virtual Media Policy to associate with this template.
172 type: str
173 author:
174 - Ron Gershburg (@rgershbu)
175 '''
176
177 EXAMPLES = r'''
178 - name: Create a basic Server Profile Template
179 cisco.intersight.intersight_server_profile_template:
180 api_private_key: "{{ api_private_key }}"
181 api_key_id: "{{ api_key_id }}"
182 organization: "default"
183 name: "SPT-Standalone-Basic"
184 description: "Basic standalone server profile template"
185 target_platform: standalone
186 tags:
187 - Key: "Environment"
188 Value: "Production"
189 state: present
190
191 - name: Create a Server Profile Template with policies
192 cisco.intersight.intersight_server_profile_template:
193 api_private_key: "{{ api_private_key }}"
194 api_key_id: "{{ api_key_id }}"
195 organization: "default"
196 name: "SPT-FI-Production"
197 description: "FI-Attached template with policies"
198 target_platform: fi-attached
199 bios_policy: "BIOS-Production"
200 boot_order_policy: "Boot-UEFI"
201 firmware_policy: "FW-Latest"
202 ntp_policy: "NTP-Corp"
203 power_policy: "Power-Grid-Redundancy"
204 storage_policy: "Storage-M2-Boot"
205 lan_connectivity_policy: "LAN-Prod"
206 san_connectivity_policy: "SAN-Prod"
207 state: present
208
209 - name: Delete a Server Profile Template
210 cisco.intersight.intersight_server_profile_template:
211 api_private_key: "{{ api_private_key }}"
212 api_key_id: "{{ api_key_id }}"
213 name: "SPT-Standalone-Basic"
214 state: absent
215 '''
216
217 RETURN = r'''
218 api_response:
219 description: The API response output returned by the specified resource.
220 returned: always
221 type: dict
222 sample:
223 "api_response": {
224 "Name": "SPT-FI-Production",
225 "ObjectType": "server.ProfileTemplate",
226 "TargetPlatform": "FIAttached",
227 "PolicyBucket": [
228 {
229 "Moid": "6937ec186275723101f63708",
230 "ObjectType": "bios.Policy"
231 },
232 {
233 "Moid": "69a42f0770686131014f259c",
234 "ObjectType": "firmware.Policy"
235 }
236 ],
237 "Tags": [
238 {
239 "Key": "Environment",
240 "Value": "Production"
241 }
242 ]
243 }
244 '''
245
246
247 from ansible.module_utils.basic import AnsibleModule
248 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import (
249 IntersightModule,
250 intersight_argument_spec,
251 resolve_policy_bucket,
252 sync_policy_bucket,
253 )
254
255
256 POLICY_MAPPING = {
257 'adapter_policy': {'resource_path': '/adapter/ConfigPolicies', 'object_type': 'adapter.ConfigPolicy'},
258 'bios_policy': {'resource_path': '/bios/Policies', 'object_type': 'bios.Policy'},
259 'boot_order_policy': {'resource_path': '/boot/PrecisionPolicies', 'object_type': 'boot.PrecisionPolicy'},
260 'certificate_policy': {'resource_path': '/certificatemanagement/Policies', 'object_type': 'certificatemanagement.Policy'},
261 'device_connector_policy': {'resource_path': '/deviceconnector/Policies', 'object_type': 'deviceconnector.Policy'},
262 'drive_security_policy': {'resource_path': '/storage/DriveSecurityPolicies', 'object_type': 'storage.DriveSecurityPolicy'},
263 'firmware_policy': {'resource_path': '/firmware/Policies', 'object_type': 'firmware.Policy'},
264 'ipmi_over_lan_policy': {'resource_path': '/ipmioverlan/Policies', 'object_type': 'ipmioverlan.Policy'},
265 'lan_connectivity_policy': {'resource_path': '/vnic/LanConnectivityPolicies', 'object_type': 'vnic.LanConnectivityPolicy'},
266 'ldap_policy': {'resource_path': '/iam/LdapPolicies', 'object_type': 'iam.LdapPolicy'},
267 'local_user_policy': {'resource_path': '/iam/EndPointUserPolicies', 'object_type': 'iam.EndPointUserPolicy'},
268 'memory_policy': {'resource_path': '/memory/Policies', 'object_type': 'memory.Policy'},
269 'network_connectivity_policy': {'resource_path': '/networkconfig/Policies', 'object_type': 'networkconfig.Policy'},
270 'ntp_policy': {'resource_path': '/ntp/Policies', 'object_type': 'ntp.Policy'},
271 'persistent_memory_policy': {'resource_path': '/memory/PersistentMemoryPolicies', 'object_type': 'memory.PersistentMemoryPolicy'},
272 'power_policy': {'resource_path': '/power/Policies', 'object_type': 'power.Policy'},
273 'san_connectivity_policy': {'resource_path': '/vnic/SanConnectivityPolicies', 'object_type': 'vnic.SanConnectivityPolicy'},
274 'sd_card_policy': {'resource_path': '/sdcard/Policies', 'object_type': 'sdcard.Policy'},
275 'serial_over_lan_policy': {'resource_path': '/sol/Policies', 'object_type': 'sol.Policy'},
276 'smtp_policy': {'resource_path': '/smtp/Policies', 'object_type': 'smtp.Policy'},
277 'snmp_policy': {'resource_path': '/snmp/Policies', 'object_type': 'snmp.Policy'},
278 'ssh_policy': {'resource_path': '/ssh/Policies', 'object_type': 'ssh.Policy'},
279 'storage_policy': {'resource_path': '/storage/StoragePolicies', 'object_type': 'storage.StoragePolicy'},
280 'syslog_policy': {'resource_path': '/syslog/Policies', 'object_type': 'syslog.Policy'},
281 'thermal_policy': {'resource_path': '/thermal/Policies', 'object_type': 'thermal.Policy'},
282 'virtual_kvm_policy': {'resource_path': '/kvm/Policies', 'object_type': 'kvm.Policy'},
283 'virtual_media_policy': {'resource_path': '/vmedia/Policies', 'object_type': 'vmedia.Policy'},
284 }
285
286 TARGET_PLATFORM_MAP = {
287 'standalone': 'Standalone',
288 'fi-attached': 'FIAttached',
289 'unified-edge': 'UnifiedEdgeServer',
290 }
291
292
293 def main():
294 argument_spec = intersight_argument_spec.copy()
295 argument_spec.update(
296 state=dict(type='str', choices=['present', 'absent'], default='present'),
297 organization=dict(type='str', default='default'),
298 name=dict(type='str', required=True),
299 description=dict(type='str', aliases=['descr']),
300 tags=dict(type='list', elements='dict'),
301 target_platform=dict(type='str', choices=['standalone', 'fi-attached', 'unified-edge'], default='standalone'),
302 adapter_policy=dict(type='str'),
303 bios_policy=dict(type='str'),
304 boot_order_policy=dict(type='str'),
305 certificate_policy=dict(type='str'),
306 device_connector_policy=dict(type='str'),
307 drive_security_policy=dict(type='str'),
308 firmware_policy=dict(type='str'),
309 ipmi_over_lan_policy=dict(type='str'),
310 lan_connectivity_policy=dict(type='str'),
311 ldap_policy=dict(type='str'),
312 local_user_policy=dict(type='str'),
313 memory_policy=dict(type='str'),
314 network_connectivity_policy=dict(type='str'),
315 ntp_policy=dict(type='str'),
316 persistent_memory_policy=dict(type='str'),
317 power_policy=dict(type='str'),
318 san_connectivity_policy=dict(type='str'),
319 sd_card_policy=dict(type='str'),
320 serial_over_lan_policy=dict(type='str'),
321 smtp_policy=dict(type='str'),
322 snmp_policy=dict(type='str'),
323 ssh_policy=dict(type='str'),
324 storage_policy=dict(type='str'),
325 syslog_policy=dict(type='str'),
326 thermal_policy=dict(type='str'),
327 virtual_kvm_policy=dict(type='str'),
328 virtual_media_policy=dict(type='str'),
329 )
330
331 module = AnsibleModule(
332 argument_spec,
333 supports_check_mode=True,
334 )
335
336 intersight = IntersightModule(module)
337 intersight.result['api_response'] = {}
338 intersight.result['trace_id'] = ''
339
340 # Create/update the template itself
341 resource_path = '/server/ProfileTemplates'
342
343 intersight.api_body = {
344 'Organization': {
345 'Name': intersight.module.params['organization'],
346 },
347 'Name': intersight.module.params['name'],
348 }
349
350 if intersight.module.params['state'] == 'present':
351 intersight.set_tags_and_description()
352 api_target_platform = TARGET_PLATFORM_MAP[intersight.module.params['target_platform']]
353 intersight.api_body['TargetPlatform'] = api_target_platform
354
355 intersight.configure_policy_or_profile(resource_path=resource_path)
356 template_response = intersight.result['api_response']
357 template_moid = template_response.get('Moid') if template_response else None
358
359 # Sync PolicyBucket via the sub-resource endpoint
360 if template_moid and intersight.module.params['state'] == 'present':
361 organization_name = intersight.module.params['organization']
362 desired_bucket = resolve_policy_bucket(intersight, organization_name, POLICY_MAPPING)
363 current_bucket = template_response.get('PolicyBucket') or []
364 bucket_path = f'/server/ProfileTemplates/{template_moid}/PolicyBucket'
365 sync_policy_bucket(intersight, bucket_path, desired_bucket, current_bucket)
366
367 module.exit_json(**intersight.result)
368
369
370 if __name__ == '__main__':
371 main()