Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_power_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_power_policy
16 short_description: Power Policy configuration for Cisco Intersight
17 description:
18 - Manages Power Policy configuration on Cisco Intersight.
19 - A policy to configure the power settings on Cisco Intersight managed servers.
20 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/power/Policy/get/).
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 Power 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 Power 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 target_platform:
54 description:
55 - The platform type for which the power policy is intended. This determines which settings are applicable.
56 - This parameter is required when C(state) is C(present).
57 type: str
58 choices: ['All', 'Standalone', 'FIAttached', 'Chassis']
59 power_profiling:
60 description:
61 - Sets the Power Profiling of the Server.
62 - If Enabled, this field allows the power manager to run power profiling utility to determine the power needs of the server.
63 - This field is only supported for Cisco UCS X series servers.
64 - Applicable for 'FIAttached' and 'All' platforms.
65 type: str
66 choices: ['Enabled', 'Disabled']
67 default: 'Enabled'
68 power_priority:
69 description:
70 - Sets the Power Priority of the Server. This priority is used to determine the initial power allocation for servers.
71 - This field is only supported for Cisco UCS B series and X series servers.
72 - Applicable for 'FIAttached' and 'All' platforms.
73 type: str
74 choices: ['Low', 'Medium', 'High']
75 default: 'Low'
76 power_restore:
77 description:
78 - Sets the Power Restore State of the Server.
79 - In the absence of Intersight connectivity, the chassis/server will use this policy to recover the host power after a power loss event.
80 - Applicable for 'Standalone', 'FIAttached', and 'All' platforms.
81 type: str
82 choices: ['AlwaysOff', 'AlwaysOn', 'LastState']
83 default: 'AlwaysOff'
84 power_redundancy:
85 description:
86 - Sets the Power Redundancy Mode of the Chassis. Redundancy Mode determines the number of PSUs the chassis keeps as redundant.
87 - N+2 mode is only supported for Cisco UCS X series Chassis.
88 - Applicable for 'Chassis' and 'All' platforms.
89 type: str
90 choices: ['Grid', 'NotRedundant', 'N+1', 'N+2']
91 default: 'Grid'
92 processor_package_power_limit:
93 description:
94 - Sets the Processor Package Power Limit (PPL) of a server. PPL refers to the amount of power that a CPU can draw from the power supply.
95 - The Processor Package Power Limit (PPL) feature is currently available exclusively on Cisco UCS C225/C245 M8 servers.
96 - Applicable for 'Standalone', 'FIAttached', and 'All' platforms.
97 type: str
98 choices: ['Default', 'Maximum', 'Minimum']
99 default: 'Default'
100 power_save_mode:
101 description:
102 - Sets the power save mode of the chassis.
103 - If the requested power budget is less than available power capacity,
104 the additional PSUs not required to comply with redundancy policy are placed in power save mode.
105 - Applicable for 'Chassis' and 'All' platforms.
106 type: str
107 choices: ['Enabled', 'Disabled']
108 default: 'Enabled'
109 dynamic_power_rebalancing:
110 description:
111 - Sets the dynamic power rebalancing mode of the chassis.
112 - If enabled, this mode allows the chassis to dynamically reallocate the power between servers depending on their power usage.
113 - Applicable for 'Chassis' and 'All' platforms.
114 type: str
115 choices: ['Enabled', 'Disabled']
116 default: 'Enabled'
117 extended_power_capacity:
118 description:
119 - Sets the Extended Power Capacity of the Chassis.
120 - If Enabled, this mode allows chassis available power to be increased by borrowing power from redundant power supplies.
121 - This option is only supported for Cisco UCS X series Chassis.
122 - Applicable for 'Chassis' and 'All' platforms.
123 type: str
124 choices: ['Enabled', 'Disabled']
125 default: 'Enabled'
126 power_allocation:
127 description:
128 - Sets the limit for the maximum input power consumption by the chassis (in Watts). Set to 0 for no limit.
129 - Applicable for 'Chassis' and 'All' platforms.
130 type: int
131 default: 0
132 author:
133 - Ron Gershburg (@rgershbu)
134 '''
135
136 EXAMPLES = r'''
137 - name: Create a Power Policy for a Standalone Server
138 cisco.intersight.intersight_power_policy:
139 api_private_key: "{{ api_private_key }}"
140 api_key_id: "{{ api_key_id }}"
141 name: "Standalone-Server-Power-Policy"
142 description: "Power policy for standalone servers, restores to last state."
143 target_platform: "Standalone"
144 power_restore: "LastState"
145 processor_package_power_limit: "Minimum"
146 tags:
147 - Key: "Owner"
148 Value: "DevOps"
149
150 - name: Create a Power Policy for a Chassis with N+1 Redundancy
151 cisco.intersight.intersight_power_policy:
152 api_private_key: "{{ api_private_key }}"
153 api_key_id: "{{ api_key_id }}"
154 name: "Chassis-N1-Redundancy"
155 description: "Power policy for chassis with N+1 redundancy."
156 target_platform: "Chassis"
157 power_redundancy: "N+1"
158 power_save_mode: "Disabled"
159 power_allocation: 7500
160
161 - name: Create a universal Power Policy for FIAttached Servers
162 cisco.intersight.intersight_power_policy:
163 api_private_key: "{{ api_private_key }}"
164 api_key_id: "{{ api_key_id }}"
165 name: "FI-Attached-Default"
166 description: "Default power policy for all FIAttached servers."
167 target_platform: "FIAttached"
168 power_profiling: "Enabled"
169 power_priority: "Medium"
170
171 - name: Delete a Power Policy
172 cisco.intersight.intersight_power_policy:
173 api_private_key: "{{ api_private_key }}"
174 api_key_id: "{{ api_key_id }}"
175 name: "Standalone-Server-Power-Policy"
176 state: absent
177 '''
178
179 RETURN = r'''
180 api_response:
181 description: The API response output returned by the specified resource.
182 returned: always
183 type: dict
184 sample:
185 "api_response": {
186 "Name": "FI-Attached-Default",
187 "ObjectType": "power.Policy",
188 "PowerPriority": "Medium",
189 "PowerProfiling": "Enabled",
190 "PowerRestoreState": "AlwaysOff",
191 "Tags": [],
192 ...
193 }
194 '''
195
196
197 from ansible.module_utils.basic import AnsibleModule
198 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
199
200 ALL = 'All'
201 STANDALONE_SERVER = 'Standalone'
202 FI_ATTACHED_SERVER = 'FIAttached'
203 CHASSIS = 'Chassis'
204
205
206 def validate_input(module: AnsibleModule):
207 power_allocation = module.params['power_allocation']
208 if 0 > power_allocation or power_allocation > 65535:
209 module.fail_json(msg=f"power_allocation has to be between 0 and 65535, current value is: {power_allocation}")
210
211
212 def main():
213 argument_spec = intersight_argument_spec.copy()
214 argument_spec.update(
215 state=dict(type='str', choices=['present', 'absent'], default='present'),
216 organization=dict(type='str', default='default'),
217 name=dict(type='str', required=True),
218 description=dict(type='str', aliases=['descr']),
219 tags=dict(type='list', elements='dict'),
220 target_platform=dict(
221 type='str',
222 choices=[ALL, STANDALONE_SERVER, FI_ATTACHED_SERVER, CHASSIS]
223 ),
224 power_profiling=dict(
225 type='str',
226 choices=['Enabled', 'Disabled'],
227 default='Enabled'
228 ),
229 power_priority=dict(
230 type='str',
231 choices=['Low', 'Medium', 'High'],
232 default='Low'
233 ),
234 power_restore=dict(
235 type='str',
236 choices=['AlwaysOff', 'AlwaysOn', 'LastState'],
237 default='AlwaysOff'
238 ),
239 power_redundancy=dict(
240 type='str',
241 choices=['Grid', 'NotRedundant', 'N+1', 'N+2'],
242 default='Grid'
243 ),
244 processor_package_power_limit=dict(
245 type='str',
246 choices=['Default', 'Maximum', 'Minimum'],
247 default='Default'
248 ),
249 power_save_mode=dict(
250 type='str',
251 choices=['Enabled', 'Disabled'],
252 default='Enabled'
253 ),
254 dynamic_power_rebalancing=dict(
255 type='str',
256 choices=['Enabled', 'Disabled'],
257 default='Enabled'
258 ),
259 extended_power_capacity=dict(
260 type='str',
261 choices=['Enabled', 'Disabled'],
262 default='Enabled'
263 ),
264 power_allocation=dict(
265 type='int',
266 default=0
267 ),
268 )
269 required_if = [
270 ('state', 'present', ['target_platform']),
271 ]
272 module = AnsibleModule(
273 argument_spec,
274 required_if=required_if,
275 supports_check_mode=True,
276 )
277
278 validate_input(module)
279 intersight = IntersightModule(module)
280 intersight.result['api_response'] = {}
281 intersight.result['trace_id'] = ''
282
283 # Resource path used to configure policy
284 resource_path = '/power/Policies'
285 # Define API body used in compares or create
286 intersight.api_body = {
287 'Organization': {
288 'Name': intersight.module.params['organization'],
289 },
290 'Name': intersight.module.params['name']
291 }
292
293 if module.params['state'] == 'present':
294 intersight.set_tags_and_description()
295
296 platform = module.params['target_platform']
297
298 # Chassis-specific parameters
299 if platform in [CHASSIS, ALL]:
300 intersight.api_body.update({
301 "RedundancyMode": module.params['power_redundancy'],
302 "PowerSaveMode": module.params['power_save_mode'],
303 "DynamicRebalancing": module.params['dynamic_power_rebalancing'],
304 "ExtendedPowerCapacity": module.params['extended_power_capacity'],
305 "AllocatedBudget": module.params['power_allocation']
306 })
307
308 # Server-specific parameters (Standalone and FIAttached)
309 if platform in [STANDALONE_SERVER, FI_ATTACHED_SERVER, ALL]:
310 intersight.api_body.update({
311 "ProcessorPackagePowerLimit": module.params['processor_package_power_limit'],
312 "PowerRestoreState": module.params['power_restore']
313 })
314
315 # FIAttached specific parameters
316 if platform in [FI_ATTACHED_SERVER, ALL]:
317 intersight.api_body.update({
318 "PowerPriority": module.params['power_priority'],
319 "PowerProfiling": module.params['power_profiling']
320 })
321
322 intersight.configure_policy_or_profile(resource_path=resource_path)
323
324 module.exit_json(**intersight.result)
325
326
327 if __name__ == '__main__':
328 main()