| 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_domain |
| 16 |
short_description: Manage UCS Domain Profiles in Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete UCS Domain Profiles (SwitchClusterProfiles) on Cisco Intersight. |
| 19 |
- Manages the associated SwitchProfiles (A and B), Fabric Interconnect assignments, and policy buckets. |
| 20 |
- Policies are attached to SwitchProfiles via the Intersight bulk API. |
| 21 |
- This module does not manage deployment of the domain profile. |
| 22 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/fabric/SwitchClusterProfiles/post/). |
| 23 |
extends_documentation_fragment: intersight |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- If C(present), will verify the resource is present and will create if needed. |
| 28 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 29 |
type: str |
| 30 |
choices: [present, absent] |
| 31 |
default: present |
| 32 |
organization: |
| 33 |
description: |
| 34 |
- The name of the Organization this resource is assigned to. |
| 35 |
- Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 36 |
type: str |
| 37 |
default: default |
| 38 |
name: |
| 39 |
description: |
| 40 |
- The name assigned to the UCS Domain Profile. |
| 41 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 42 |
- SwitchProfiles are automatically named C({name}-A) and C({name}-B). |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
description: |
| 46 |
description: |
| 47 |
- The user-defined description of the UCS Domain Profile. |
| 48 |
type: str |
| 49 |
aliases: [descr] |
| 50 |
tags: |
| 51 |
description: |
| 52 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 53 |
type: list |
| 54 |
elements: dict |
| 55 |
assigned_switch_a_serial: |
| 56 |
description: |
| 57 |
- The serial number of the Fabric Interconnect to assign to SwitchProfile A. |
| 58 |
- Resolved via the C(/network/ElementSummaries) API endpoint using the C(Serial) field. |
| 59 |
type: str |
| 60 |
assigned_switch_b_serial: |
| 61 |
description: |
| 62 |
- The serial number of the Fabric Interconnect to assign to SwitchProfile B. |
| 63 |
- Resolved via the C(/network/ElementSummaries) API endpoint using the C(Serial) field. |
| 64 |
type: str |
| 65 |
vlan_policy_fi_a: |
| 66 |
description: |
| 67 |
- Name of the VLAN Policy to associate with Fabric Interconnect A. |
| 68 |
type: str |
| 69 |
vlan_policy_fi_b: |
| 70 |
description: |
| 71 |
- Name of the VLAN Policy to associate with Fabric Interconnect B. |
| 72 |
type: str |
| 73 |
vsan_policy_fi_a: |
| 74 |
description: |
| 75 |
- Name of the VSAN Policy to associate with Fabric Interconnect A. |
| 76 |
type: str |
| 77 |
vsan_policy_fi_b: |
| 78 |
description: |
| 79 |
- Name of the VSAN Policy to associate with Fabric Interconnect B. |
| 80 |
type: str |
| 81 |
port_policy_fi_a: |
| 82 |
description: |
| 83 |
- Name of the Port Policy to associate with Fabric Interconnect A. |
| 84 |
type: str |
| 85 |
port_policy_fi_b: |
| 86 |
description: |
| 87 |
- Name of the Port Policy to associate with Fabric Interconnect B. |
| 88 |
type: str |
| 89 |
ntp_policy: |
| 90 |
description: |
| 91 |
- Name of the NTP Policy to associate with both Fabric Interconnects. |
| 92 |
type: str |
| 93 |
syslog_policy: |
| 94 |
description: |
| 95 |
- Name of the Syslog Policy to associate with both Fabric Interconnects. |
| 96 |
type: str |
| 97 |
network_connectivity_policy: |
| 98 |
description: |
| 99 |
- Name of the Network Connectivity (DNS) Policy to associate with both Fabric Interconnects. |
| 100 |
type: str |
| 101 |
snmp_policy: |
| 102 |
description: |
| 103 |
- Name of the SNMP Policy to associate with both Fabric Interconnects. |
| 104 |
type: str |
| 105 |
ldap_policy: |
| 106 |
description: |
| 107 |
- Name of the LDAP Policy to associate with both Fabric Interconnects. |
| 108 |
type: str |
| 109 |
certificate_management_policy: |
| 110 |
description: |
| 111 |
- Name of the Certificate Management Policy to associate with both Fabric Interconnects. |
| 112 |
type: str |
| 113 |
system_qos_policy: |
| 114 |
description: |
| 115 |
- Name of the System QoS Policy to associate with both Fabric Interconnects. |
| 116 |
- This policy is mandatory for UCS Domain Profiles. |
| 117 |
type: str |
| 118 |
auditd_policy: |
| 119 |
description: |
| 120 |
- Name of the Audit Log Policy to associate with both Fabric Interconnects. |
| 121 |
type: str |
| 122 |
switch_control_policy: |
| 123 |
description: |
| 124 |
- Name of the Switch Control Policy to associate with both Fabric Interconnects. |
| 125 |
type: str |
| 126 |
author: |
| 127 |
- Ron Gershburg (@rgershbu) |
| 128 |
''' |
| 129 |
|
| 130 |
EXAMPLES = r''' |
| 131 |
- name: Create a basic UCS Domain Profile |
| 132 |
cisco.intersight.intersight_domain: |
| 133 |
api_private_key: "{{ api_private_key }}" |
| 134 |
api_key_id: "{{ api_key_id }}" |
| 135 |
organization: "default" |
| 136 |
name: "Domain-01" |
| 137 |
description: "Basic domain profile" |
| 138 |
system_qos_policy: "Default-QoS" |
| 139 |
state: present |
| 140 |
|
| 141 |
- name: Create a UCS Domain Profile with switch assignments and policies |
| 142 |
cisco.intersight.intersight_domain: |
| 143 |
api_private_key: "{{ api_private_key }}" |
| 144 |
api_key_id: "{{ api_key_id }}" |
| 145 |
organization: "default" |
| 146 |
name: "Domain-01" |
| 147 |
description: "Full domain profile" |
| 148 |
assigned_switch_a_serial: "FDO23456ABC" |
| 149 |
assigned_switch_b_serial: "FDO23456DEF" |
| 150 |
vlan_policy_fi_a: "VLAN-Policy-A" |
| 151 |
vlan_policy_fi_b: "VLAN-Policy-B" |
| 152 |
vsan_policy_fi_a: "VSAN-Policy-A" |
| 153 |
vsan_policy_fi_b: "VSAN-Policy-B" |
| 154 |
port_policy_fi_a: "Port-Policy-A" |
| 155 |
port_policy_fi_b: "Port-Policy-B" |
| 156 |
ntp_policy: "NTP-Corp" |
| 157 |
syslog_policy: "Syslog-Corp" |
| 158 |
snmp_policy: "SNMP-Monitor" |
| 159 |
system_qos_policy: "QoS-Default" |
| 160 |
switch_control_policy: "Switch-Control" |
| 161 |
state: present |
| 162 |
|
| 163 |
- name: Delete a UCS Domain Profile |
| 164 |
cisco.intersight.intersight_domain: |
| 165 |
api_private_key: "{{ api_private_key }}" |
| 166 |
api_key_id: "{{ api_key_id }}" |
| 167 |
name: "Domain-01" |
| 168 |
state: absent |
| 169 |
''' |
| 170 |
|
| 171 |
RETURN = r''' |
| 172 |
api_response: |
| 173 |
description: The API response output returned by the SwitchClusterProfile resource. |
| 174 |
returned: always |
| 175 |
type: dict |
| 176 |
sample: |
| 177 |
"api_response": { |
| 178 |
"Name": "Domain-01", |
| 179 |
"ObjectType": "fabric.SwitchClusterProfile", |
| 180 |
"Organization": { |
| 181 |
"Moid": "675450ee69726530014753e2", |
| 182 |
"ObjectType": "organization.Organization" |
| 183 |
}, |
| 184 |
"Tags": [ |
| 185 |
{ |
| 186 |
"Key": "Environment", |
| 187 |
"Value": "Production" |
| 188 |
} |
| 189 |
] |
| 190 |
} |
| 191 |
''' |
| 192 |
|
| 193 |
|
| 194 |
from ansible.module_utils.basic import AnsibleModule |
| 195 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import ( |
| 196 |
IntersightModule, |
| 197 |
intersight_argument_spec, |
| 198 |
resolve_policy_bucket, |
| 199 |
sync_policy_bucket, |
| 200 |
) |
| 201 |
|
| 202 |
|
| 203 |
PER_FI_A_POLICY_MAPPING = { |
| 204 |
'vlan_policy_fi_a': {'resource_path': '/fabric/EthNetworkPolicies', 'object_type': 'fabric.EthNetworkPolicy'}, |
| 205 |
'vsan_policy_fi_a': {'resource_path': '/fabric/FcNetworkPolicies', 'object_type': 'fabric.FcNetworkPolicy'}, |
| 206 |
'port_policy_fi_a': {'resource_path': '/fabric/PortPolicies', 'object_type': 'fabric.PortPolicy'}, |
| 207 |
} |
| 208 |
|
| 209 |
PER_FI_B_POLICY_MAPPING = { |
| 210 |
'vlan_policy_fi_b': {'resource_path': '/fabric/EthNetworkPolicies', 'object_type': 'fabric.EthNetworkPolicy'}, |
| 211 |
'vsan_policy_fi_b': {'resource_path': '/fabric/FcNetworkPolicies', 'object_type': 'fabric.FcNetworkPolicy'}, |
| 212 |
'port_policy_fi_b': {'resource_path': '/fabric/PortPolicies', 'object_type': 'fabric.PortPolicy'}, |
| 213 |
} |
| 214 |
|
| 215 |
SHARED_POLICY_MAPPING = { |
| 216 |
'system_qos_policy': {'resource_path': '/fabric/SystemQosPolicies', 'object_type': 'fabric.SystemQosPolicy'}, |
| 217 |
'ntp_policy': {'resource_path': '/ntp/Policies', 'object_type': 'ntp.Policy'}, |
| 218 |
'syslog_policy': {'resource_path': '/syslog/Policies', 'object_type': 'syslog.Policy'}, |
| 219 |
'network_connectivity_policy': {'resource_path': '/networkconfig/Policies', 'object_type': 'networkconfig.Policy'}, |
| 220 |
'snmp_policy': {'resource_path': '/snmp/Policies', 'object_type': 'snmp.Policy'}, |
| 221 |
'ldap_policy': {'resource_path': '/iam/LdapPolicies', 'object_type': 'iam.LdapPolicy'}, |
| 222 |
'certificate_management_policy': {'resource_path': '/certificatemanagement/Policies', 'object_type': 'certificatemanagement.Policy'}, |
| 223 |
'auditd_policy': {'resource_path': '/auditd/Policies', 'object_type': 'auditd.Policy'}, |
| 224 |
'switch_control_policy': {'resource_path': '/fabric/SwitchControlPolicies', 'object_type': 'fabric.SwitchControlPolicy'}, |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
def resolve_switch_moid_by_serial(intersight, serial): |
| 229 |
"""Resolve a Fabric Interconnect serial number to its MOID via /network/ElementSummaries.""" |
| 230 |
intersight.get_resource( |
| 231 |
resource_path='/network/ElementSummaries', |
| 232 |
query_params={ |
| 233 |
'$filter': f"Serial eq '{serial}'", |
| 234 |
'$select': 'Moid', |
| 235 |
}, |
| 236 |
) |
| 237 |
moid = intersight.result['api_response'].get('Moid') |
| 238 |
if not moid: |
| 239 |
intersight.module.fail_json( |
| 240 |
msg=f"Fabric Interconnect with serial '{serial}' not found." |
| 241 |
) |
| 242 |
return moid |
| 243 |
|
| 244 |
|
| 245 |
def ensure_switch_profile(intersight, domain_name, switch_id, cluster_moid): |
| 246 |
"""Ensure a SwitchProfile exists for the given switch ID, return its MOID.""" |
| 247 |
profile_name = f"{domain_name}-{switch_id}" |
| 248 |
filter_str = ( |
| 249 |
f"Name eq '{profile_name}'" |
| 250 |
f" and SwitchClusterProfile.Moid eq '{cluster_moid}'" |
| 251 |
) |
| 252 |
|
| 253 |
intersight.get_resource( |
| 254 |
resource_path='/fabric/SwitchProfiles', |
| 255 |
query_params={'$filter': filter_str}, |
| 256 |
) |
| 257 |
|
| 258 |
if intersight.result['api_response'].get('Moid'): |
| 259 |
return intersight.result['api_response']['Moid'] |
| 260 |
|
| 261 |
body = { |
| 262 |
'Name': profile_name, |
| 263 |
'SwitchId': switch_id, |
| 264 |
'SwitchClusterProfile': {'Moid': cluster_moid}, |
| 265 |
} |
| 266 |
intersight.configure_resource( |
| 267 |
moid=None, |
| 268 |
resource_path='/fabric/SwitchProfiles', |
| 269 |
body=body, |
| 270 |
query_params={'$filter': filter_str}, |
| 271 |
) |
| 272 |
return intersight.result['api_response'].get('Moid') |
| 273 |
|
| 274 |
|
| 275 |
def configure_switch_profiles(intersight, domain_name, cluster_moid): |
| 276 |
"""Create SwitchProfiles A/B and assign Fabric Interconnects if provided. |
| 277 |
|
| 278 |
Returns a list of (profile_moid, profile_state) tuples for each FI. |
| 279 |
""" |
| 280 |
fi_configs = [ |
| 281 |
{'switch_id': 'A', 'switch_param': 'assigned_switch_a_serial'}, |
| 282 |
{'switch_id': 'B', 'switch_param': 'assigned_switch_b_serial'}, |
| 283 |
] |
| 284 |
|
| 285 |
profile_results = [] |
| 286 |
for fi in fi_configs: |
| 287 |
profile_moid = ensure_switch_profile(intersight, domain_name, fi['switch_id'], cluster_moid) |
| 288 |
profile_state = dict(intersight.result['api_response']) |
| 289 |
|
| 290 |
switch_serial = intersight.module.params.get(fi['switch_param']) |
| 291 |
if switch_serial and profile_moid: |
| 292 |
switch_moid = resolve_switch_moid_by_serial(intersight, switch_serial) |
| 293 |
current_switch = (profile_state.get('AssignedSwitch') or {}).get('Moid') |
| 294 |
if current_switch != switch_moid: |
| 295 |
intersight.configure_resource( |
| 296 |
moid=profile_moid, |
| 297 |
resource_path='/fabric/SwitchProfiles', |
| 298 |
body={'AssignedSwitch': {'Moid': switch_moid}}, |
| 299 |
query_params={}, |
| 300 |
) |
| 301 |
|
| 302 |
profile_results.append((profile_moid, profile_state)) |
| 303 |
|
| 304 |
return profile_results |
| 305 |
|
| 306 |
|
| 307 |
def build_policy_sync_data(intersight, organization, profile_results): |
| 308 |
"""Resolve desired vs current PolicyBuckets for each SwitchProfile. |
| 309 |
|
| 310 |
Returns a list of (profile_moid, desired_bucket, current_bucket) tuples |
| 311 |
ready for sync_domain_policy_buckets. |
| 312 |
""" |
| 313 |
per_fi_mappings = [PER_FI_A_POLICY_MAPPING, PER_FI_B_POLICY_MAPPING] |
| 314 |
shared_bucket = resolve_policy_bucket(intersight, organization, SHARED_POLICY_MAPPING) |
| 315 |
|
| 316 |
profiles_to_sync = [] |
| 317 |
for (profile_moid, profile_state), per_fi_mapping in zip(profile_results, per_fi_mappings): |
| 318 |
if not profile_moid: |
| 319 |
continue |
| 320 |
desired_bucket = resolve_policy_bucket(intersight, organization, per_fi_mapping) + shared_bucket |
| 321 |
current_bucket = profile_state.get('PolicyBucket') or [] |
| 322 |
profiles_to_sync.append((profile_moid, desired_bucket, current_bucket)) |
| 323 |
|
| 324 |
return profiles_to_sync |
| 325 |
|
| 326 |
|
| 327 |
def unassign_switches_before_delete(intersight, cluster_moid): |
| 328 |
"""Unassign switches from SwitchProfiles before deleting the domain. |
| 329 |
|
| 330 |
The Intersight API may prevent deletion of a domain profile while |
| 331 |
switches are assigned to its SwitchProfiles. |
| 332 |
""" |
| 333 |
intersight.get_resource( |
| 334 |
resource_path='/fabric/SwitchProfiles', |
| 335 |
query_params={ |
| 336 |
'$filter': f"SwitchClusterProfile.Moid eq '{cluster_moid}'" |
| 337 |
}, |
| 338 |
return_list=True, |
| 339 |
) |
| 340 |
|
| 341 |
profiles = intersight.result['api_response'] |
| 342 |
if not isinstance(profiles, list): |
| 343 |
profiles = [profiles] if profiles else [] |
| 344 |
|
| 345 |
for profile in profiles: |
| 346 |
if not profile.get('Moid'): |
| 347 |
continue |
| 348 |
assigned = profile.get('AssignedSwitch') |
| 349 |
if assigned and assigned.get('Moid'): |
| 350 |
intersight.configure_resource( |
| 351 |
moid=profile['Moid'], |
| 352 |
resource_path='/fabric/SwitchProfiles', |
| 353 |
body={'AssignedSwitch': None}, |
| 354 |
query_params={}, |
| 355 |
) |
| 356 |
|
| 357 |
|
| 358 |
def main(): |
| 359 |
argument_spec = intersight_argument_spec.copy() |
| 360 |
argument_spec.update( |
| 361 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 362 |
organization=dict(type='str', default='default'), |
| 363 |
name=dict(type='str', required=True), |
| 364 |
description=dict(type='str', aliases=['descr']), |
| 365 |
tags=dict(type='list', elements='dict'), |
| 366 |
assigned_switch_a_serial=dict(type='str'), |
| 367 |
assigned_switch_b_serial=dict(type='str'), |
| 368 |
vlan_policy_fi_a=dict(type='str'), |
| 369 |
vlan_policy_fi_b=dict(type='str'), |
| 370 |
vsan_policy_fi_a=dict(type='str'), |
| 371 |
vsan_policy_fi_b=dict(type='str'), |
| 372 |
port_policy_fi_a=dict(type='str'), |
| 373 |
port_policy_fi_b=dict(type='str'), |
| 374 |
ntp_policy=dict(type='str'), |
| 375 |
syslog_policy=dict(type='str'), |
| 376 |
network_connectivity_policy=dict(type='str'), |
| 377 |
snmp_policy=dict(type='str'), |
| 378 |
ldap_policy=dict(type='str'), |
| 379 |
certificate_management_policy=dict(type='str'), |
| 380 |
system_qos_policy=dict(type='str'), |
| 381 |
auditd_policy=dict(type='str'), |
| 382 |
switch_control_policy=dict(type='str'), |
| 383 |
) |
| 384 |
|
| 385 |
module = AnsibleModule( |
| 386 |
argument_spec, |
| 387 |
supports_check_mode=True, |
| 388 |
required_if=[ |
| 389 |
['state', 'present', ['system_qos_policy']], |
| 390 |
], |
| 391 |
required_together=[ |
| 392 |
['assigned_switch_a_serial', 'assigned_switch_b_serial'], |
| 393 |
], |
| 394 |
) |
| 395 |
|
| 396 |
intersight = IntersightModule(module) |
| 397 |
intersight.result['api_response'] = {} |
| 398 |
intersight.result['trace_id'] = '' |
| 399 |
|
| 400 |
cluster_path = '/fabric/SwitchClusterProfiles' |
| 401 |
name = intersight.module.params['name'] |
| 402 |
organization = intersight.module.params['organization'] |
| 403 |
state = intersight.module.params['state'] |
| 404 |
|
| 405 |
intersight.api_body = { |
| 406 |
'Organization': {'Name': organization}, |
| 407 |
'Name': name, |
| 408 |
'TargetPlatform': 'UCS Domain', |
| 409 |
} |
| 410 |
|
| 411 |
if state == 'present': |
| 412 |
intersight.set_tags_and_description() |
| 413 |
|
| 414 |
if state == 'absent': |
| 415 |
organization_moid = intersight.get_moid_by_name( |
| 416 |
resource_path='/organization/Organizations', |
| 417 |
resource_name=organization, |
| 418 |
) |
| 419 |
if organization_moid: |
| 420 |
filter_str = ( |
| 421 |
f"Name eq '{name}'" |
| 422 |
f" and Organization.Moid eq '{organization_moid}'" |
| 423 |
) |
| 424 |
intersight.get_resource( |
| 425 |
resource_path=cluster_path, |
| 426 |
query_params={'$filter': filter_str}, |
| 427 |
) |
| 428 |
cluster_moid = intersight.result['api_response'].get('Moid') |
| 429 |
if cluster_moid: |
| 430 |
unassign_switches_before_delete(intersight, cluster_moid) |
| 431 |
|
| 432 |
cluster_moid = intersight.configure_policy_or_profile(resource_path=cluster_path) |
| 433 |
cluster_response = dict(intersight.result['api_response']) if intersight.result['api_response'] else {} |
| 434 |
|
| 435 |
if cluster_moid and state == 'present': |
| 436 |
profile_results = configure_switch_profiles(intersight, name, cluster_moid) |
| 437 |
profiles_to_sync = build_policy_sync_data(intersight, organization, profile_results) |
| 438 |
for profile_moid, desired_bucket, current_bucket in profiles_to_sync: |
| 439 |
bucket_path = f'/fabric/SwitchProfiles/{profile_moid}/PolicyBucket' |
| 440 |
sync_policy_bucket(intersight, bucket_path, desired_bucket, current_bucket) |
| 441 |
|
| 442 |
if cluster_response: |
| 443 |
intersight.result['api_response'] = cluster_response |
| 444 |
|
| 445 |
module.exit_json(**intersight.result) |
| 446 |
|
| 447 |
|
| 448 |
if __name__ == '__main__': |
| 449 |
main() |