| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Sabari Jaganathan <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_fabric_interface_policy_group |
| 16 |
short_description: Manage Fabric Interface Policy Groups (fabric:LePortPGrp and fabric:SpPortPGrp) |
| 17 |
description: |
| 18 |
- Manage Fabric Interface Policy Groups on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the Fabric Leaf or Spine Interface Policy Group. |
| 23 |
type: str |
| 24 |
aliases: [ policy_group ] |
| 25 |
description: |
| 26 |
description: |
| 27 |
- The description of the Fabric Leaf or Spine Interface Policy Group. |
| 28 |
type: str |
| 29 |
aliases: [ descr ] |
| 30 |
type: |
| 31 |
description: |
| 32 |
- The type of the Fabric Leaf or Spine Interface Policy Group. |
| 33 |
- Use C(leaf) to create a Fabric Leaf Interface Policy Group. |
| 34 |
- Use C(spine) to create a Fabric Spine Interface Policy Group. |
| 35 |
type: str |
| 36 |
aliases: [ policy_group_type ] |
| 37 |
choices: [ leaf, spine ] |
| 38 |
required: true |
| 39 |
dwdm_policy: |
| 40 |
description: |
| 41 |
- The name of the DWDM policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 42 |
type: str |
| 43 |
link_level_policy: |
| 44 |
description: |
| 45 |
- The name of the Link Level policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 46 |
type: str |
| 47 |
link_flap_policy: |
| 48 |
description: |
| 49 |
- The name of the Link Flap policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 50 |
type: str |
| 51 |
l3_interface_policy: |
| 52 |
description: |
| 53 |
- The name of the L3 Interface policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 54 |
type: str |
| 55 |
macsec_policy: |
| 56 |
description: |
| 57 |
- The name of the MACSec policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 58 |
type: str |
| 59 |
monitoring_policy: |
| 60 |
description: |
| 61 |
- The name of the Monitoring policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 62 |
type: str |
| 63 |
transceiver_policy_tdn: |
| 64 |
description: |
| 65 |
- The target Dn of the Transceiver policy to bind to the Fabric Leaf or Spine Interface Policy Group. |
| 66 |
- The Transceiver policy group is only compatible with ACI versions 6.0(2h) and higher. |
| 67 |
type: str |
| 68 |
state: |
| 69 |
description: |
| 70 |
- Use C(present) or C(absent) for adding or removing. |
| 71 |
- Use C(query) for listing an object or multiple objects. |
| 72 |
type: str |
| 73 |
choices: [ absent, present, query ] |
| 74 |
default: present |
| 75 |
name_alias: |
| 76 |
description: |
| 77 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 78 |
type: str |
| 79 |
extends_documentation_fragment: |
| 80 |
- cisco.aci.aci |
| 81 |
- cisco.aci.annotation |
| 82 |
- cisco.aci.owner |
| 83 |
|
| 84 |
seealso: |
| 85 |
- name: APIC Management Information Model reference |
| 86 |
description: More information about the internal APIC class B(fabric:LePortPGrp, fabric:SpPortPGrp). |
| 87 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 88 |
author: |
| 89 |
- Sabari Jaganathan (@sajagana) |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Add a Fabric Leaf Policy Group |
| 94 |
cisco.aci.aci_fabric_interface_policy_group: |
| 95 |
host: apic |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
name: leaf_policy_group |
| 99 |
type: leaf |
| 100 |
state: present |
| 101 |
delegate_to: localhost |
| 102 |
|
| 103 |
- name: Query a Fabric Leaf Policy Group with name |
| 104 |
cisco.aci.aci_fabric_interface_policy_group: |
| 105 |
host: apic |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
name: leaf_policy_group |
| 109 |
type: leaf |
| 110 |
state: query |
| 111 |
delegate_to: localhost |
| 112 |
register: query_result |
| 113 |
|
| 114 |
- name: Query all Fabric Leaf Policy Groups |
| 115 |
cisco.aci.aci_fabric_interface_policy_group: |
| 116 |
host: apic |
| 117 |
username: admin |
| 118 |
password: SomeSecretPassword |
| 119 |
type: leaf |
| 120 |
state: query |
| 121 |
delegate_to: localhost |
| 122 |
register: query_result |
| 123 |
|
| 124 |
- name: Remove a Fabric Leaf Policy Group |
| 125 |
cisco.aci.aci_fabric_interface_policy_group: |
| 126 |
host: apic |
| 127 |
username: admin |
| 128 |
password: SomeSecretPassword |
| 129 |
name: leaf_policy_group |
| 130 |
type: leaf |
| 131 |
state: absent |
| 132 |
delegate_to: localhost |
| 133 |
""" |
| 134 |
|
| 135 |
RETURN = r""" |
| 136 |
current: |
| 137 |
description: The existing configuration from the APIC after the module has finished |
| 138 |
returned: success |
| 139 |
type: list |
| 140 |
sample: |
| 141 |
[ |
| 142 |
{ |
| 143 |
"fvTenant": { |
| 144 |
"attributes": { |
| 145 |
"descr": "Production environment", |
| 146 |
"dn": "uni/tn-production", |
| 147 |
"name": "production", |
| 148 |
"nameAlias": "", |
| 149 |
"ownerKey": "", |
| 150 |
"ownerTag": "" |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
] |
| 155 |
error: |
| 156 |
description: The error information as returned from the APIC |
| 157 |
returned: failure |
| 158 |
type: dict |
| 159 |
sample: |
| 160 |
{ |
| 161 |
"code": "122", |
| 162 |
"text": "unknown managed object class foo" |
| 163 |
} |
| 164 |
raw: |
| 165 |
description: The raw output returned by the APIC REST API (xml or json) |
| 166 |
returned: parse error |
| 167 |
type: str |
| 168 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 169 |
sent: |
| 170 |
description: The actual/minimal configuration pushed to the APIC |
| 171 |
returned: info |
| 172 |
type: list |
| 173 |
sample: |
| 174 |
{ |
| 175 |
"fvTenant": { |
| 176 |
"attributes": { |
| 177 |
"descr": "Production environment" |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
previous: |
| 182 |
description: The original configuration from the APIC before the module has started |
| 183 |
returned: info |
| 184 |
type: list |
| 185 |
sample: |
| 186 |
[ |
| 187 |
{ |
| 188 |
"fvTenant": { |
| 189 |
"attributes": { |
| 190 |
"descr": "Production", |
| 191 |
"dn": "uni/tn-production", |
| 192 |
"name": "production", |
| 193 |
"nameAlias": "", |
| 194 |
"ownerKey": "", |
| 195 |
"ownerTag": "" |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
] |
| 200 |
proposed: |
| 201 |
description: The assembled configuration from the user-provided parameters |
| 202 |
returned: info |
| 203 |
type: dict |
| 204 |
sample: |
| 205 |
{ |
| 206 |
"fvTenant": { |
| 207 |
"attributes": { |
| 208 |
"descr": "Production environment", |
| 209 |
"name": "production" |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
filter_string: |
| 214 |
description: The filter string used for the request |
| 215 |
returned: failure or debug |
| 216 |
type: str |
| 217 |
sample: ?rsp-prop-include=config-only |
| 218 |
method: |
| 219 |
description: The HTTP method used for the request to the APIC |
| 220 |
returned: failure or debug |
| 221 |
type: str |
| 222 |
sample: POST |
| 223 |
response: |
| 224 |
description: The HTTP response from the APIC |
| 225 |
returned: failure or debug |
| 226 |
type: str |
| 227 |
sample: OK (30 bytes) |
| 228 |
status: |
| 229 |
description: The HTTP status from the APIC |
| 230 |
returned: failure or debug |
| 231 |
type: int |
| 232 |
sample: 200 |
| 233 |
url: |
| 234 |
description: The HTTP url used for the request to the APIC |
| 235 |
returned: failure or debug |
| 236 |
type: str |
| 237 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 238 |
""" |
| 239 |
|
| 240 |
from ansible.module_utils.basic import AnsibleModule |
| 241 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
argument_spec = aci_argument_spec() |
| 246 |
argument_spec.update(aci_annotation_spec()) |
| 247 |
argument_spec.update(aci_owner_spec()) |
| 248 |
argument_spec.update( |
| 249 |
name=dict(type="str", aliases=["policy_group"]), |
| 250 |
description=dict(type="str", aliases=["descr"]), |
| 251 |
name_alias=dict(type="str"), |
| 252 |
type=dict(type="str", aliases=["policy_group_type"], choices=["leaf", "spine"], required=True), |
| 253 |
dwdm_policy=dict(type="str"), |
| 254 |
link_level_policy=dict(type="str"), |
| 255 |
link_flap_policy=dict(type="str"), |
| 256 |
l3_interface_policy=dict(type="str"), |
| 257 |
macsec_policy=dict(type="str"), |
| 258 |
monitoring_policy=dict(type="str"), |
| 259 |
transceiver_policy_tdn=dict(type="str"), |
| 260 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 261 |
) |
| 262 |
|
| 263 |
module = AnsibleModule( |
| 264 |
argument_spec=argument_spec, |
| 265 |
supports_check_mode=True, |
| 266 |
required_if=[ |
| 267 |
["state", "absent", ["name"]], |
| 268 |
["state", "present", ["name"]], |
| 269 |
], |
| 270 |
) |
| 271 |
|
| 272 |
aci = ACIModule(module) |
| 273 |
|
| 274 |
name = module.params.get("name") |
| 275 |
description = module.params.get("description") |
| 276 |
state = module.params.get("state") |
| 277 |
name_alias = module.params.get("name_alias") |
| 278 |
policy_group_type = module.params.get("type") |
| 279 |
dwdm_policy = module.params.get("dwdm_policy") |
| 280 |
link_level_policy = module.params.get("link_level_policy") |
| 281 |
link_flap_policy = module.params.get("link_flap_policy") |
| 282 |
l3_interface_policy = module.params.get("l3_interface_policy") |
| 283 |
macsec_policy = module.params.get("macsec_policy") |
| 284 |
monitoring_policy = module.params.get("monitoring_policy") |
| 285 |
transceiver_policy_tdn = module.params.get("transceiver_policy_tdn") |
| 286 |
|
| 287 |
if policy_group_type == "leaf": |
| 288 |
policy_group_class_name = "fabricLePortPGrp" |
| 289 |
policy_group_class_rn = "leportgrp-{0}".format(name) |
| 290 |
else: |
| 291 |
policy_group_class_name = "fabricSpPortPGrp" |
| 292 |
policy_group_class_rn = "spportgrp-{0}".format(name) |
| 293 |
|
| 294 |
child_classes = [ |
| 295 |
"fabricRsDwdmFabIfPol", |
| 296 |
"fabricRsFIfPol", |
| 297 |
"fabricRsFLinkFlapPol", |
| 298 |
"fabricRsL3IfPol", |
| 299 |
"fabricRsMacsecFabIfPol", |
| 300 |
"fabricRsMonIfFabricPol", |
| 301 |
] |
| 302 |
|
| 303 |
if transceiver_policy_tdn is not None: |
| 304 |
child_classes.append("fabricRsOpticsFabIfPol") |
| 305 |
|
| 306 |
aci.construct_url( |
| 307 |
root_class=dict( |
| 308 |
aci_class="fabric", |
| 309 |
aci_rn="fabric", |
| 310 |
), |
| 311 |
subclass_1=dict( |
| 312 |
aci_class="fabricFuncP", |
| 313 |
aci_rn="funcprof", |
| 314 |
), |
| 315 |
subclass_2=dict( |
| 316 |
aci_class=policy_group_class_name, |
| 317 |
aci_rn=policy_group_class_rn, |
| 318 |
module_object=name, |
| 319 |
target_filter={"name": name}, |
| 320 |
), |
| 321 |
child_classes=child_classes, |
| 322 |
) |
| 323 |
|
| 324 |
aci.get_existing() |
| 325 |
|
| 326 |
if state == "present": |
| 327 |
child_configs = [] |
| 328 |
if dwdm_policy is not None: |
| 329 |
child_configs.append(dict(fabricRsDwdmFabIfPol=dict(attributes=dict(tnDwdmFabIfPolName=dwdm_policy)))) |
| 330 |
if link_level_policy is not None: |
| 331 |
child_configs.append(dict(fabricRsFIfPol=dict(attributes=dict(tnFabricFIfPolName=link_level_policy)))) |
| 332 |
if link_flap_policy is not None: |
| 333 |
child_configs.append(dict(fabricRsFLinkFlapPol=dict(attributes=dict(tnFabricFLinkFlapPolName=link_flap_policy)))) |
| 334 |
if l3_interface_policy is not None: |
| 335 |
child_configs.append(dict(fabricRsL3IfPol=dict(attributes=dict(tnL3IfPolName=l3_interface_policy)))) |
| 336 |
if macsec_policy is not None: |
| 337 |
child_configs.append(dict(fabricRsMacsecFabIfPol=dict(attributes=dict(tnMacsecFabIfPolName=macsec_policy)))) |
| 338 |
if monitoring_policy is not None: |
| 339 |
child_configs.append(dict(fabricRsMonIfFabricPol=dict(attributes=dict(tnMonFabricPolName=monitoring_policy)))) |
| 340 |
if transceiver_policy_tdn is not None: |
| 341 |
child_configs.append(dict(fabricRsOpticsFabIfPol=dict(attributes=dict(tDn=transceiver_policy_tdn)))) |
| 342 |
|
| 343 |
aci.payload( |
| 344 |
aci_class=policy_group_class_name, |
| 345 |
class_config=dict( |
| 346 |
name=name, |
| 347 |
descr=description, |
| 348 |
nameAlias=name_alias, |
| 349 |
), |
| 350 |
child_configs=child_configs, |
| 351 |
) |
| 352 |
|
| 353 |
aci.get_diff(aci_class=policy_group_class_name) |
| 354 |
|
| 355 |
aci.post_config() |
| 356 |
|
| 357 |
if state == "absent": |
| 358 |
aci.delete_config() |
| 359 |
|
| 360 |
aci.exit_json() |
| 361 |
|
| 362 |
|
| 363 |
if __name__ == "__main__": |
| 364 |
main() |