| 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_logical_interface |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage L4-L7 Logical Interface (vns:LIf) |
| 19 |
description: |
| 20 |
- Manage Layer 4 to Layer 7 (L4-L7) Logical Interfaces. |
| 21 |
options: |
| 22 |
tenant: |
| 23 |
description: |
| 24 |
- The name of an existing tenant. |
| 25 |
type: str |
| 26 |
aliases: [ tenant_name ] |
| 27 |
logical_device: |
| 28 |
description: |
| 29 |
- The name of an existing Logical Device. |
| 30 |
type: str |
| 31 |
aliases: [ device_name, device, logical_device_name ] |
| 32 |
logical_interface: |
| 33 |
description: |
| 34 |
- The name of an existing Logical Interface. |
| 35 |
type: str |
| 36 |
aliases: [ name ] |
| 37 |
encap: |
| 38 |
description: |
| 39 |
- The encapsulation of the Logical Interface. |
| 40 |
- It requires the VLAN to be prepended, for example, 'vlan-987'. |
| 41 |
type: str |
| 42 |
enhanced_lag_policy: |
| 43 |
description: |
| 44 |
- Name of the VMM Domain Enhanced Lag Policy. |
| 45 |
type: str |
| 46 |
aliases: [ lag_policy ] |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Use C(present) or C(absent) for adding or removing. |
| 50 |
- Use C(query) for listing an object or multiple objects. |
| 51 |
type: str |
| 52 |
choices: [ absent, present, query ] |
| 53 |
default: present |
| 54 |
extends_documentation_fragment: |
| 55 |
- cisco.aci.aci |
| 56 |
- cisco.aci.annotation |
| 57 |
notes: |
| 58 |
- The I(tenant) and I(logical_device) must exist before using this module in your playbook. |
| 59 |
The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_l4l7_device) modules can be used for this. |
| 60 |
seealso: |
| 61 |
- module: cisco.aci.aci_tenant |
| 62 |
- module: cisco.aci.aci_l4l7_device |
| 63 |
- name: APIC Management Information Model reference |
| 64 |
description: More information about the internal APIC class B(vns:LIf) |
| 65 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 66 |
author: |
| 67 |
- Tim Cragg (@timcragg) |
| 68 |
- Shreyas Srish (@shrsr) |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Add a new logical interface |
| 73 |
cisco.aci.aci_l4l7_logical_interface: |
| 74 |
host: apic |
| 75 |
username: admin |
| 76 |
password: SomeSecretPassword |
| 77 |
tenant: my_tenant |
| 78 |
device: my_device |
| 79 |
logical_interface: my_log_intf |
| 80 |
encap: vlan-987 |
| 81 |
state: present |
| 82 |
delegate_to: localhost |
| 83 |
|
| 84 |
- name: Query a logical interface |
| 85 |
cisco.aci.aci_l4l7_logical_interface: |
| 86 |
host: apic |
| 87 |
username: admin |
| 88 |
password: SomeSecretPassword |
| 89 |
tenant: my_tenant |
| 90 |
device: my_device |
| 91 |
logical_interface: my_log_intf |
| 92 |
state: query |
| 93 |
delegate_to: localhost |
| 94 |
register: query_result |
| 95 |
|
| 96 |
- name: Query all logical interfaces |
| 97 |
cisco.aci.aci_l4l7_logical_interface: |
| 98 |
host: apic |
| 99 |
username: admin |
| 100 |
password: SomeSecretPassword |
| 101 |
state: query |
| 102 |
delegate_to: localhost |
| 103 |
register: query_result |
| 104 |
|
| 105 |
- name: Delete a logical interface |
| 106 |
cisco.aci.aci_l4l7_logical_interface: |
| 107 |
host: apic |
| 108 |
username: admin |
| 109 |
password: SomeSecretPassword |
| 110 |
tenant: my_tenant |
| 111 |
device: my_device |
| 112 |
logical_interface: my_log_intf |
| 113 |
state: absent |
| 114 |
delegate_to: localhost |
| 115 |
""" |
| 116 |
|
| 117 |
RETURN = r""" |
| 118 |
current: |
| 119 |
description: The existing configuration from the APIC after the module has finished |
| 120 |
returned: success |
| 121 |
type: list |
| 122 |
sample: |
| 123 |
[ |
| 124 |
{ |
| 125 |
"fvTenant": { |
| 126 |
"attributes": { |
| 127 |
"descr": "Production environment", |
| 128 |
"dn": "uni/tn-production", |
| 129 |
"name": "production", |
| 130 |
"nameAlias": "", |
| 131 |
"ownerKey": "", |
| 132 |
"ownerTag": "" |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
] |
| 137 |
error: |
| 138 |
description: The error information as returned from the APIC |
| 139 |
returned: failure |
| 140 |
type: dict |
| 141 |
sample: |
| 142 |
{ |
| 143 |
"code": "122", |
| 144 |
"text": "unknown managed object class foo" |
| 145 |
} |
| 146 |
raw: |
| 147 |
description: The raw output returned by the APIC REST API (xml or json) |
| 148 |
returned: parse error |
| 149 |
type: str |
| 150 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 151 |
sent: |
| 152 |
description: The actual/minimal configuration pushed to the APIC |
| 153 |
returned: info |
| 154 |
type: list |
| 155 |
sample: |
| 156 |
{ |
| 157 |
"fvTenant": { |
| 158 |
"attributes": { |
| 159 |
"descr": "Production environment" |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
previous: |
| 164 |
description: The original configuration from the APIC before the module has started |
| 165 |
returned: info |
| 166 |
type: list |
| 167 |
sample: |
| 168 |
[ |
| 169 |
{ |
| 170 |
"fvTenant": { |
| 171 |
"attributes": { |
| 172 |
"descr": "Production", |
| 173 |
"dn": "uni/tn-production", |
| 174 |
"name": "production", |
| 175 |
"nameAlias": "", |
| 176 |
"ownerKey": "", |
| 177 |
"ownerTag": "" |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
] |
| 182 |
proposed: |
| 183 |
description: The assembled configuration from the user-provided parameters |
| 184 |
returned: info |
| 185 |
type: dict |
| 186 |
sample: |
| 187 |
{ |
| 188 |
"fvTenant": { |
| 189 |
"attributes": { |
| 190 |
"descr": "Production environment", |
| 191 |
"name": "production" |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
filter_string: |
| 196 |
description: The filter string used for the request |
| 197 |
returned: failure or debug |
| 198 |
type: str |
| 199 |
sample: ?rsp-prop-include=config-only |
| 200 |
method: |
| 201 |
description: The HTTP method used for the request to the APIC |
| 202 |
returned: failure or debug |
| 203 |
type: str |
| 204 |
sample: POST |
| 205 |
response: |
| 206 |
description: The HTTP response from the APIC |
| 207 |
returned: failure or debug |
| 208 |
type: str |
| 209 |
sample: OK (30 bytes) |
| 210 |
status: |
| 211 |
description: The HTTP status from the APIC |
| 212 |
returned: failure or debug |
| 213 |
type: int |
| 214 |
sample: 200 |
| 215 |
url: |
| 216 |
description: The HTTP url used for the request to the APIC |
| 217 |
returned: failure or debug |
| 218 |
type: str |
| 219 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 220 |
""" |
| 221 |
|
| 222 |
|
| 223 |
from ansible.module_utils.basic import AnsibleModule |
| 224 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
argument_spec = aci_argument_spec() |
| 229 |
argument_spec.update(aci_annotation_spec()) |
| 230 |
argument_spec.update( |
| 231 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 232 |
logical_device=dict(type="str", aliases=["device_name", "device", "logical_device_name"]), |
| 233 |
logical_interface=dict(type="str", aliases=["name"]), |
| 234 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 235 |
encap=dict(type="str"), |
| 236 |
enhanced_lag_policy=dict(type="str", aliases=["lag_policy"]), |
| 237 |
) |
| 238 |
|
| 239 |
module = AnsibleModule( |
| 240 |
argument_spec=argument_spec, |
| 241 |
supports_check_mode=True, |
| 242 |
required_if=[ |
| 243 |
["state", "absent", ["tenant", "logical_device", "logical_interface"]], |
| 244 |
["state", "present", ["tenant", "logical_device", "logical_interface"]], |
| 245 |
], |
| 246 |
) |
| 247 |
|
| 248 |
tenant = module.params.get("tenant") |
| 249 |
state = module.params.get("state") |
| 250 |
logical_device = module.params.get("logical_device") |
| 251 |
logical_interface = module.params.get("logical_interface") |
| 252 |
encap = module.params.get("encap") |
| 253 |
enhanced_lag_policy = module.params.get("enhanced_lag_policy") |
| 254 |
|
| 255 |
aci = ACIModule(module) |
| 256 |
|
| 257 |
aci.construct_url( |
| 258 |
root_class=dict( |
| 259 |
aci_class="fvTenant", |
| 260 |
aci_rn="tn-{0}".format(tenant), |
| 261 |
module_object=tenant, |
| 262 |
target_filter={"name": tenant}, |
| 263 |
), |
| 264 |
subclass_1=dict( |
| 265 |
aci_class="vnsLDevVip", |
| 266 |
aci_rn="lDevVip-{0}".format(logical_device), |
| 267 |
module_object=logical_device, |
| 268 |
target_filter={"name": logical_device}, |
| 269 |
), |
| 270 |
subclass_2=dict( |
| 271 |
aci_class="vnsLIf", |
| 272 |
aci_rn="lIf-{0}".format(logical_interface), |
| 273 |
module_object=logical_interface, |
| 274 |
target_filter={"name": logical_interface}, |
| 275 |
), |
| 276 |
) |
| 277 |
|
| 278 |
aci.get_existing() |
| 279 |
|
| 280 |
if state == "present": |
| 281 |
aci.payload( |
| 282 |
aci_class="vnsLIf", |
| 283 |
class_config=dict(name=logical_interface, encap=encap, lagPolicyName=enhanced_lag_policy), |
| 284 |
) |
| 285 |
aci.get_diff(aci_class="vnsLIf") |
| 286 |
|
| 287 |
aci.post_config() |
| 288 |
|
| 289 |
elif state == "absent": |
| 290 |
aci.delete_config() |
| 291 |
|
| 292 |
aci.exit_json() |
| 293 |
|
| 294 |
|
| 295 |
if __name__ == "__main__": |
| 296 |
main() |