| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Tim Cragg (@timcragg) |
| 5 |
# Copyright: (c) 2025, Shreyas Srish (@shrsr) |
| 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": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_l4l7_device |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage L4-L7 Devices (vns:LDevVip) |
| 19 |
description: |
| 20 |
- Manage Layer 4 to Layer 7 (L4-L7) Devices. |
| 21 |
options: |
| 22 |
tenant: |
| 23 |
description: |
| 24 |
- The name of an existing tenant. |
| 25 |
type: str |
| 26 |
aliases: [ tenant_name ] |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the L4-L7 device. |
| 30 |
type: str |
| 31 |
aliases: [ device, logical_device, device_name, logical_device_name ] |
| 32 |
context_aware: |
| 33 |
description: |
| 34 |
- Is device Single or Multi context aware. |
| 35 |
- The APIC defaults to C(single) when unset during creation. |
| 36 |
type: str |
| 37 |
choices: [ multi, single ] |
| 38 |
device_type: |
| 39 |
description: |
| 40 |
- The type of the device. |
| 41 |
- The APIC defaults to C(physical) when unset during creation. |
| 42 |
type: str |
| 43 |
choices: [ physical, virtual ] |
| 44 |
aliases: [ dev_type ] |
| 45 |
function_type: |
| 46 |
description: |
| 47 |
- The function type of the device. |
| 48 |
- The APIC defaults to C(go_to) when unset during creation. |
| 49 |
type: str |
| 50 |
choices: [ go_to, go_through, l1, l2 ] |
| 51 |
aliases: [ func_type ] |
| 52 |
managed: |
| 53 |
description: |
| 54 |
- Is the device a managed device. |
| 55 |
- The APIC defaults to true when unset during creation. |
| 56 |
type: bool |
| 57 |
promiscuous_mode: |
| 58 |
description: |
| 59 |
- Enable promiscuous mode. |
| 60 |
- The APIC defaults to false when unset during creation. |
| 61 |
type: bool |
| 62 |
aliases: [ prom_mode ] |
| 63 |
service_type: |
| 64 |
description: |
| 65 |
- The service type running on the device. |
| 66 |
- The APIC defaults to C(others) when unset during creation. |
| 67 |
type: str |
| 68 |
choices: [ adc, fw, others ] |
| 69 |
aliases: [ svc_type ] |
| 70 |
trunking: |
| 71 |
description: |
| 72 |
- Enable trunking. |
| 73 |
- The APIC defaults to false when unset during creation. |
| 74 |
type: bool |
| 75 |
domain: |
| 76 |
description: |
| 77 |
- The domain to bind to the device. |
| 78 |
- The type of domain is controlled by the device_type setting. |
| 79 |
type: str |
| 80 |
active_active_mode: |
| 81 |
description: |
| 82 |
- The active active mode on the device. |
| 83 |
- This is only applicable when C(function_type="l1") or C(function_type="l2"). |
| 84 |
- The APIC defaults to false when unset during creation. |
| 85 |
type: bool |
| 86 |
state: |
| 87 |
description: |
| 88 |
- Use C(present) or C(absent) for adding or removing. |
| 89 |
- Use C(query) for listing an object or multiple objects. |
| 90 |
type: str |
| 91 |
choices: [ absent, present, query ] |
| 92 |
default: present |
| 93 |
extends_documentation_fragment: |
| 94 |
- cisco.aci.aci |
| 95 |
- cisco.aci.annotation |
| 96 |
notes: |
| 97 |
- The I(tenant) must exist before using this module in your playbook. |
| 98 |
The M(cisco.aci.aci_tenant) modules can be used for this. |
| 99 |
seealso: |
| 100 |
- module: cisco.aci.aci_tenant |
| 101 |
- name: APIC Management Information Model reference |
| 102 |
description: More information about the internal APIC class B(vns:LDevVip) |
| 103 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 104 |
author: |
| 105 |
- Tim Cragg (@timcragg) |
| 106 |
- Shreyas Srish (@shrsr) |
| 107 |
""" |
| 108 |
|
| 109 |
EXAMPLES = r""" |
| 110 |
- name: Add a new L4-L7 device |
| 111 |
cisco.aci.aci_l4l7_device: |
| 112 |
host: apic |
| 113 |
username: admin |
| 114 |
password: SomeSecretPassword |
| 115 |
tenant: my_tenant |
| 116 |
name: my_device |
| 117 |
state: present |
| 118 |
domain: phys |
| 119 |
function_type: go_to |
| 120 |
context_aware: single |
| 121 |
managed: false |
| 122 |
device_type: physical |
| 123 |
service_type: adc |
| 124 |
trunking: false |
| 125 |
promiscuous_mode: true |
| 126 |
delegate_to: localhost |
| 127 |
|
| 128 |
- name: Query an L4-L7 device |
| 129 |
cisco.aci.aci_l4l7_device: |
| 130 |
host: apic |
| 131 |
username: admin |
| 132 |
password: SomeSecretPassword |
| 133 |
tenant: my_tenant |
| 134 |
name: my_device |
| 135 |
state: query |
| 136 |
delegate_to: localhost |
| 137 |
register: query_result |
| 138 |
|
| 139 |
- name: Query all L4-L7 devices |
| 140 |
cisco.aci.aci_l4l7_device: |
| 141 |
host: apic |
| 142 |
username: admin |
| 143 |
password: SomeSecretPassword |
| 144 |
state: query |
| 145 |
delegate_to: localhost |
| 146 |
register: query_result |
| 147 |
|
| 148 |
- name: Delete an existing L4-L7 device |
| 149 |
cisco.aci.aci_l4l7_device: |
| 150 |
host: apic |
| 151 |
username: admin |
| 152 |
password: SomeSecretPassword |
| 153 |
tenant: my_tenant |
| 154 |
name: my_device |
| 155 |
state: absent |
| 156 |
delegate_to: localhost |
| 157 |
""" |
| 158 |
|
| 159 |
RETURN = r""" |
| 160 |
current: |
| 161 |
description: The existing configuration from the APIC after the module has finished |
| 162 |
returned: success |
| 163 |
type: list |
| 164 |
sample: |
| 165 |
[ |
| 166 |
{ |
| 167 |
"fvTenant": { |
| 168 |
"attributes": { |
| 169 |
"descr": "Production environment", |
| 170 |
"dn": "uni/tn-production", |
| 171 |
"name": "production", |
| 172 |
"nameAlias": "", |
| 173 |
"ownerKey": "", |
| 174 |
"ownerTag": "" |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
] |
| 179 |
error: |
| 180 |
description: The error information as returned from the APIC |
| 181 |
returned: failure |
| 182 |
type: dict |
| 183 |
sample: |
| 184 |
{ |
| 185 |
"code": "122", |
| 186 |
"text": "unknown managed object class foo" |
| 187 |
} |
| 188 |
raw: |
| 189 |
description: The raw output returned by the APIC REST API (xml or json) |
| 190 |
returned: parse error |
| 191 |
type: str |
| 192 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 193 |
sent: |
| 194 |
description: The actual/minimal configuration pushed to the APIC |
| 195 |
returned: info |
| 196 |
type: list |
| 197 |
sample: |
| 198 |
{ |
| 199 |
"fvTenant": { |
| 200 |
"attributes": { |
| 201 |
"descr": "Production environment" |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
previous: |
| 206 |
description: The original configuration from the APIC before the module has started |
| 207 |
returned: info |
| 208 |
type: list |
| 209 |
sample: |
| 210 |
[ |
| 211 |
{ |
| 212 |
"fvTenant": { |
| 213 |
"attributes": { |
| 214 |
"descr": "Production", |
| 215 |
"dn": "uni/tn-production", |
| 216 |
"name": "production", |
| 217 |
"nameAlias": "", |
| 218 |
"ownerKey": "", |
| 219 |
"ownerTag": "" |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
] |
| 224 |
proposed: |
| 225 |
description: The assembled configuration from the user-provided parameters |
| 226 |
returned: info |
| 227 |
type: dict |
| 228 |
sample: |
| 229 |
{ |
| 230 |
"fvTenant": { |
| 231 |
"attributes": { |
| 232 |
"descr": "Production environment", |
| 233 |
"name": "production" |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
filter_string: |
| 238 |
description: The filter string used for the request |
| 239 |
returned: failure or debug |
| 240 |
type: str |
| 241 |
sample: ?rsp-prop-include=config-only |
| 242 |
method: |
| 243 |
description: The HTTP method used for the request to the APIC |
| 244 |
returned: failure or debug |
| 245 |
type: str |
| 246 |
sample: POST |
| 247 |
response: |
| 248 |
description: The HTTP response from the APIC |
| 249 |
returned: failure or debug |
| 250 |
type: str |
| 251 |
sample: OK (30 bytes) |
| 252 |
status: |
| 253 |
description: The HTTP status from the APIC |
| 254 |
returned: failure or debug |
| 255 |
type: int |
| 256 |
sample: 200 |
| 257 |
url: |
| 258 |
description: The HTTP url used for the request to the APIC |
| 259 |
returned: failure or debug |
| 260 |
type: str |
| 261 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 262 |
""" |
| 263 |
|
| 264 |
|
| 265 |
from ansible.module_utils.basic import AnsibleModule |
| 266 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 267 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import L4L7_FUNC_TYPES_MAPPING |
| 268 |
|
| 269 |
|
| 270 |
def main(): |
| 271 |
argument_spec = aci_argument_spec() |
| 272 |
argument_spec.update(aci_annotation_spec()) |
| 273 |
argument_spec.update( |
| 274 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 275 |
name=dict(type="str", aliases=["device", "device_name", "logical_device", "logical_device_name"]), |
| 276 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 277 |
context_aware=dict(type="str", choices=["single", "multi"]), |
| 278 |
device_type=dict(type="str", aliases=["dev_type"], choices=["physical", "virtual"]), |
| 279 |
function_type=dict(type="str", aliases=["func_type"], choices=list(L4L7_FUNC_TYPES_MAPPING)), |
| 280 |
managed=dict(type="bool"), |
| 281 |
promiscuous_mode=dict(type="bool", aliases=["prom_mode"]), |
| 282 |
service_type=dict(type="str", aliases=["svc_type"], choices=["adc", "fw", "others"]), |
| 283 |
trunking=dict(type="bool"), |
| 284 |
domain=dict(type="str"), |
| 285 |
active_active_mode=dict(type="bool"), |
| 286 |
) |
| 287 |
|
| 288 |
module = AnsibleModule( |
| 289 |
argument_spec=argument_spec, |
| 290 |
supports_check_mode=True, |
| 291 |
required_if=[ |
| 292 |
["state", "absent", ["tenant", "name"]], |
| 293 |
["state", "present", ["tenant", "name", "device_type", "domain"]], |
| 294 |
], |
| 295 |
) |
| 296 |
|
| 297 |
aci = ACIModule(module) |
| 298 |
|
| 299 |
tenant = module.params.get("tenant") |
| 300 |
state = module.params.get("state") |
| 301 |
name = module.params.get("name") |
| 302 |
context_aware = module.params.get("context_aware") |
| 303 |
device_type = module.params.get("device_type") |
| 304 |
function_type = L4L7_FUNC_TYPES_MAPPING.get(module.params.get("function_type")) |
| 305 |
managed = aci.boolean(module.params.get("managed")) |
| 306 |
promiscuous_mode = aci.boolean(module.params.get("promiscuous_mode")) |
| 307 |
service_type = module.params.get("service_type") |
| 308 |
trunking = aci.boolean(module.params.get("trunking")) |
| 309 |
domain = module.params.get("domain") |
| 310 |
active_active_mode = aci.boolean(module.params.get("active_active_mode")) |
| 311 |
|
| 312 |
aci.construct_url( |
| 313 |
root_class=dict( |
| 314 |
aci_class="fvTenant", |
| 315 |
aci_rn="tn-{0}".format(tenant), |
| 316 |
module_object=tenant, |
| 317 |
target_filter={"name": tenant}, |
| 318 |
), |
| 319 |
subclass_1=dict( |
| 320 |
aci_class="vnsLDevVip", |
| 321 |
aci_rn="lDevVip-{0}".format(name), |
| 322 |
module_object=name, |
| 323 |
target_filter={"name": name}, |
| 324 |
), |
| 325 |
child_classes=["vnsRsALDevToPhysDomP", "vnsCDev", "vnsRsALDevToDomP"], |
| 326 |
) |
| 327 |
|
| 328 |
aci.get_existing() |
| 329 |
|
| 330 |
if state == "present": |
| 331 |
child_configs = [] |
| 332 |
if device_type == "virtual": |
| 333 |
dom_class = "vnsRsALDevToDomP" |
| 334 |
tdn = "uni/vmmp-VMware/dom-{0}".format(domain) |
| 335 |
else: |
| 336 |
dom_class = "vnsRsALDevToPhysDomP" |
| 337 |
tdn = "uni/phys-{0}".format(domain) |
| 338 |
child_configs.append({dom_class: {"attributes": {"tDn": tdn}}}) |
| 339 |
|
| 340 |
aci.payload( |
| 341 |
aci_class="vnsLDevVip", |
| 342 |
class_config=dict( |
| 343 |
name=name, |
| 344 |
contextAware="{0}-Context".format(context_aware), |
| 345 |
devtype=device_type.upper(), |
| 346 |
funcType=function_type, |
| 347 |
managed=managed, |
| 348 |
promMode=promiscuous_mode, |
| 349 |
svcType=service_type.upper(), |
| 350 |
trunking=trunking, |
| 351 |
activeActive=active_active_mode, |
| 352 |
), |
| 353 |
child_configs=child_configs, |
| 354 |
) |
| 355 |
|
| 356 |
aci.get_diff(aci_class="vnsLDevVip") |
| 357 |
|
| 358 |
aci.post_config() |
| 359 |
|
| 360 |
elif state == "absent": |
| 361 |
aci.delete_config() |
| 362 |
|
| 363 |
aci.exit_json() |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == "__main__": |
| 367 |
main() |