| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Akini Ross <[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_span_src_group |
| 16 |
short_description: Manage Fabric SPAN source groups (span:SrcGrp) |
| 17 |
description: |
| 18 |
- Manage SPAN source groups on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
source_group: |
| 21 |
description: |
| 22 |
- The name of the Fabric SPAN source group. |
| 23 |
type: str |
| 24 |
aliases: [ name, src_group ] |
| 25 |
description: |
| 26 |
description: |
| 27 |
- The description for Fabric SPAN source group. |
| 28 |
type: str |
| 29 |
aliases: [ descr ] |
| 30 |
admin_state: |
| 31 |
description: |
| 32 |
- Enable C(true) or disable C(false) the SPAN sources. |
| 33 |
- The APIC defaults to C(true) when unset during creation. |
| 34 |
type: bool |
| 35 |
destination_group: |
| 36 |
description: |
| 37 |
- The name of the Fabric SPAN destination group to associate with the source group. |
| 38 |
type: str |
| 39 |
aliases: [ dst_group ] |
| 40 |
state: |
| 41 |
description: |
| 42 |
- Use C(present) or C(absent) for adding or removing. |
| 43 |
- Use C(query) for listing an object or multiple objects. |
| 44 |
type: str |
| 45 |
choices: [ absent, present, query ] |
| 46 |
default: present |
| 47 |
name_alias: |
| 48 |
description: |
| 49 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 50 |
type: str |
| 51 |
extends_documentation_fragment: |
| 52 |
- cisco.aci.aci |
| 53 |
- cisco.aci.annotation |
| 54 |
- cisco.aci.owner |
| 55 |
|
| 56 |
notes: |
| 57 |
- The I(destination_group) must exist before using this module in your playbook. |
| 58 |
The M(cisco.aci.aci_fabric_span_dst_group) module can be used for this. |
| 59 |
seealso: |
| 60 |
- module: cisco.aci.aci_fabric_span_dst_group |
| 61 |
- name: APIC Management Information Model reference |
| 62 |
description: More information about the internal APIC class B(span:SrcGrp). |
| 63 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 64 |
author: |
| 65 |
- Akini Ross (@akinross) |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
- name: Create a Fabric SPAN source group |
| 70 |
cisco.aci.aci_fabric_span_src_group: |
| 71 |
host: apic |
| 72 |
username: admin |
| 73 |
password: SomeSecretPassword |
| 74 |
source_group: my_span_source_group |
| 75 |
destination_group: my_span_dest_group |
| 76 |
state: present |
| 77 |
delegate_to: localhost |
| 78 |
|
| 79 |
- name: Delete a Fabric SPAN source group |
| 80 |
cisco.aci.aci_fabric_span_src_group: |
| 81 |
host: apic |
| 82 |
username: admin |
| 83 |
password: SomeSecretPassword |
| 84 |
source_group: my_span_source_group |
| 85 |
state: absent |
| 86 |
delegate_to: localhost |
| 87 |
|
| 88 |
- name: Query all Fabric SPAN source groups |
| 89 |
cisco.aci.aci_fabric_span_src_group: |
| 90 |
host: apic |
| 91 |
username: admin |
| 92 |
password: SomeSecretPassword |
| 93 |
state: query |
| 94 |
delegate_to: localhost |
| 95 |
register: query_result |
| 96 |
|
| 97 |
- name: Query a specific Fabric SPAN source group |
| 98 |
cisco.aci.aci_fabric_span_src_group: |
| 99 |
host: apic |
| 100 |
username: admin |
| 101 |
password: SomeSecretPassword |
| 102 |
source_group: my_span_source_group |
| 103 |
state: query |
| 104 |
delegate_to: localhost |
| 105 |
register: query_result |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = r""" |
| 109 |
current: |
| 110 |
description: The existing configuration from the APIC after the module has finished |
| 111 |
returned: success |
| 112 |
type: list |
| 113 |
sample: |
| 114 |
[ |
| 115 |
{ |
| 116 |
"fvTenant": { |
| 117 |
"attributes": { |
| 118 |
"descr": "Production environment", |
| 119 |
"dn": "uni/tn-production", |
| 120 |
"name": "production", |
| 121 |
"nameAlias": "", |
| 122 |
"ownerKey": "", |
| 123 |
"ownerTag": "" |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
] |
| 128 |
error: |
| 129 |
description: The error information as returned from the APIC |
| 130 |
returned: failure |
| 131 |
type: dict |
| 132 |
sample: |
| 133 |
{ |
| 134 |
"code": "122", |
| 135 |
"text": "unknown managed object class foo" |
| 136 |
} |
| 137 |
raw: |
| 138 |
description: The raw output returned by the APIC REST API (xml or json) |
| 139 |
returned: parse error |
| 140 |
type: str |
| 141 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 142 |
sent: |
| 143 |
description: The actual/minimal configuration pushed to the APIC |
| 144 |
returned: info |
| 145 |
type: list |
| 146 |
sample: |
| 147 |
{ |
| 148 |
"fvTenant": { |
| 149 |
"attributes": { |
| 150 |
"descr": "Production environment" |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
previous: |
| 155 |
description: The original configuration from the APIC before the module has started |
| 156 |
returned: info |
| 157 |
type: list |
| 158 |
sample: |
| 159 |
[ |
| 160 |
{ |
| 161 |
"fvTenant": { |
| 162 |
"attributes": { |
| 163 |
"descr": "Production", |
| 164 |
"dn": "uni/tn-production", |
| 165 |
"name": "production", |
| 166 |
"nameAlias": "", |
| 167 |
"ownerKey": "", |
| 168 |
"ownerTag": "" |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
] |
| 173 |
proposed: |
| 174 |
description: The assembled configuration from the user-provided parameters |
| 175 |
returned: info |
| 176 |
type: dict |
| 177 |
sample: |
| 178 |
{ |
| 179 |
"fvTenant": { |
| 180 |
"attributes": { |
| 181 |
"descr": "Production environment", |
| 182 |
"name": "production" |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
filter_string: |
| 187 |
description: The filter string used for the request |
| 188 |
returned: failure or debug |
| 189 |
type: str |
| 190 |
sample: ?rsp-prop-include=config-only |
| 191 |
method: |
| 192 |
description: The HTTP method used for the request to the APIC |
| 193 |
returned: failure or debug |
| 194 |
type: str |
| 195 |
sample: POST |
| 196 |
response: |
| 197 |
description: The HTTP response from the APIC |
| 198 |
returned: failure or debug |
| 199 |
type: str |
| 200 |
sample: OK (30 bytes) |
| 201 |
status: |
| 202 |
description: The HTTP status from the APIC |
| 203 |
returned: failure or debug |
| 204 |
type: int |
| 205 |
sample: 200 |
| 206 |
url: |
| 207 |
description: The HTTP url used for the request to the APIC |
| 208 |
returned: failure or debug |
| 209 |
type: str |
| 210 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 211 |
""" |
| 212 |
|
| 213 |
from ansible.module_utils.basic import AnsibleModule |
| 214 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
argument_spec = aci_argument_spec() |
| 219 |
argument_spec.update(aci_annotation_spec()) |
| 220 |
argument_spec.update(aci_owner_spec()) |
| 221 |
argument_spec.update( |
| 222 |
source_group=dict(type="str", aliases=["name", "src_group"]), # Not required for querying all objects |
| 223 |
description=dict(type="str", aliases=["descr"]), |
| 224 |
admin_state=dict(type="bool"), |
| 225 |
destination_group=dict(type="str", aliases=["dst_group"]), |
| 226 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 227 |
name_alias=dict(type="str"), |
| 228 |
) |
| 229 |
|
| 230 |
module = AnsibleModule( |
| 231 |
argument_spec=argument_spec, |
| 232 |
supports_check_mode=True, |
| 233 |
required_if=[ |
| 234 |
["state", "absent", ["source_group"]], |
| 235 |
["state", "present", ["source_group", "destination_group"]], |
| 236 |
], |
| 237 |
) |
| 238 |
|
| 239 |
aci = ACIModule(module) |
| 240 |
|
| 241 |
source_group = module.params.get("source_group") |
| 242 |
description = module.params.get("description") |
| 243 |
admin_state = aci.boolean(module.params.get("admin_state"), "enabled", "disabled") |
| 244 |
destination_group = module.params.get("destination_group") |
| 245 |
state = module.params.get("state") |
| 246 |
name_alias = module.params.get("name_alias") |
| 247 |
|
| 248 |
aci.construct_url( |
| 249 |
root_class=dict( |
| 250 |
aci_class="fabric", |
| 251 |
aci_rn="fabric", |
| 252 |
), |
| 253 |
subclass_1=dict( |
| 254 |
aci_class="spanSrcGrp", |
| 255 |
aci_rn="srcgrp-{0}".format(source_group), |
| 256 |
module_object=source_group, |
| 257 |
target_filter={"name": source_group}, |
| 258 |
), |
| 259 |
child_classes=["spanSpanLbl"], |
| 260 |
) |
| 261 |
|
| 262 |
aci.get_existing() |
| 263 |
|
| 264 |
if state == "present": |
| 265 |
# Create new child configs payload |
| 266 |
child_configs = [{"spanSpanLbl": {"attributes": {"name": destination_group}}}] |
| 267 |
|
| 268 |
# Validate if existing and remove child object when is does not match provided configuration |
| 269 |
if isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 270 |
for child in aci.existing[0].get("spanSrcGrp", {}).get("children", {}): |
| 271 |
if child.get("spanSpanLbl") and child.get("spanSpanLbl").get("attributes").get("name") != destination_group: |
| 272 |
child_configs.append( |
| 273 |
{ |
| 274 |
"spanSpanLbl": { |
| 275 |
"attributes": { |
| 276 |
"dn": "uni/fabric/srcgrp-{0}/spanlbl-{1}".format(source_group, child.get("spanSpanLbl").get("attributes").get("name")), |
| 277 |
"status": "deleted", |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
) |
| 282 |
|
| 283 |
aci.payload( |
| 284 |
aci_class="spanSrcGrp", |
| 285 |
class_config=dict(adminSt=admin_state, descr=description, name=source_group, nameAlias=name_alias), |
| 286 |
child_configs=child_configs, |
| 287 |
) |
| 288 |
|
| 289 |
aci.get_diff(aci_class="spanSrcGrp") |
| 290 |
|
| 291 |
aci.post_config() |
| 292 |
|
| 293 |
elif state == "absent": |
| 294 |
aci.delete_config() |
| 295 |
|
| 296 |
aci.exit_json() |
| 297 |
|
| 298 |
|
| 299 |
if __name__ == "__main__": |
| 300 |
main() |