| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Tim Cragg (@timcragg) <[email protected]> |
| 5 |
# Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_system_endpoint_controls |
| 17 |
short_description: Manage System Endpoint Controls (ep:IpAgingP, ep:ControlP, and ep:LoopProtectP) |
| 18 |
description: |
| 19 |
- Manage System Endpoint Controls on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
ip_aging: |
| 22 |
description: Configuration container for IP Aging. |
| 23 |
type: dict |
| 24 |
suboptions: |
| 25 |
admin_state: |
| 26 |
description: |
| 27 |
- Whether to enable IP Aging Controls on the fabric. |
| 28 |
type: bool |
| 29 |
roque_ep_control: |
| 30 |
description: Configuration container for Rogue EP Control. |
| 31 |
type: dict |
| 32 |
suboptions: |
| 33 |
admin_state: |
| 34 |
description: |
| 35 |
- Whether to enable Rogue EP Control on the fabric. |
| 36 |
type: bool |
| 37 |
interval: |
| 38 |
description: |
| 39 |
- The rogue endpoint detection interval in seconds. |
| 40 |
type: int |
| 41 |
multiplication_factor: |
| 42 |
description: |
| 43 |
- The rogue endpoint detection multiplication factor. |
| 44 |
type: int |
| 45 |
hold_interval: |
| 46 |
description: |
| 47 |
- The rogue endpoint hold interval in seconds. |
| 48 |
type: int |
| 49 |
ep_loop_protection: |
| 50 |
description: Configuration container for EP Loop Protection. |
| 51 |
type: dict |
| 52 |
suboptions: |
| 53 |
admin_state: |
| 54 |
description: |
| 55 |
- Whether to enable EP Loop Protection on the fabric. |
| 56 |
type: bool |
| 57 |
interval: |
| 58 |
description: |
| 59 |
- The loop protection detection interval in seconds. |
| 60 |
type: int |
| 61 |
multiplication_factor: |
| 62 |
description: |
| 63 |
- The loop protection detection multiplication factor. |
| 64 |
type: int |
| 65 |
action: |
| 66 |
description: |
| 67 |
- The action(s) to take when a loop is detected. |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
choices: [ bd, port ] |
| 71 |
state: |
| 72 |
description: |
| 73 |
- Use C(present) for updating configuration. |
| 74 |
- Use C(query) for showing current configuration. |
| 75 |
type: str |
| 76 |
choices: [ present, query ] |
| 77 |
default: present |
| 78 |
extends_documentation_fragment: |
| 79 |
- cisco.aci.aci |
| 80 |
- cisco.aci.annotation |
| 81 |
- cisco.aci.owner |
| 82 |
|
| 83 |
seealso: |
| 84 |
- name: APIC Management Information Model reference |
| 85 |
description: More information about the internal APIC class B(ep:IpAgingP), B(ep:ControlP), and B(ep:LoopProtectP). |
| 86 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 87 |
author: |
| 88 |
- Tim Cragg (@timcragg) |
| 89 |
- Akini Ross (@akinross) |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Set system endpoint controls settings |
| 94 |
cisco.aci.aci_system_endpoint_controls: |
| 95 |
host: apic |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
admin_state: true |
| 99 |
ip_aging: |
| 100 |
admin_state: true |
| 101 |
roque_ep_control: |
| 102 |
admin_state: true |
| 103 |
interval: 50 |
| 104 |
multiplication_factor: 10 |
| 105 |
hold_interval: 2000 |
| 106 |
ep_loop_protection: |
| 107 |
admin_state: true |
| 108 |
interval: 70 |
| 109 |
multiplication_factor: 15 |
| 110 |
action: [bd, port] |
| 111 |
delegate_to: localhost |
| 112 |
|
| 113 |
- name: Query system endpoint controls settings |
| 114 |
cisco.aci.aci_system_endpoint_controls: |
| 115 |
host: apic |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
state: query |
| 119 |
delegate_to: localhost |
| 120 |
register: query_result |
| 121 |
""" |
| 122 |
|
| 123 |
RETURN = r""" |
| 124 |
current: |
| 125 |
description: The existing configuration from the APIC after the module has finished |
| 126 |
returned: success |
| 127 |
type: list |
| 128 |
sample: |
| 129 |
[ |
| 130 |
{ |
| 131 |
"fvTenant": { |
| 132 |
"attributes": { |
| 133 |
"descr": "Production environment", |
| 134 |
"dn": "uni/tn-production", |
| 135 |
"name": "production", |
| 136 |
"nameAlias": "", |
| 137 |
"ownerKey": "", |
| 138 |
"ownerTag": "" |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
] |
| 143 |
error: |
| 144 |
description: The error information as returned from the APIC |
| 145 |
returned: failure |
| 146 |
type: dict |
| 147 |
sample: |
| 148 |
{ |
| 149 |
"code": "122", |
| 150 |
"text": "unknown managed object class foo" |
| 151 |
} |
| 152 |
raw: |
| 153 |
description: The raw output returned by the APIC REST API (xml or json) |
| 154 |
returned: parse error |
| 155 |
type: str |
| 156 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 157 |
sent: |
| 158 |
description: The actual/minimal configuration pushed to the APIC |
| 159 |
returned: info |
| 160 |
type: list |
| 161 |
sample: |
| 162 |
{ |
| 163 |
"fvTenant": { |
| 164 |
"attributes": { |
| 165 |
"descr": "Production environment" |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
previous: |
| 170 |
description: The original configuration from the APIC before the module has started |
| 171 |
returned: info |
| 172 |
type: list |
| 173 |
sample: |
| 174 |
[ |
| 175 |
{ |
| 176 |
"fvTenant": { |
| 177 |
"attributes": { |
| 178 |
"descr": "Production", |
| 179 |
"dn": "uni/tn-production", |
| 180 |
"name": "production", |
| 181 |
"nameAlias": "", |
| 182 |
"ownerKey": "", |
| 183 |
"ownerTag": "" |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
] |
| 188 |
proposed: |
| 189 |
description: The assembled configuration from the user-provided parameters |
| 190 |
returned: info |
| 191 |
type: dict |
| 192 |
sample: |
| 193 |
{ |
| 194 |
"fvTenant": { |
| 195 |
"attributes": { |
| 196 |
"descr": "Production environment", |
| 197 |
"name": "production" |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
filter_string: |
| 202 |
description: The filter string used for the request |
| 203 |
returned: failure or debug |
| 204 |
type: str |
| 205 |
sample: ?rsp-prop-include=config-only |
| 206 |
method: |
| 207 |
description: The HTTP method used for the request to the APIC |
| 208 |
returned: failure or debug |
| 209 |
type: str |
| 210 |
sample: POST |
| 211 |
response: |
| 212 |
description: The HTTP response from the APIC |
| 213 |
returned: failure or debug |
| 214 |
type: str |
| 215 |
sample: OK (30 bytes) |
| 216 |
status: |
| 217 |
description: The HTTP status from the APIC |
| 218 |
returned: failure or debug |
| 219 |
type: int |
| 220 |
sample: 200 |
| 221 |
url: |
| 222 |
description: The HTTP url used for the request to the APIC |
| 223 |
returned: failure or debug |
| 224 |
type: str |
| 225 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 226 |
""" |
| 227 |
|
| 228 |
from ansible.module_utils.basic import AnsibleModule |
| 229 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 230 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import EP_LOOP_PROTECTION_ACTION_MAPPING |
| 231 |
|
| 232 |
|
| 233 |
def main(): |
| 234 |
argument_spec = aci_argument_spec() |
| 235 |
argument_spec.update(aci_annotation_spec()) |
| 236 |
argument_spec.update(aci_owner_spec()) |
| 237 |
argument_spec.update( |
| 238 |
ip_aging=dict(type="dict", options=dict(admin_state=dict(type="bool"))), |
| 239 |
roque_ep_control=dict( |
| 240 |
type="dict", |
| 241 |
options=dict( |
| 242 |
admin_state=dict(type="bool"), |
| 243 |
interval=dict(type="int"), |
| 244 |
multiplication_factor=dict(type="int"), |
| 245 |
hold_interval=dict(type="int"), |
| 246 |
), |
| 247 |
), |
| 248 |
ep_loop_protection=dict( |
| 249 |
type="dict", |
| 250 |
options=dict( |
| 251 |
admin_state=dict(type="bool"), |
| 252 |
interval=dict(type="int"), |
| 253 |
multiplication_factor=dict(type="int"), |
| 254 |
action=dict(type="list", elements="str", choices=["bd", "port"]), |
| 255 |
), |
| 256 |
), |
| 257 |
state=dict(type="str", default="present", choices=["present", "query"]), |
| 258 |
) |
| 259 |
|
| 260 |
module = AnsibleModule( |
| 261 |
argument_spec=argument_spec, |
| 262 |
supports_check_mode=True, |
| 263 |
required_if=[["state", "present", ["ip_aging", "roque_ep_control", "ep_loop_protection"], True]], |
| 264 |
) |
| 265 |
|
| 266 |
aci = ACIModule(module) |
| 267 |
state = module.params.get("state") |
| 268 |
|
| 269 |
aci.construct_url( |
| 270 |
root_class=dict( |
| 271 |
aci_class="infraInfra", |
| 272 |
aci_rn="infra", |
| 273 |
), |
| 274 |
child_classes=["epIpAgingP", "epControlP", "epLoopProtectP"], |
| 275 |
) |
| 276 |
|
| 277 |
aci.get_existing() |
| 278 |
|
| 279 |
if state == "present": |
| 280 |
child_configs = [] |
| 281 |
ip_aging = module.params.get("ip_aging") |
| 282 |
roque_ep_control = module.params.get("roque_ep_control") |
| 283 |
ep_loop_protection = module.params.get("ep_loop_protection") |
| 284 |
|
| 285 |
if ip_aging: |
| 286 |
child_configs.append( |
| 287 |
{"epIpAgingP": {"attributes": {"name": "default", "adminSt": aci.boolean(ip_aging.get("admin_state"), "enabled", "disabled")}}} |
| 288 |
) |
| 289 |
|
| 290 |
if roque_ep_control: |
| 291 |
child_configs.append( |
| 292 |
{ |
| 293 |
"epControlP": { |
| 294 |
"attributes": { |
| 295 |
"name": "default", |
| 296 |
"adminSt": aci.boolean(roque_ep_control.get("admin_state"), "enabled", "disabled"), |
| 297 |
"rogueEpDetectIntvl": roque_ep_control.get("interval"), |
| 298 |
"rogueEpDetectMult": roque_ep_control.get("multiplication_factor"), |
| 299 |
"holdIntvl": roque_ep_control.get("hold_interval"), |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
) |
| 304 |
|
| 305 |
if ep_loop_protection: |
| 306 |
actions = None |
| 307 |
if ep_loop_protection.get("action"): |
| 308 |
actions = ",".join(sorted([EP_LOOP_PROTECTION_ACTION_MAPPING.get(action) for action in ep_loop_protection.get("action")])) |
| 309 |
child_configs.append( |
| 310 |
{ |
| 311 |
"epLoopProtectP": { |
| 312 |
"attributes": { |
| 313 |
"name": "default", |
| 314 |
"adminSt": aci.boolean(ep_loop_protection.get("admin_state"), "enabled", "disabled"), |
| 315 |
"loopDetectIntvl": ep_loop_protection.get("interval"), |
| 316 |
"loopDetectMult": ep_loop_protection.get("multiplication_factor"), |
| 317 |
"action": actions, |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
) |
| 322 |
|
| 323 |
aci.payload( |
| 324 |
aci_class="infraInfra", |
| 325 |
class_config=dict(), |
| 326 |
child_configs=child_configs, |
| 327 |
) |
| 328 |
|
| 329 |
aci.get_diff(aci_class="infraInfra") |
| 330 |
|
| 331 |
aci.post_config() |
| 332 |
|
| 333 |
aci.exit_json() |
| 334 |
|
| 335 |
|
| 336 |
if __name__ == "__main__": |
| 337 |
main() |