| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]> |
| 5 |
# Copyright: (c) 2023, Tim Cragg (@timcragg) <[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_match_route_destination |
| 17 |
short_description: Manage Match action rule term based on the Route Destination (rtctrl:MatchRtDest) |
| 18 |
description: |
| 19 |
- Match action rule terms based on the Route Destination for Subject Profiles on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
tenant: |
| 22 |
description: |
| 23 |
- The name of an existing tenant. |
| 24 |
type: str |
| 25 |
aliases: [ tenant_name ] |
| 26 |
match_rule: |
| 27 |
description: |
| 28 |
- The name of an exising match rule profile. |
| 29 |
type: str |
| 30 |
aliases: [ match_rule_name ] |
| 31 |
ip: |
| 32 |
description: |
| 33 |
- The IP address of the route destination. |
| 34 |
type: str |
| 35 |
aggregate: |
| 36 |
description: |
| 37 |
- Option to enable/disable aggregated route. |
| 38 |
type: str |
| 39 |
choices: [ "no", "yes" ] |
| 40 |
from_prefix_length: |
| 41 |
description: |
| 42 |
- The start of the prefix length. |
| 43 |
- It corresponds to the lesser Mask if the route is aggregated. |
| 44 |
type: int |
| 45 |
to_prefix_length: |
| 46 |
description: |
| 47 |
- The end of the prefix length. |
| 48 |
- It corresponds to greater Mask if the route is aggregated. |
| 49 |
type: int |
| 50 |
description: |
| 51 |
description: |
| 52 |
- The description for the match action rule term. |
| 53 |
type: str |
| 54 |
aliases: [ descr ] |
| 55 |
state: |
| 56 |
description: |
| 57 |
- Use C(present) or C(absent) for adding or removing. |
| 58 |
- Use C(query) for listing an object or multiple objects. |
| 59 |
type: str |
| 60 |
choices: [ absent, present, query ] |
| 61 |
default: present |
| 62 |
name_alias: |
| 63 |
description: |
| 64 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 65 |
type: str |
| 66 |
extends_documentation_fragment: |
| 67 |
- cisco.aci.aci |
| 68 |
- cisco.aci.annotation |
| 69 |
- cisco.aci.owner |
| 70 |
|
| 71 |
notes: |
| 72 |
- The C(tenant) and the C(match_rule) used must exist before using this module in your playbook. |
| 73 |
The M(cisco.aci.aci_tenant) and the M(cisco.aci.aci_match_rule) modules can be used for this. |
| 74 |
seealso: |
| 75 |
- module: cisco.aci.aci_tenant |
| 76 |
- module: cisco.aci.aci_match_rule |
| 77 |
- name: APIC Management Information Model reference |
| 78 |
description: More information about the internal APIC class B(rtctrl:MatchRtDest). |
| 79 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 80 |
author: |
| 81 |
- Gaspard Micol (@gmicol) |
| 82 |
- Tim Cragg (@timcragg) |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
- name: Create a match rule destination |
| 87 |
cisco.aci.aci_match_rule_destination: |
| 88 |
host: apic |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
match_rule: prod_match_rule |
| 92 |
tenant: production |
| 93 |
ip: 11.11.11.11/24 |
| 94 |
aggregate: "yes" |
| 95 |
from_prefix_length: 0 |
| 96 |
to_prefix_length: 32 |
| 97 |
state: present |
| 98 |
delegate_to: localhost |
| 99 |
|
| 100 |
- name: Delete a match rule destination |
| 101 |
cisco.aci.aci_match_rule_destination: |
| 102 |
host: apic |
| 103 |
username: admin |
| 104 |
password: SomeSecretPassword |
| 105 |
match_rule: prod_match_rule |
| 106 |
tenant: production |
| 107 |
state: absent |
| 108 |
delegate_to: localhost |
| 109 |
|
| 110 |
- name: Query all match rules destination |
| 111 |
cisco.aci.aci_match_rule_destination: |
| 112 |
host: apic |
| 113 |
username: admin |
| 114 |
password: SomeSecretPassword |
| 115 |
state: query |
| 116 |
delegate_to: localhost |
| 117 |
register: query_result |
| 118 |
|
| 119 |
- name: Query a specific match rule destination |
| 120 |
cisco.aci.aci_match_rule_destination: |
| 121 |
host: apic |
| 122 |
username: admin |
| 123 |
password: SomeSecretPassword |
| 124 |
match_rule: prod_match_rule |
| 125 |
tenant: production |
| 126 |
ip: 11.11.11.11/24 |
| 127 |
state: query |
| 128 |
delegate_to: localhost |
| 129 |
register: query_result |
| 130 |
""" |
| 131 |
|
| 132 |
RETURN = r""" |
| 133 |
current: |
| 134 |
description: The existing configuration from the APIC after the module has finished |
| 135 |
returned: success |
| 136 |
type: list |
| 137 |
sample: |
| 138 |
[ |
| 139 |
{ |
| 140 |
"fvTenant": { |
| 141 |
"attributes": { |
| 142 |
"descr": "Production environment", |
| 143 |
"dn": "uni/tn-production", |
| 144 |
"name": "production", |
| 145 |
"nameAlias": "", |
| 146 |
"ownerKey": "", |
| 147 |
"ownerauto_continue": "" |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
] |
| 152 |
error: |
| 153 |
description: The error information as returned from the APIC |
| 154 |
returned: failure |
| 155 |
type: dict |
| 156 |
sample: |
| 157 |
{ |
| 158 |
"code": "122", |
| 159 |
"text": "unknown managed object class foo" |
| 160 |
} |
| 161 |
raw: |
| 162 |
description: The raw output returned by the APIC REST API (xml or json) |
| 163 |
returned: parse error |
| 164 |
type: str |
| 165 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 166 |
sent: |
| 167 |
description: The actual/minimal configuration pushed to the APIC |
| 168 |
returned: info |
| 169 |
type: list |
| 170 |
sample: |
| 171 |
{ |
| 172 |
"fvTenant": { |
| 173 |
"attributes": { |
| 174 |
"descr": "Production environment" |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
previous: |
| 179 |
description: The original configuration from the APIC before the module has started |
| 180 |
returned: info |
| 181 |
type: list |
| 182 |
sample: |
| 183 |
[ |
| 184 |
{ |
| 185 |
"fvTenant": { |
| 186 |
"attributes": { |
| 187 |
"descr": "Production", |
| 188 |
"dn": "uni/tn-production", |
| 189 |
"name": "production", |
| 190 |
"nameAlias": "", |
| 191 |
"ownerKey": "", |
| 192 |
"ownerauto_continue": "" |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
] |
| 197 |
proposed: |
| 198 |
description: The assembled configuration from the user-provided parameters |
| 199 |
returned: info |
| 200 |
type: dict |
| 201 |
sample: |
| 202 |
{ |
| 203 |
"fvTenant": { |
| 204 |
"attributes": { |
| 205 |
"descr": "Production environment", |
| 206 |
"name": "production" |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
filter_string: |
| 211 |
description: The filter string used for the request |
| 212 |
returned: failure or debug |
| 213 |
type: str |
| 214 |
sample: ?rsp-prop-include=config-only |
| 215 |
method: |
| 216 |
description: The HTTP method used for the request to the APIC |
| 217 |
returned: failure or debug |
| 218 |
type: str |
| 219 |
sample: POST |
| 220 |
response: |
| 221 |
description: The HTTP response from the APIC |
| 222 |
returned: failure or debug |
| 223 |
type: str |
| 224 |
sample: OK (30 bytes) |
| 225 |
status: |
| 226 |
description: The HTTP status from the APIC |
| 227 |
returned: failure or debug |
| 228 |
type: int |
| 229 |
sample: 200 |
| 230 |
url: |
| 231 |
description: The HTTP url used for the request to the APIC |
| 232 |
returned: failure or debug |
| 233 |
type: str |
| 234 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 235 |
""" |
| 236 |
|
| 237 |
from ansible.module_utils.basic import AnsibleModule |
| 238 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 239 |
|
| 240 |
|
| 241 |
def main(): |
| 242 |
argument_spec = aci_argument_spec() |
| 243 |
argument_spec.update(aci_annotation_spec()) |
| 244 |
argument_spec.update(aci_owner_spec()) |
| 245 |
argument_spec.update( |
| 246 |
tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects |
| 247 |
match_rule=dict(type="str", aliases=["match_rule_name"]), # Not required for querying all objects |
| 248 |
ip=dict(type="str"), |
| 249 |
aggregate=dict(type="str", choices=["no", "yes"]), |
| 250 |
from_prefix_length=dict(type="int"), |
| 251 |
to_prefix_length=dict(type="int"), |
| 252 |
description=dict(type="str", aliases=["descr"]), |
| 253 |
name_alias=dict(type="str"), |
| 254 |
state=dict(type="str", default="present", choices=["present", "absent", "query"]), |
| 255 |
) |
| 256 |
|
| 257 |
module = AnsibleModule( |
| 258 |
argument_spec=argument_spec, |
| 259 |
supports_check_mode=True, |
| 260 |
required_if=[ |
| 261 |
["state", "absent", ["ip", "tenant", "match_rule"]], |
| 262 |
["state", "present", ["ip", "tenant", "match_rule"]], |
| 263 |
], |
| 264 |
) |
| 265 |
|
| 266 |
ip = module.params.get("ip") |
| 267 |
description = module.params.get("description") |
| 268 |
aggregate = module.params.get("aggregate") |
| 269 |
from_prefix_length = module.params.get("from_prefix_length") |
| 270 |
to_prefix_length = module.params.get("to_prefix_length") |
| 271 |
state = module.params.get("state") |
| 272 |
tenant = module.params.get("tenant") |
| 273 |
match_rule = module.params.get("match_rule") |
| 274 |
name_alias = module.params.get("name_alias") |
| 275 |
|
| 276 |
aci = ACIModule(module) |
| 277 |
|
| 278 |
aci.construct_url( |
| 279 |
root_class=dict( |
| 280 |
aci_class="fvTenant", |
| 281 |
aci_rn="tn-{0}".format(tenant), |
| 282 |
module_object=tenant, |
| 283 |
target_filter={"name": tenant}, |
| 284 |
), |
| 285 |
subclass_1=dict( |
| 286 |
aci_class="rtctrlSubjP", |
| 287 |
aci_rn="subj-{0}".format(match_rule), |
| 288 |
module_object=match_rule, |
| 289 |
target_filter={"name": match_rule}, |
| 290 |
), |
| 291 |
subclass_2=dict( |
| 292 |
aci_class="rtctrlMatchRtDest", |
| 293 |
aci_rn="dest-[{0}]".format(ip), |
| 294 |
module_object=ip, |
| 295 |
target_filter={"ip": ip}, |
| 296 |
), |
| 297 |
) |
| 298 |
|
| 299 |
aci.get_existing() |
| 300 |
|
| 301 |
if state == "present": |
| 302 |
aci.payload( |
| 303 |
aci_class="rtctrlMatchRtDest", |
| 304 |
class_config=dict( |
| 305 |
ip=ip, |
| 306 |
aggregate=aggregate, |
| 307 |
fromPfxLen=from_prefix_length, |
| 308 |
toPfxLen=to_prefix_length, |
| 309 |
descr=description, |
| 310 |
nameAlias=name_alias, |
| 311 |
), |
| 312 |
) |
| 313 |
|
| 314 |
aci.get_diff(aci_class="rtctrlMatchRtDest") |
| 315 |
|
| 316 |
aci.post_config() |
| 317 |
|
| 318 |
elif state == "absent": |
| 319 |
aci.delete_config() |
| 320 |
|
| 321 |
aci.exit_json() |
| 322 |
|
| 323 |
|
| 324 |
if __name__ == "__main__": |
| 325 |
main() |