Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_memory_persistent_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_memory_persistent_policy
16 short_description: Memory Persistent Policy configuration for Cisco Intersight
17 description:
18 - Manages Memory Persistent Policy configuration on Cisco Intersight.
19 - Configure Persistent Memory Modules (PMM) on servers including security, goals, and namespaces.
20 - Supports both Intersight-managed and Operating System-managed configuration modes.
21 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/memory/PersistentMemoryPolicy/get/).
22 extends_documentation_fragment: intersight
23 options:
24 state:
25 description:
26 - If C(present), will verify the resource is present and will create if needed.
27 - If C(absent), will verify the resource is absent and will delete if needed.
28 type: str
29 choices: [present, absent]
30 default: present
31 organization:
32 description:
33 - The name of the Organization this resource is assigned to.
34 - Policies created within a Custom Organization are applicable only to devices in the same Organization.
35 type: str
36 default: default
37 name:
38 description:
39 - The name assigned to the Memory Persistent Policy.
40 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
41 type: str
42 required: true
43 description:
44 description:
45 - The user-defined description for the Memory Persistent Policy.
46 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
47 type: str
48 aliases: [descr]
49 tags:
50 description:
51 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
52 type: list
53 elements: dict
54 management_mode:
55 description:
56 - Configuration management mode for Persistent Memory.
57 - C(configured-from-intersight) - Policy settings are configured and managed from Intersight.
58 - C(configured-from-operating-system) - Associated servers are managed by Operating System tools and policy settings are unavailable.
59 - When set to C(configured-from-operating-system), no other configuration fields are required or used.
60 type: str
61 choices: [configured-from-intersight, configured-from-operating-system]
62 default: configured-from-intersight
63 enable_security_passphrase:
64 description:
65 - Enable secure passphrase for Persistent Memory Modules.
66 - When enabled, requires secure_passphrase parameter.
67 - Only applicable when management_mode is C(configured-from-intersight).
68 type: bool
69 default: false
70 secure_passphrase:
71 description:
72 - Secure passphrase to be applied on the Persistent Memory Modules on the server.
73 - Required when enable_security_passphrase is true.
74 - The allowed characters are a-z, A-Z, 0-9, and special characters =, !, &, #, $, %, +, ^, @, _, *, -.
75 - Only applicable when management_mode is C(configured-from-intersight).
76 type: str
77 enable_goal:
78 description:
79 - Enable goal configuration for Persistent Memory Modules.
80 - The Goal configured will be applicable to all the Persistent Memory Modules.
81 - Goal modification will delete all existing regions and namespaces along with their data during profile deployment.
82 - New regions and namespaces will be created after goal modification.
83 - Only applicable when management_mode is C(configured-from-intersight).
84 type: bool
85 default: true
86 memory_mode_percentage:
87 description:
88 - Volatile memory percentage for Memory Mode.
89 - Valid range is 0-100.
90 - Only applicable when enable_goal is true and management_mode is C(configured-from-intersight).
91 type: int
92 default: 0
93 persistent_memory_type:
94 description:
95 - Type of Persistent Memory configuration.
96 - C(app-direct) - Persistent Memory Modules are combined in an interleaved set.
97 - C(app-direct-non-interleaved) - Persistent Memory Modules are not interleaved.
98 - Only applicable when enable_goal is true and management_mode is C(configured-from-intersight).
99 type: str
100 choices: [app-direct, app-direct-non-interleaved]
101 default: app-direct
102 retain_namespaces:
103 description:
104 - Retain existing Persistent Memory Namespaces.
105 - If false, all existing namespaces not listed in the namespaces parameter will be deleted along with their data.
106 - Only applicable when management_mode is C(configured-from-intersight).
107 type: bool
108 default: true
109 namespaces:
110 description:
111 - List of Logical Namespaces to be created or modified on the server.
112 - Only applicable when management_mode is C(configured-from-intersight).
113 type: list
114 elements: dict
115 suboptions:
116 name:
117 description:
118 - Name of this Namespace to be created on the server.
119 type: str
120 required: true
121 socket_id:
122 description:
123 - Socket ID of the region on which this Namespace has to be created or modified.
124 - Valid values are 1, 2, 3, or 4.
125 type: int
126 choices: [1, 2, 3, 4]
127 default: 1
128 socket_memory_id:
129 description:
130 - Socket Memory ID of the region on which this Namespace has to be created or modified.
131 - Only applicable when persistent_memory_type is C(app-direct-non-interleaved).
132 - Valid values are 2, 4, 6, 8, 10, or 12.
133 type: int
134 choices: [2, 4, 6, 8, 10, 12]
135 default: 2
136 capacity:
137 description:
138 - Capacity of this Namespace in GiB.
139 - Valid range is 1 to 9223372036854775807.
140 type: int
141 required: true
142 mode:
143 description:
144 - Mode of this Namespace.
145 type: str
146 choices: [raw, block]
147 default: raw
148 author:
149 - Ron Gershburg (@rgershbu)
150 '''
151
152 EXAMPLES = r'''
153 - name: Create Memory Persistent Policy with Intersight management
154 cisco.intersight.intersight_memory_persistent_policy:
155 api_private_key: "{{ api_private_key }}"
156 api_key_id: "{{ api_key_id }}"
157 organization: "default"
158 name: "pmem-policy-01"
159 description: "Persistent Memory policy with security and namespaces"
160 management_mode: configured-from-intersight
161 enable_security_passphrase: true
162 secure_passphrase: "SecurePass123!"
163 enable_goal: true
164 memory_mode_percentage: 0
165 persistent_memory_type: app-direct
166 retain_namespaces: true
167 namespaces:
168 - name: "ns1"
169 socket_id: 1
170 capacity: 100000
171 mode: raw
172 - name: "ns2"
173 socket_id: 4
174 capacity: 1000065
175 mode: block
176 state: present
177
178 - name: Create Memory Persistent Policy with non-interleaved configuration
179 cisco.intersight.intersight_memory_persistent_policy:
180 api_private_key: "{{ api_private_key }}"
181 api_key_id: "{{ api_key_id }}"
182 organization: "default"
183 name: "pmem-policy-non-interleaved"
184 description: "Non-interleaved persistent memory configuration"
185 management_mode: configured-from-intersight
186 enable_security_passphrase: true
187 secure_passphrase: "MySecurePass123"
188 enable_goal: true
189 memory_mode_percentage: 0
190 persistent_memory_type: app-direct-non-interleaved
191 retain_namespaces: true
192 namespaces:
193 - name: "ns1"
194 socket_id: 1
195 socket_memory_id: 2
196 capacity: 100000
197 mode: raw
198 - name: "ns2"
199 socket_id: 2
200 socket_memory_id: 4
201 capacity: 200000
202 mode: block
203 state: present
204
205 - name: Create Memory Persistent Policy with OS management
206 cisco.intersight.intersight_memory_persistent_policy:
207 api_private_key: "{{ api_private_key }}"
208 api_key_id: "{{ api_key_id }}"
209 organization: "default"
210 name: "pmem-policy-os-managed"
211 description: "OS-managed persistent memory configuration"
212 management_mode: configured-from-operating-system
213 state: present
214
215 - name: Create Memory Persistent Policy without security passphrase
216 cisco.intersight.intersight_memory_persistent_policy:
217 api_private_key: "{{ api_private_key }}"
218 api_key_id: "{{ api_key_id }}"
219 name: "pmem-policy-no-security"
220 description: "Policy without security passphrase"
221 management_mode: configured-from-intersight
222 enable_security_passphrase: false
223 enable_goal: true
224 memory_mode_percentage: 10
225 persistent_memory_type: app-direct
226 state: present
227
228 - name: Delete Memory Persistent Policy
229 cisco.intersight.intersight_memory_persistent_policy:
230 api_private_key: "{{ api_private_key }}"
231 api_key_id: "{{ api_key_id }}"
232 name: "pmem-policy-01"
233 state: absent
234 '''
235
236 RETURN = r'''
237 api_response:
238 description: The API response output returned by the specified resource.
239 returned: always
240 type: dict
241 sample:
242 "api_response": {
243 "Name": "pmem-policy-01",
244 "ObjectType": "memory.PersistentMemoryPolicy",
245 "ManagementMode": "configured-from-intersight",
246 "LocalSecurity": {
247 "Enabled": true
248 },
249 "Goals": [
250 {
251 "MemoryModePercentage": 0,
252 "PersistentMemoryType": "app-direct",
253 "SocketId": "All Sockets"
254 }
255 ],
256 "RetainNamespaces": true,
257 "LogicalNamespaces": [
258 {
259 "Name": "ns1",
260 "SocketId": 1,
261 "SocketMemoryId": "Not Applicable",
262 "Capacity": 100000,
263 "Mode": "raw"
264 }
265 ]
266 }
267 '''
268
269
270 from ansible.module_utils.basic import AnsibleModule
271 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
272
273
274 def validate_namespace_capacity(capacity):
275 """
276 Validate namespace capacity is within acceptable range.
277 """
278 if capacity < 1 or capacity > 9223372036854775807:
279 raise ValueError(f"Namespace capacity {capacity} is out of valid range (1-9223372036854775807 GiB)")
280 return True
281
282
283 def main():
284 argument_spec = intersight_argument_spec.copy()
285 argument_spec.update(
286 state=dict(type='str', choices=['present', 'absent'], default='present'),
287 organization=dict(type='str', default='default'),
288 name=dict(type='str', required=True),
289 description=dict(type='str', aliases=['descr']),
290 tags=dict(type='list', elements='dict'),
291 management_mode=dict(type='str', choices=['configured-from-intersight', 'configured-from-operating-system'], default='configured-from-intersight'),
292 enable_security_passphrase=dict(type='bool', default=False),
293 secure_passphrase=dict(type='str', no_log=True),
294 enable_goal=dict(type='bool', default=True),
295 memory_mode_percentage=dict(type='int', default=0),
296 persistent_memory_type=dict(type='str', choices=['app-direct', 'app-direct-non-interleaved'], default='app-direct'),
297 retain_namespaces=dict(type='bool', default=True),
298 namespaces=dict(type='list', elements='dict', options=dict(
299 name=dict(type='str', required=True),
300 socket_id=dict(type='int', choices=[1, 2, 3, 4], default=1),
301 socket_memory_id=dict(type='int', choices=[2, 4, 6, 8, 10, 12], default=2),
302 capacity=dict(type='int', required=True),
303 mode=dict(type='str', choices=['raw', 'block'], default='raw')
304 ))
305 )
306 module = AnsibleModule(
307 argument_spec,
308 supports_check_mode=True,
309 required_if=[
310 ['enable_security_passphrase', True, ['secure_passphrase']],
311 ],
312 )
313
314 intersight = IntersightModule(module)
315 intersight.result['api_response'] = {}
316 intersight.result['trace_id'] = ''
317
318 # Resource path for Memory Persistent Policy
319 resource_path = '/memory/PersistentMemoryPolicies'
320
321 # Build base API body
322 intersight.api_body = {
323 'Organization': {
324 'Name': intersight.module.params['organization'],
325 },
326 'Name': intersight.module.params['name']
327 }
328
329 if intersight.module.params['state'] == 'present':
330 intersight.set_tags_and_description()
331
332 # Convert management mode to API format
333 management_mode_value = intersight.module.params['management_mode']
334 intersight.api_body['ManagementMode'] = management_mode_value
335
336 # Only add configuration fields if managed from Intersight
337 if management_mode_value == 'configured-from-intersight':
338 # Add security configuration if enabled
339 if intersight.module.params['enable_security_passphrase']:
340 intersight.api_body['LocalSecurity'] = {
341 'Enabled': True,
342 'SecurePassphrase': intersight.module.params['secure_passphrase']
343 }
344 else:
345 intersight.api_body['LocalSecurity'] = {
346 'Enabled': False
347 }
348
349 # Add goal configuration if enabled
350 if intersight.module.params['enable_goal']:
351 # Convert persistent memory type to API format
352 pmem_type_value = intersight.module.params['persistent_memory_type']
353 intersight.api_body['Goals'] = [
354 {
355 'MemoryModePercentage': intersight.module.params['memory_mode_percentage'],
356 'PersistentMemoryType': pmem_type_value,
357 'SocketId': 'All Sockets'
358 }
359 ]
360
361 # Add retain namespaces setting
362 intersight.api_body['RetainNamespaces'] = intersight.module.params['retain_namespaces']
363
364 # Add logical namespaces if provided
365 if intersight.module.params.get('namespaces'):
366 logical_namespaces = []
367 pmem_type = intersight.module.params['persistent_memory_type']
368 for ns in intersight.module.params['namespaces']:
369 # Validate namespace capacity
370 try:
371 validate_namespace_capacity(ns['capacity'])
372 except ValueError as e:
373 module.fail_json(msg=str(e))
374
375 # Determine SocketMemoryId based on persistent memory type
376 if pmem_type == 'app-direct-non-interleaved':
377 socket_memory_id = str(ns.get('socket_memory_id', 2))
378 else:
379 socket_memory_id = 'Not Applicable'
380
381 namespace_entry = {
382 'Name': ns['name'],
383 'SocketId': ns['socket_id'],
384 'SocketMemoryId': socket_memory_id,
385 'Capacity': ns['capacity'],
386 'Mode': ns['mode']
387 }
388 logical_namespaces.append(namespace_entry)
389
390 intersight.api_body['LogicalNamespaces'] = logical_namespaces
391
392 # Configure the policy
393 intersight.configure_policy_or_profile(resource_path=resource_path)
394
395 module.exit_json(**intersight.result)
396
397
398 if __name__ == '__main__':
399 main()