| 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_resource_group |
| 16 |
short_description: Resource Group configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages Resource Group configuration on Cisco Intersight. |
| 19 |
- Resource Groups define a set of resources that can be used for access control and organization assignment. |
| 20 |
- Users specify Intersight-managed UCS domains by their target name (as shown in the Intersight Targets page). |
| 21 |
- Optionally, specific servers (sub-targets) within a domain can be selected by serial number. |
| 22 |
- Resource Groups are account-level resources and are not scoped to an organization. |
| 23 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/resource/Group/model/). |
| 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 |
name: |
| 34 |
description: |
| 35 |
- The name assigned to the Resource Group. |
| 36 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
description: |
| 40 |
description: |
| 41 |
- The user-defined description for the Resource Group. |
| 42 |
type: str |
| 43 |
aliases: [descr] |
| 44 |
tags: |
| 45 |
description: |
| 46 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 47 |
type: list |
| 48 |
elements: dict |
| 49 |
resources: |
| 50 |
description: |
| 51 |
- List of Intersight-managed UCS domain resources to include in the Resource Group. |
| 52 |
- Required when O(state) is C(present). |
| 53 |
- Each entry specifies a domain by its target name as shown in the Intersight GUI Targets page. |
| 54 |
- When O(resources[].sub_targets) is provided, only the specified servers (by serial number) are included instead of the full domain. |
| 55 |
- Multiple entries can be mixed, some with sub-targets and some without. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
suboptions: |
| 59 |
domain_name: |
| 60 |
description: |
| 61 |
- The name of the Intersight-managed UCS domain target. |
| 62 |
- Must match the target name exactly as displayed in the Intersight Targets page. |
| 63 |
type: str |
| 64 |
required: true |
| 65 |
sub_targets: |
| 66 |
description: |
| 67 |
- List of server serial numbers to include as sub-targets within the domain. |
| 68 |
- When specified, only these servers are included instead of the entire domain. |
| 69 |
- Servers are automatically classified as Blades or RackUnits via the Intersight API. |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
author: |
| 73 |
- Ron Gershburg (@rgershbu) |
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = r''' |
| 77 |
- name: Create Resource Group for full UCS domains |
| 78 |
cisco.intersight.intersight_resource_group: |
| 79 |
api_private_key: "{{ api_private_key }}" |
| 80 |
api_key_id: "{{ api_key_id }}" |
| 81 |
name: "ucs-domains-group" |
| 82 |
description: "Resource group for two UCS domains" |
| 83 |
resources: |
| 84 |
- domain_name: "ucs-domain-1" |
| 85 |
- domain_name: "ucs-domain-2" |
| 86 |
state: present |
| 87 |
|
| 88 |
- name: Create Resource Group with sub-targets (specific servers) |
| 89 |
cisco.intersight.intersight_resource_group: |
| 90 |
api_private_key: "{{ api_private_key }}" |
| 91 |
api_key_id: "{{ api_key_id }}" |
| 92 |
name: "specific-servers-group" |
| 93 |
description: "Only specific servers from the domain" |
| 94 |
resources: |
| 95 |
- domain_name: "ucs-domain-1" |
| 96 |
sub_targets: |
| 97 |
- "FCH26387BD1" |
| 98 |
- "FCH243974ZD" |
| 99 |
- "WZP26030TAV" |
| 100 |
state: present |
| 101 |
|
| 102 |
- name: Create Resource Group with mixed resources |
| 103 |
cisco.intersight.intersight_resource_group: |
| 104 |
api_private_key: "{{ api_private_key }}" |
| 105 |
api_key_id: "{{ api_key_id }}" |
| 106 |
name: "mixed-resources-group" |
| 107 |
description: "Full domain and specific servers from another" |
| 108 |
resources: |
| 109 |
- domain_name: "ucs-domain-1" |
| 110 |
- domain_name: "ucs-domain-2" |
| 111 |
sub_targets: |
| 112 |
- "FCH26387BD1" |
| 113 |
- "WZP26030TAV" |
| 114 |
state: present |
| 115 |
|
| 116 |
- name: Delete Resource Group |
| 117 |
cisco.intersight.intersight_resource_group: |
| 118 |
api_private_key: "{{ api_private_key }}" |
| 119 |
api_key_id: "{{ api_key_id }}" |
| 120 |
name: "ucs-domains-group" |
| 121 |
state: absent |
| 122 |
''' |
| 123 |
|
| 124 |
RETURN = r''' |
| 125 |
api_response: |
| 126 |
description: The API response output returned by the specified resource. |
| 127 |
returned: always |
| 128 |
type: dict |
| 129 |
sample: |
| 130 |
"api_response": { |
| 131 |
"Name": "ucs-domains-group", |
| 132 |
"ObjectType": "resource.Group", |
| 133 |
"Description": "Resource group for two UCS domains", |
| 134 |
"Moid": "507f1f77bcf86cd799439099" |
| 135 |
} |
| 136 |
''' |
| 137 |
|
| 138 |
from ansible.module_utils.basic import AnsibleModule |
| 139 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 140 |
|
| 141 |
|
| 142 |
def resolve_domain_device_registration(intersight, domain_name): |
| 143 |
"""Resolve a domain target name to its DeviceRegistration Moid.""" |
| 144 |
intersight.get_resource( |
| 145 |
resource_path='/asset/Targets', |
| 146 |
query_params={ |
| 147 |
'$filter': f"Name eq '{domain_name}' and TargetType eq 'UCSFIISM'", |
| 148 |
'$select': 'Name,RegisteredDevice', |
| 149 |
'$expand': 'RegisteredDevice($select=Moid)', |
| 150 |
}, |
| 151 |
) |
| 152 |
|
| 153 |
target = intersight.result['api_response'] |
| 154 |
if not target.get('Moid'): |
| 155 |
intersight.module.fail_json( |
| 156 |
msg=f"Intersight-managed UCS domain target '{domain_name}' not found. " |
| 157 |
"Verify the target name matches exactly as shown in the Intersight Targets page." |
| 158 |
) |
| 159 |
|
| 160 |
registered_device = target.get('RegisteredDevice', {}) |
| 161 |
device_reg_moid = registered_device.get('Moid') |
| 162 |
if not device_reg_moid: |
| 163 |
intersight.module.fail_json( |
| 164 |
msg=f"Domain target '{domain_name}' has no associated DeviceRegistration. " |
| 165 |
"Ensure the domain is properly claimed and connected." |
| 166 |
) |
| 167 |
|
| 168 |
return device_reg_moid |
| 169 |
|
| 170 |
|
| 171 |
def classify_serial_numbers(intersight, serials): |
| 172 |
"""Classify serial numbers as Blade or RackUnit via PhysicalSummaries. |
| 173 |
|
| 174 |
Returns: |
| 175 |
Tuple of (blade_serials, rack_serials) lists. |
| 176 |
""" |
| 177 |
serials_filter = ",".join(f"'{s}'" for s in serials) |
| 178 |
intersight.get_resource( |
| 179 |
resource_path='/compute/PhysicalSummaries', |
| 180 |
query_params={ |
| 181 |
'$filter': f"Serial in ({serials_filter}) and ManagementMode eq 'Intersight'", |
| 182 |
'$select': 'Serial,SourceObjectType', |
| 183 |
}, |
| 184 |
return_list=True, |
| 185 |
) |
| 186 |
|
| 187 |
results = intersight.result['api_response'] |
| 188 |
if not isinstance(results, list): |
| 189 |
results = [results] if results else [] |
| 190 |
|
| 191 |
found_serials = {} |
| 192 |
for entry in results: |
| 193 |
serial = entry.get('Serial') |
| 194 |
obj_type = entry.get('SourceObjectType', '') |
| 195 |
if serial: |
| 196 |
found_serials[serial] = obj_type |
| 197 |
|
| 198 |
missing = set(serials) - set(found_serials.keys()) |
| 199 |
if missing: |
| 200 |
intersight.module.fail_json( |
| 201 |
msg=f"The following serial numbers were not found as Intersight-managed servers: {sorted(missing)}" |
| 202 |
) |
| 203 |
|
| 204 |
blade_serials = [s for s in serials if 'Blade' in found_serials[s]] |
| 205 |
rack_serials = [s for s in serials if 'RackUnit' in found_serials[s]] |
| 206 |
|
| 207 |
return blade_serials, rack_serials |
| 208 |
|
| 209 |
|
| 210 |
def resolve_resources_to_selectors(intersight, resources): |
| 211 |
"""Resolve user-provided resources into API selector strings.""" |
| 212 |
device_reg_moids = [] |
| 213 |
all_blade_serials = [] |
| 214 |
all_rack_serials = [] |
| 215 |
|
| 216 |
for resource in resources: |
| 217 |
domain_name = resource['domain_name'] |
| 218 |
sub_targets = resource.get('sub_targets') |
| 219 |
|
| 220 |
if sub_targets: |
| 221 |
blade_serials, rack_serials = classify_serial_numbers(intersight, sub_targets) |
| 222 |
all_blade_serials.extend(blade_serials) |
| 223 |
all_rack_serials.extend(rack_serials) |
| 224 |
else: |
| 225 |
moid = resolve_domain_device_registration(intersight, domain_name) |
| 226 |
device_reg_moids.append(moid) |
| 227 |
|
| 228 |
selectors = [] |
| 229 |
|
| 230 |
if device_reg_moids: |
| 231 |
moids_str = ",".join(f"'{m}'" for m in device_reg_moids) |
| 232 |
selectors.append( |
| 233 |
f"/api/v1/asset/DeviceRegistrations?$filter=Moid in ({moids_str})" |
| 234 |
) |
| 235 |
|
| 236 |
if all_blade_serials: |
| 237 |
serials_str = ",".join(f"'{s}'" for s in all_blade_serials) |
| 238 |
selectors.append( |
| 239 |
f"/api/v1/compute/Blades?$filter=Serial in ({serials_str}) and ManagementMode eq 'Intersight'" |
| 240 |
) |
| 241 |
|
| 242 |
if all_rack_serials: |
| 243 |
serials_str = ",".join(f"'{s}'" for s in all_rack_serials) |
| 244 |
selectors.append( |
| 245 |
f"/api/v1/compute/RackUnits?$filter=Serial in ({serials_str}) and ManagementMode eq 'Intersight'" |
| 246 |
) |
| 247 |
|
| 248 |
return selectors |
| 249 |
|
| 250 |
|
| 251 |
def build_api_body(intersight): |
| 252 |
"""Build the API body for Resource Group configuration.""" |
| 253 |
params = intersight.module.params |
| 254 |
if params['state'] == 'present': |
| 255 |
selectors = resolve_resources_to_selectors(intersight, params['resources']) |
| 256 |
|
| 257 |
intersight.api_body = { |
| 258 |
'Name': params['name'], |
| 259 |
'Qualifier': 'Allow-Selectors', |
| 260 |
'Selectors': [{'Selector': s} for s in selectors], |
| 261 |
} |
| 262 |
|
| 263 |
intersight.set_tags_and_description() |
| 264 |
|
| 265 |
|
| 266 |
def main(): |
| 267 |
argument_spec = intersight_argument_spec.copy() |
| 268 |
argument_spec.update( |
| 269 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 270 |
name=dict(type='str', required=True), |
| 271 |
description=dict(type='str', aliases=['descr']), |
| 272 |
tags=dict(type='list', elements='dict'), |
| 273 |
resources=dict(type='list', elements='dict', options=dict( |
| 274 |
domain_name=dict(type='str', required=True), |
| 275 |
sub_targets=dict(type='list', elements='str'), |
| 276 |
)), |
| 277 |
) |
| 278 |
|
| 279 |
module = AnsibleModule( |
| 280 |
argument_spec, |
| 281 |
supports_check_mode=True, |
| 282 |
required_if=[ |
| 283 |
['state', 'present', ['resources']], |
| 284 |
], |
| 285 |
) |
| 286 |
|
| 287 |
intersight = IntersightModule(module) |
| 288 |
intersight.result['api_response'] = {} |
| 289 |
intersight.result['trace_id'] = '' |
| 290 |
|
| 291 |
build_api_body(intersight) |
| 292 |
|
| 293 |
resource_path = '/resource/Groups' |
| 294 |
|
| 295 |
intersight.configure_secondary_resource( |
| 296 |
resource_path=resource_path, |
| 297 |
resource_name=module.params['name'], |
| 298 |
state=module.params['state'], |
| 299 |
) |
| 300 |
|
| 301 |
module.exit_json(**intersight.result) |
| 302 |
|
| 303 |
|
| 304 |
if __name__ == '__main__': |
| 305 |
main() |