| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Gaspard Micol (@gmicol) <[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": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_netflow_exporter_policy |
| 16 |
short_description: Manage Netflow Exporter Policy (netflow:ExporterPol) |
| 17 |
description: |
| 18 |
- Manage Netflow Exporter Policies for tenants on Cisco ACI fabrics. |
| 19 |
- Exporter information for bootstrapping the netflow Collection agent. |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- The name of an existing tenant. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
netflow_exporter_policy: |
| 27 |
description: |
| 28 |
- The name of the Netflow Exporter Policy. |
| 29 |
type: str |
| 30 |
aliases: [ netflow_exporter, netflow_exporter_name, name ] |
| 31 |
dscp: |
| 32 |
description: |
| 33 |
- The IP DSCP value. |
| 34 |
- The APIC defaults to C(CS2) when unset during creation. |
| 35 |
It defaults to C(VA) for APIC versions 4.2 or prior. |
| 36 |
type: str |
| 37 |
choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ] |
| 38 |
destination_address: |
| 39 |
description: |
| 40 |
- The remote node destination IP address. |
| 41 |
type: str |
| 42 |
destination_port: |
| 43 |
description: |
| 44 |
- The remote node destination port. |
| 45 |
- Accepted values are any valid TCP/UDP port range. |
| 46 |
- The APIC defaults to C(unspecified) when unset during creation. |
| 47 |
type: str |
| 48 |
source_ip_type: |
| 49 |
description: |
| 50 |
- The type of Exporter source IP Address. |
| 51 |
- It can be one of the available management IP Address for a given leaf or a custom IP Address. |
| 52 |
type: str |
| 53 |
choices: [ custom_source_ip, inband_management_ip, out_of_band_management_ip, ptep ] |
| 54 |
custom_source_address: |
| 55 |
description: |
| 56 |
- The custom source IP address. |
| 57 |
- It can only be used if O(source_ip_type=custom_source_ip). |
| 58 |
type: str |
| 59 |
associated_epg: |
| 60 |
description: |
| 61 |
- The associated EPG. |
| 62 |
- To remove the current associated EPG, pass an empty dictionary. |
| 63 |
type: dict |
| 64 |
aliases: [ epg ] |
| 65 |
suboptions: |
| 66 |
tenant: |
| 67 |
description: |
| 68 |
- The name of the tenant to which the associated AP/EPG belong. |
| 69 |
type: str |
| 70 |
ap: |
| 71 |
description: |
| 72 |
- The name of the associated Application Profile to which the associated EPG belongs. |
| 73 |
type: str |
| 74 |
epg: |
| 75 |
description: |
| 76 |
- The name of the associated EPG. |
| 77 |
type: str |
| 78 |
associated_extepg: |
| 79 |
description: |
| 80 |
- The associated external EPG. |
| 81 |
- To remove the current associated external EPG, pass an empty dictionary. |
| 82 |
type: dict |
| 83 |
aliases: [ external_epg, associated_external_epg ] |
| 84 |
suboptions: |
| 85 |
tenant: |
| 86 |
description: |
| 87 |
- The name of the tenant to which the associated L3Out/external EPG belong. |
| 88 |
type: str |
| 89 |
l3out: |
| 90 |
description: |
| 91 |
- The name of the L3Out to which the associated external EPG belongs. |
| 92 |
type: str |
| 93 |
extepg: |
| 94 |
description: |
| 95 |
- The name of the associated EPG. |
| 96 |
type: str |
| 97 |
associated_vrf: |
| 98 |
description: |
| 99 |
- The associated VRF. |
| 100 |
- To remove the current associated VRF, pass an empty dictionary. |
| 101 |
type: dict |
| 102 |
aliases: [ vrf, context, associated_context ] |
| 103 |
suboptions: |
| 104 |
tenant: |
| 105 |
description: |
| 106 |
- The name of the tenant to which the associated VRF belongs. |
| 107 |
type: str |
| 108 |
vrf: |
| 109 |
description: |
| 110 |
- The name of the associated VRF. |
| 111 |
type: str |
| 112 |
description: |
| 113 |
description: |
| 114 |
- The description for the Netflow Exporter Policy. |
| 115 |
type: str |
| 116 |
aliases: [ descr ] |
| 117 |
state: |
| 118 |
description: |
| 119 |
- Use C(present) or C(absent) for adding or removing. |
| 120 |
- Use C(query) for listing an object or multiple objects. |
| 121 |
type: str |
| 122 |
choices: [ absent, present, query ] |
| 123 |
default: present |
| 124 |
extends_documentation_fragment: |
| 125 |
- cisco.aci.aci |
| 126 |
- cisco.aci.annotation |
| 127 |
- cisco.aci.owner |
| 128 |
|
| 129 |
notes: |
| 130 |
- The I(tenant) must exist before using this module in your playbook. |
| 131 |
The M(cisco.aci.aci_tenant) can be used for this. |
| 132 |
- The I(associated_epg) and I(associated_extepg) are mutually exclusive. |
| 133 |
- If the I(associated_epg) is used, the I(epg), the(tenant) and |
| 134 |
the I(ap) must exist before using this module in your play book. |
| 135 |
The M(cisco.aci.aci_epg) and the M(cisco.aci.aci_ap) can be used for this. |
| 136 |
- If the I(associated_extepg) is used, the I(extepg), the(tenant) and |
| 137 |
the I(l3out) must exist before using this module in your play book. |
| 138 |
The M(cisco.aci.aci_l3out_extepg) and the M(cisco.aci.aci_l3out) can be used for this. |
| 139 |
- If the I(associated_vrf) is used, the I(vrf) and the I(tenant) must exist |
| 140 |
before using this module in your play book. |
| 141 |
The M(cisco.aci.aci_vrf) can be used for this. |
| 142 |
seealso: |
| 143 |
- module: cisco.aci.aci_tenant |
| 144 |
- module: cisco.aci.aci_vrf |
| 145 |
- module: cisco.aci.aci_ap |
| 146 |
- module: cisco.aci.aci_epg |
| 147 |
- module: cisco.aci.aci_l3out |
| 148 |
- module: cisco.aci.aci_l3out_extepg |
| 149 |
- name: APIC Management Information Model reference |
| 150 |
description: More information about the internal APIC class B(netflow:ExporterPol). |
| 151 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 152 |
author: |
| 153 |
- Gaspard Micol (@gmicol) |
| 154 |
""" |
| 155 |
|
| 156 |
EXAMPLES = r""" |
| 157 |
- name: Add a new Netflow Exporter Policy |
| 158 |
cisco.aci.aci_netflow_exporter_policy: |
| 159 |
host: apic |
| 160 |
username: admin |
| 161 |
password: SomeSecretPassword |
| 162 |
tenant: my_tenant |
| 163 |
netflow_exporter_policy: my_netflow_exporter_policy |
| 164 |
dscp: CS2 |
| 165 |
destination_address: 11.11.11.1 |
| 166 |
destination_port: 25 |
| 167 |
source_ip_type: custom_source_ip |
| 168 |
custom_source_address: 11.11.11.2 |
| 169 |
associated_epg: |
| 170 |
tenant: my_tenant |
| 171 |
ap: my_ap |
| 172 |
epg: my_epg |
| 173 |
associated_vrf: |
| 174 |
tenant: my_tenant |
| 175 |
vrf: my_vrf |
| 176 |
state: present |
| 177 |
delegate_to: localhost |
| 178 |
|
| 179 |
- name: Remove associated EPG and VRF from the new Netflow Exporter Policy |
| 180 |
cisco.aci.aci_netflow_exporter_policy: |
| 181 |
host: apic |
| 182 |
username: admin |
| 183 |
password: SomeSecretPassword |
| 184 |
tenant: my_tenant |
| 185 |
netflow_exporter_policy: my_netflow_exporter_policy |
| 186 |
associated_epg: {} |
| 187 |
associated_vrf: {} |
| 188 |
delegate_to: localhost |
| 189 |
|
| 190 |
- name: Query a Netflow Exporter Policy |
| 191 |
cisco.aci.aci_netflow_exporter_policy: |
| 192 |
host: apic |
| 193 |
username: admin |
| 194 |
password: SomeSecretPassword |
| 195 |
tenant: my_tenant |
| 196 |
netflow_exporter_policy: my_netflow_exporter_policy |
| 197 |
state: query |
| 198 |
delegate_to: localhost |
| 199 |
|
| 200 |
- name: Query all Netflow Exporter Policies in my_tenant |
| 201 |
cisco.aci.aci_netflow_exporter_policy: |
| 202 |
host: apic |
| 203 |
username: admin |
| 204 |
password: SomeSecretPassword |
| 205 |
tenant: my_tenant |
| 206 |
state: query |
| 207 |
delegate_to: localhost |
| 208 |
|
| 209 |
- name: Query all Netflow Exporter Policies |
| 210 |
cisco.aci.aci_netflow_exporter_policy: |
| 211 |
host: apic |
| 212 |
username: admin |
| 213 |
password: SomeSecretPassword |
| 214 |
state: query |
| 215 |
delegate_to: localhost |
| 216 |
|
| 217 |
- name: Delete a Netflow Exporter Policy |
| 218 |
cisco.aci.aci_netflow_exporter_policy: |
| 219 |
host: apic |
| 220 |
username: admin |
| 221 |
password: SomeSecretPassword |
| 222 |
tenant: my_tenant |
| 223 |
netflow_exporter_policy: my_netflow_exporter_policy |
| 224 |
state: absent |
| 225 |
delegate_to: localhost |
| 226 |
""" |
| 227 |
|
| 228 |
RETURN = r""" |
| 229 |
current: |
| 230 |
description: The existing configuration from the APIC after the module has finished |
| 231 |
returned: success |
| 232 |
type: list |
| 233 |
sample: |
| 234 |
[ |
| 235 |
{ |
| 236 |
"fvTenant": { |
| 237 |
"attributes": { |
| 238 |
"descr": "Production environment", |
| 239 |
"dn": "uni/tn-production", |
| 240 |
"name": "production", |
| 241 |
"nameAlias": "", |
| 242 |
"ownerKey": "", |
| 243 |
"ownerTag": "" |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
] |
| 248 |
error: |
| 249 |
description: The error information as returned from the APIC |
| 250 |
returned: failure |
| 251 |
type: dict |
| 252 |
sample: |
| 253 |
{ |
| 254 |
"code": "122", |
| 255 |
"text": "unknown managed object class foo" |
| 256 |
} |
| 257 |
raw: |
| 258 |
description: The raw output returned by the APIC REST API (xml or json) |
| 259 |
returned: parse error |
| 260 |
type: str |
| 261 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 262 |
sent: |
| 263 |
description: The actual/minimal configuration pushed to the APIC |
| 264 |
returned: info |
| 265 |
type: list |
| 266 |
sample: |
| 267 |
{ |
| 268 |
"fvTenant": { |
| 269 |
"attributes": { |
| 270 |
"descr": "Production environment" |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
previous: |
| 275 |
description: The original configuration from the APIC before the module has started |
| 276 |
returned: info |
| 277 |
type: list |
| 278 |
sample: |
| 279 |
[ |
| 280 |
{ |
| 281 |
"fvTenant": { |
| 282 |
"attributes": { |
| 283 |
"descr": "Production", |
| 284 |
"dn": "uni/tn-production", |
| 285 |
"name": "production", |
| 286 |
"nameAlias": "", |
| 287 |
"ownerKey": "", |
| 288 |
"ownerTag": "" |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
] |
| 293 |
proposed: |
| 294 |
description: The assembled configuration from the user-provided parameters |
| 295 |
returned: info |
| 296 |
type: dict |
| 297 |
sample: |
| 298 |
{ |
| 299 |
"fvTenant": { |
| 300 |
"attributes": { |
| 301 |
"descr": "Production environment", |
| 302 |
"name": "production" |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
filter_string: |
| 307 |
description: The filter string used for the request |
| 308 |
returned: failure or debug |
| 309 |
type: str |
| 310 |
sample: ?rsp-prop-include=config-only |
| 311 |
method: |
| 312 |
description: The HTTP method used for the request to the APIC |
| 313 |
returned: failure or debug |
| 314 |
type: str |
| 315 |
sample: POST |
| 316 |
response: |
| 317 |
description: The HTTP response from the APIC |
| 318 |
returned: failure or debug |
| 319 |
type: str |
| 320 |
sample: OK (30 bytes) |
| 321 |
status: |
| 322 |
description: The HTTP status from the APIC |
| 323 |
returned: failure or debug |
| 324 |
type: int |
| 325 |
sample: 200 |
| 326 |
url: |
| 327 |
description: The HTTP url used for the request to the APIC |
| 328 |
returned: failure or debug |
| 329 |
type: str |
| 330 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 331 |
""" |
| 332 |
|
| 333 |
|
| 334 |
from ansible.module_utils.basic import AnsibleModule |
| 335 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ( |
| 336 |
ACIModule, |
| 337 |
aci_argument_spec, |
| 338 |
aci_annotation_spec, |
| 339 |
aci_owner_spec, |
| 340 |
aci_contract_dscp_spec, |
| 341 |
associated_netflow_exporter_epg_spec, |
| 342 |
associated_netflow_exporter_extepg_spec, |
| 343 |
associated_netflow_exporter_vrf_spec, |
| 344 |
) |
| 345 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import MATCH_SOURCE_IP_TYPE_NETFLOW_EXPORTER_MAPPING |
| 346 |
|
| 347 |
|
| 348 |
def main(): |
| 349 |
argument_spec = aci_argument_spec() |
| 350 |
argument_spec.update(aci_annotation_spec()) |
| 351 |
argument_spec.update(aci_owner_spec()) |
| 352 |
argument_spec.update( |
| 353 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 354 |
netflow_exporter_policy=dict(type="str", aliases=["netflow_exporter", "netflow_exporter_name", "name"]), |
| 355 |
dscp=dict((k, aci_contract_dscp_spec()[k]) for k in aci_contract_dscp_spec() if k != "aliases"), |
| 356 |
destination_address=dict(type="str"), |
| 357 |
destination_port=dict(type="str"), |
| 358 |
source_ip_type=dict(type="str", choices=list(MATCH_SOURCE_IP_TYPE_NETFLOW_EXPORTER_MAPPING.keys())), |
| 359 |
custom_source_address=dict(type="str"), |
| 360 |
associated_epg=dict(type="dict", aliases=["epg"], options=associated_netflow_exporter_epg_spec()), |
| 361 |
associated_extepg=dict(type="dict", aliases=["external_epg", "associated_external_epg"], options=associated_netflow_exporter_extepg_spec()), |
| 362 |
associated_vrf=dict(type="dict", aliases=["vrf", "associated_context", "context"], options=associated_netflow_exporter_vrf_spec()), |
| 363 |
description=dict(type="str", aliases=["descr"]), |
| 364 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 365 |
) |
| 366 |
|
| 367 |
module = AnsibleModule( |
| 368 |
argument_spec=argument_spec, |
| 369 |
supports_check_mode=True, |
| 370 |
required_if=[ |
| 371 |
["state", "absent", ["tenant", "netflow_exporter_policy"]], |
| 372 |
["state", "present", ["tenant", "netflow_exporter_policy", "destination_address", "destination_port"]], |
| 373 |
], |
| 374 |
mutually_exclusive=[["associated_epg", "associated_extepg"]], |
| 375 |
) |
| 376 |
|
| 377 |
tenant = module.params.get("tenant") |
| 378 |
description = module.params.get("description") |
| 379 |
netflow_exporter_policy = module.params.get("netflow_exporter_policy") |
| 380 |
dscp = module.params.get("dscp") |
| 381 |
destination_address = module.params.get("destination_address") |
| 382 |
destination_port = module.params.get("destination_port") |
| 383 |
source_ip_type = MATCH_SOURCE_IP_TYPE_NETFLOW_EXPORTER_MAPPING.get(module.params.get("source_ip_type")) |
| 384 |
custom_source_address = module.params.get("custom_source_address") |
| 385 |
associated_epg = module.params.get("associated_epg") |
| 386 |
associated_extepg = module.params.get("associated_extepg") |
| 387 |
associated_vrf = module.params.get("associated_vrf") |
| 388 |
state = module.params.get("state") |
| 389 |
|
| 390 |
aci = ACIModule(module) |
| 391 |
|
| 392 |
child_classes = ["netflowRsExporterToCtx", "netflowRsExporterToEPg"] |
| 393 |
|
| 394 |
aci.construct_url( |
| 395 |
root_class=dict( |
| 396 |
aci_class="fvTenant", |
| 397 |
aci_rn="tn-{0}".format(tenant), |
| 398 |
module_object=tenant, |
| 399 |
target_filter={"name": tenant}, |
| 400 |
), |
| 401 |
subclass_1=dict( |
| 402 |
aci_class="netflowExporterPol", |
| 403 |
aci_rn="exporterpol-{0}".format(netflow_exporter_policy), |
| 404 |
module_object=netflow_exporter_policy, |
| 405 |
target_filter={"name": netflow_exporter_policy}, |
| 406 |
), |
| 407 |
child_classes=child_classes, |
| 408 |
) |
| 409 |
|
| 410 |
aci.get_existing() |
| 411 |
|
| 412 |
if state == "present": |
| 413 |
child_configs = [] |
| 414 |
if associated_vrf is not None: |
| 415 |
if all(value is None for value in associated_vrf.values()) and isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 416 |
for child in aci.existing[0].get("netflowExporterPol", {}).get("children", {}): |
| 417 |
if child.get("netflowRsExporterToCtx"): |
| 418 |
child_configs.extend([dict(netflowRsExporterToCtx=dict(attributes=dict(status="deleted")))]) |
| 419 |
elif all(value is not None for value in associated_vrf.values()): |
| 420 |
child_configs.extend( |
| 421 |
[ |
| 422 |
dict( |
| 423 |
netflowRsExporterToCtx=dict( |
| 424 |
attributes=dict(tDn="uni/tn-{0}/ctx-{1}".format(associated_vrf.get("tenant"), associated_vrf.get("vrf"))) |
| 425 |
) |
| 426 |
), |
| 427 |
] |
| 428 |
) |
| 429 |
if associated_epg is not None: |
| 430 |
if all(value is None for value in associated_epg.values()) and isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 431 |
for child in aci.existing[0].get("netflowExporterPol", {}).get("children", {}): |
| 432 |
if child.get("netflowRsExporterToEPg"): |
| 433 |
child_configs.extend([dict(netflowRsExporterToEPg=dict(attributes=dict(status="deleted")))]) |
| 434 |
elif all(value is not None for value in associated_epg.values()): |
| 435 |
child_configs.extend( |
| 436 |
[ |
| 437 |
dict( |
| 438 |
netflowRsExporterToEPg=dict( |
| 439 |
attributes=dict( |
| 440 |
tDn="uni/tn-{0}/ap-{1}/epg-{2}".format(associated_epg.get("tenant"), associated_epg.get("ap"), associated_epg.get("epg")) |
| 441 |
) |
| 442 |
) |
| 443 |
), |
| 444 |
] |
| 445 |
) |
| 446 |
elif associated_extepg is not None: |
| 447 |
if all(value is None for value in associated_extepg.values()) and isinstance(aci.existing, list) and len(aci.existing) > 0: |
| 448 |
for child in aci.existing[0].get("netflowExporterPol", {}).get("children", {}): |
| 449 |
if child.get("netflowRsExporterToEPg"): |
| 450 |
child_configs.extend([dict(netflowRsExporterToEPg=dict(attributes=dict(status="deleted")))]) |
| 451 |
elif all(value is not None for value in associated_extepg.values()): |
| 452 |
child_configs.extend( |
| 453 |
[ |
| 454 |
dict( |
| 455 |
netflowRsExporterToEPg=dict( |
| 456 |
attributes=dict( |
| 457 |
tDn="uni/tn-{0}/out-{1}/instP-{2}".format( |
| 458 |
associated_extepg.get("tenant"), associated_extepg.get("l3out"), associated_extepg.get("extepg") |
| 459 |
) |
| 460 |
) |
| 461 |
) |
| 462 |
), |
| 463 |
] |
| 464 |
) |
| 465 |
aci.payload( |
| 466 |
aci_class="netflowExporterPol", |
| 467 |
class_config=dict( |
| 468 |
name=netflow_exporter_policy, |
| 469 |
descr=description, |
| 470 |
dscp=dscp, |
| 471 |
dstAddr=destination_address, |
| 472 |
dstPort=destination_port, |
| 473 |
sourceIpType=source_ip_type, |
| 474 |
srcAddr=custom_source_address, |
| 475 |
), |
| 476 |
child_configs=child_configs, |
| 477 |
) |
| 478 |
|
| 479 |
aci.get_diff(aci_class="netflowExporterPol") |
| 480 |
|
| 481 |
aci.post_config() |
| 482 |
|
| 483 |
elif state == "absent": |
| 484 |
aci.delete_config() |
| 485 |
|
| 486 |
aci.exit_json() |
| 487 |
|
| 488 |
|
| 489 |
if __name__ == "__main__": |
| 490 |
main() |