| 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_firmware_policy |
| 16 |
short_description: Firmware Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages Firmware Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure firmware settings and versions for Cisco Intersight managed servers. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 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 Firmware 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 Firmware 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 |
default: [] |
| 54 |
target_platform: |
| 55 |
description: |
| 56 |
- The platform type for which the firmware policy is intended. |
| 57 |
- Server type can be either Standalone or FIAttached. |
| 58 |
- This parameter is required when C(state) is C(present). |
| 59 |
type: str |
| 60 |
choices: ['Standalone', 'FIAttached'] |
| 61 |
model_bundle_combo: |
| 62 |
description: |
| 63 |
- List of server model and firmware version pairs. |
| 64 |
- Each entry specifies which firmware version should be used for a specific server model family. |
| 65 |
- This parameter is required when C(state) is C(present). |
| 66 |
type: list |
| 67 |
elements: dict |
| 68 |
suboptions: |
| 69 |
model_family: |
| 70 |
description: |
| 71 |
- The server model family (e.g., UCSC-C220-M5, UCSC-C220-M4, UCSC-C220-M7). |
| 72 |
type: str |
| 73 |
required: true |
| 74 |
bundle_version: |
| 75 |
description: |
| 76 |
- The firmware bundle version to apply to the specified model family. |
| 77 |
type: str |
| 78 |
required: true |
| 79 |
exclude_drives: |
| 80 |
description: |
| 81 |
- When set to C(true), excludes local disk drives from firmware upgrades. |
| 82 |
- This adds local-disk to the ExcludeComponentList. |
| 83 |
type: bool |
| 84 |
default: false |
| 85 |
exclude_storage_controllers: |
| 86 |
description: |
| 87 |
- When set to C(true), excludes storage controllers from firmware upgrades. |
| 88 |
- This adds storage-controller to the ExcludeComponentList. |
| 89 |
type: bool |
| 90 |
default: false |
| 91 |
author: |
| 92 |
- Ron Gershburg (@rgershbu) |
| 93 |
''' |
| 94 |
|
| 95 |
EXAMPLES = r''' |
| 96 |
- name: Create a firmware policy for standalone servers |
| 97 |
cisco.intersight.intersight_firmware_policy: |
| 98 |
api_private_key: "{{ api_private_key }}" |
| 99 |
api_key_id: "{{ api_key_id }}" |
| 100 |
name: "Standalone-Firmware-Policy" |
| 101 |
description: "Firmware policy for standalone servers" |
| 102 |
target_platform: "Standalone" |
| 103 |
model_bundle_combo: |
| 104 |
- model_family: "UCSC-C220-M5" |
| 105 |
bundle_version: "4.3(2.250037)" |
| 106 |
- model_family: "UCSC-C220-M4" |
| 107 |
bundle_version: "4.1(2m)" |
| 108 |
tags: |
| 109 |
- Key: "Environment" |
| 110 |
Value: "Production" |
| 111 |
|
| 112 |
- name: Create a firmware policy with excluded components |
| 113 |
cisco.intersight.intersight_firmware_policy: |
| 114 |
api_private_key: "{{ api_private_key }}" |
| 115 |
api_key_id: "{{ api_key_id }}" |
| 116 |
name: "Firmware-Policy-With-Exclusions" |
| 117 |
description: "Firmware policy excluding storage components" |
| 118 |
target_platform: "Standalone" |
| 119 |
model_bundle_combo: |
| 120 |
- model_family: "UCSC-C220-M7" |
| 121 |
bundle_version: "4.3(4.242038)" |
| 122 |
exclude_drives: true |
| 123 |
exclude_storage_controllers: true |
| 124 |
tags: |
| 125 |
- Key: "Site" |
| 126 |
Value: "Datacenter1" |
| 127 |
|
| 128 |
- name: Delete a firmware policy |
| 129 |
cisco.intersight.intersight_firmware_policy: |
| 130 |
api_private_key: "{{ api_private_key }}" |
| 131 |
api_key_id: "{{ api_key_id }}" |
| 132 |
name: "Old-Firmware-Policy" |
| 133 |
state: absent |
| 134 |
''' |
| 135 |
|
| 136 |
RETURN = r''' |
| 137 |
api_response: |
| 138 |
description: The API response output returned by the specified resource. |
| 139 |
returned: always |
| 140 |
type: dict |
| 141 |
sample: |
| 142 |
"api_response": { |
| 143 |
"Name": "Standalone-Firmware-Policy", |
| 144 |
"TargetPlatform": "Standalone", |
| 145 |
"ModelBundleCombo": [ |
| 146 |
{ |
| 147 |
"ModelFamily": "UCSC-C220-M5", |
| 148 |
"BundleVersion": "4.3(2.250037)" |
| 149 |
} |
| 150 |
], |
| 151 |
"ExcludeComponentList": ["local-disk", "storage-controller"], |
| 152 |
"ObjectType": "firmware.Policy", |
| 153 |
"Tags": [ |
| 154 |
{ |
| 155 |
"Key": "Environment", |
| 156 |
"Value": "Production" |
| 157 |
} |
| 158 |
] |
| 159 |
} |
| 160 |
''' |
| 161 |
|
| 162 |
|
| 163 |
from ansible.module_utils.basic import AnsibleModule |
| 164 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 165 |
|
| 166 |
|
| 167 |
def validate_input(module): |
| 168 |
""" |
| 169 |
Validate input parameters for the firmware policy module. |
| 170 |
""" |
| 171 |
# Validate ModelBundleCombo entries |
| 172 |
model_bundle_combo = module.params.get('model_bundle_combo') |
| 173 |
for combo in model_bundle_combo: |
| 174 |
if not combo.get('model_family'): |
| 175 |
module.fail_json(msg="model_family is required for each entry in model_bundle_combo") |
| 176 |
if not combo.get('bundle_version'): |
| 177 |
module.fail_json(msg="bundle_version is required for each entry in model_bundle_combo") |
| 178 |
|
| 179 |
|
| 180 |
def main(): |
| 181 |
argument_spec = intersight_argument_spec.copy() |
| 182 |
argument_spec.update( |
| 183 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 184 |
organization=dict(type='str', default='default'), |
| 185 |
name=dict(type='str', required=True), |
| 186 |
description=dict(type='str', aliases=['descr']), |
| 187 |
tags=dict(type='list', elements='dict', default=[]), |
| 188 |
target_platform=dict(type='str', choices=['Standalone', 'FIAttached']), |
| 189 |
exclude_drives=dict(type='bool', default=False), |
| 190 |
exclude_storage_controllers=dict(type='bool', default=False), |
| 191 |
model_bundle_combo=dict( |
| 192 |
type='list', |
| 193 |
elements='dict', |
| 194 |
options=dict( |
| 195 |
model_family=dict(type='str', required=True), |
| 196 |
bundle_version=dict(type='str', required=True) |
| 197 |
), |
| 198 |
), |
| 199 |
) |
| 200 |
|
| 201 |
required_if = [ |
| 202 |
['state', 'present', ['target_platform', 'model_bundle_combo']], |
| 203 |
] |
| 204 |
|
| 205 |
module = AnsibleModule( |
| 206 |
argument_spec, |
| 207 |
required_if=required_if, |
| 208 |
supports_check_mode=True, |
| 209 |
) |
| 210 |
if module.params['state'] == 'present': |
| 211 |
validate_input(module) |
| 212 |
intersight = IntersightModule(module) |
| 213 |
intersight.result['api_response'] = {} |
| 214 |
intersight.result['trace_id'] = '' |
| 215 |
|
| 216 |
# Resource path used to configure policy |
| 217 |
resource_path = '/firmware/Policies' |
| 218 |
|
| 219 |
# Define API body used in compares or create |
| 220 |
intersight.api_body = { |
| 221 |
'Organization': { |
| 222 |
'Name': intersight.module.params['organization'], |
| 223 |
}, |
| 224 |
'Name': intersight.module.params['name'] |
| 225 |
} |
| 226 |
|
| 227 |
if module.params['state'] == 'present': |
| 228 |
intersight.set_tags_and_description() |
| 229 |
intersight.api_body['TargetPlatform'] = intersight.module.params['target_platform'] |
| 230 |
|
| 231 |
# Set ModelBundleCombo |
| 232 |
model_bundle_combo = [] |
| 233 |
for combo in module.params['model_bundle_combo']: |
| 234 |
model_bundle_combo.append({ |
| 235 |
'ModelFamily': combo['model_family'], |
| 236 |
'BundleVersion': combo['bundle_version'] |
| 237 |
}) |
| 238 |
intersight.api_body['ModelBundleCombo'] = model_bundle_combo |
| 239 |
|
| 240 |
# Set exclude list |
| 241 |
exclude_components = [] |
| 242 |
if module.params['exclude_drives']: |
| 243 |
exclude_components.append('local-disk') |
| 244 |
if module.params['exclude_storage_controllers']: |
| 245 |
exclude_components.append('storage-controller') |
| 246 |
intersight.api_body['ExcludeComponentList'] = exclude_components |
| 247 |
|
| 248 |
intersight.configure_policy_or_profile(resource_path) |
| 249 |
|
| 250 |
module.exit_json(**intersight.result) |
| 251 |
|
| 252 |
|
| 253 |
if __name__ == '__main__': |
| 254 |
main() |