| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross (@akiniross) <[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: mso_schema_template_vrf_rp |
| 16 |
short_description: Manage VRF Rendezvous Points (RP) in schema templates. |
| 17 |
description: |
| 18 |
- Manage VRF Rendezvous Points (RP) in schema templates on Cisco ACI Multi-Site. |
| 19 |
- RPs can only be configured when the VRF has Layer 3 Multicast is enabled. |
| 20 |
author: |
| 21 |
- Akini Ross (@akinross) |
| 22 |
options: |
| 23 |
schema: |
| 24 |
description: |
| 25 |
- The name of the schema. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
template: |
| 29 |
description: |
| 30 |
- The name of the template. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
vrf: |
| 34 |
description: |
| 35 |
- The name of the VRF. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
ip: |
| 39 |
description: |
| 40 |
- The IP address of the RP. |
| 41 |
type: str |
| 42 |
aliases: [ ip_address ] |
| 43 |
type: |
| 44 |
description: |
| 45 |
- The type of the RP. |
| 46 |
type: str |
| 47 |
choices: [ fabric, static ] |
| 48 |
multicast_route_map_policy: |
| 49 |
description: |
| 50 |
- The name of the Multicast Route Map Policy. |
| 51 |
- The Multicast Route Map Policy must reside in the same tenant as the tenant associated to the schema. |
| 52 |
- Use O(multicast_route_map_policy="") to remove the Multicast Route Map Policy. |
| 53 |
type: str |
| 54 |
aliases: [ route_map_policy ] |
| 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 |
extends_documentation_fragment: cisco.mso.modules |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
- name: Add a new VRF RP |
| 67 |
cisco.mso.mso_schema_template_vrf_rp: |
| 68 |
host: mso_host |
| 69 |
username: admin |
| 70 |
password: SomeSecretPassword |
| 71 |
schema: Schema 1 |
| 72 |
template: Template 1 |
| 73 |
vrf: VRF 1 |
| 74 |
ip: 1.1.1.1 |
| 75 |
type: static |
| 76 |
multicast_route_map_policy: RouteMapPolicy1 |
| 77 |
state: present |
| 78 |
|
| 79 |
- name: Query a specific VRF RP |
| 80 |
cisco.mso.mso_schema_template_vrf_rp: |
| 81 |
host: mso_host |
| 82 |
username: admin |
| 83 |
password: SomeSecretPassword |
| 84 |
schema: Schema 1 |
| 85 |
template: Template 1 |
| 86 |
vrf: VRF 1 |
| 87 |
ip: 1.1.1.1 |
| 88 |
state: query |
| 89 |
register: query_result |
| 90 |
|
| 91 |
- name: Query all VRF RPs |
| 92 |
cisco.mso.mso_schema_template_vrf_rp: |
| 93 |
host: mso_host |
| 94 |
username: admin |
| 95 |
password: SomeSecretPassword |
| 96 |
schema: Schema 1 |
| 97 |
template: Template 1 |
| 98 |
vrf: VRF 1 |
| 99 |
state: query |
| 100 |
register: query_result |
| 101 |
|
| 102 |
- name: Remove an VRF RP |
| 103 |
cisco.mso.mso_schema_template_vrf_rp: |
| 104 |
host: mso_host |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
schema: Schema 1 |
| 108 |
template: Template 1 |
| 109 |
vrf: VRF 1 |
| 110 |
ip: 1.1.1.1 |
| 111 |
state: absent |
| 112 |
""" |
| 113 |
|
| 114 |
RETURN = r""" |
| 115 |
""" |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 119 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 120 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate |
| 121 |
|
| 122 |
|
| 123 |
def main(): |
| 124 |
argument_spec = mso_argument_spec() |
| 125 |
argument_spec.update( |
| 126 |
schema=dict(type="str", required=True), |
| 127 |
template=dict(type="str", required=True), |
| 128 |
vrf=dict(type="str", required=True), |
| 129 |
ip=dict(type="str", aliases=["ip_address"]), |
| 130 |
type=dict(type="str", choices=["fabric", "static"]), |
| 131 |
multicast_route_map_policy=dict(type="str", aliases=["route_map_policy"]), |
| 132 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 133 |
) |
| 134 |
|
| 135 |
module = AnsibleModule( |
| 136 |
argument_spec=argument_spec, |
| 137 |
supports_check_mode=True, |
| 138 |
required_if=[ |
| 139 |
["state", "absent", ["ip"]], |
| 140 |
["state", "present", ["ip", "type"]], |
| 141 |
], |
| 142 |
) |
| 143 |
|
| 144 |
schema = module.params.get("schema") |
| 145 |
template = module.params.get("template").replace(" ", "") |
| 146 |
vrf = module.params.get("vrf") |
| 147 |
ip = module.params.get("ip") |
| 148 |
rp_type = module.params.get("type") |
| 149 |
multicast_route_map_policy = module.params.get("multicast_route_map_policy") |
| 150 |
state = module.params.get("state") |
| 151 |
|
| 152 |
rps_path = "/templates/{0}/vrfs/{1}/rpConfigs".format(template, vrf) |
| 153 |
|
| 154 |
mso = MSOModule(module) |
| 155 |
|
| 156 |
mso_schema = MSOSchema(mso, schema, template) |
| 157 |
mso_schema.set_template(template) |
| 158 |
mso_schema.set_template_vrf(vrf) |
| 159 |
|
| 160 |
if state == "query": |
| 161 |
if ip: |
| 162 |
mso_schema.set_template_vrf_rp(ip) |
| 163 |
mso.existing = mso_schema.schema_objects.get("template_vrf_rp").details |
| 164 |
else: |
| 165 |
mso.existing = mso_schema.schema_objects.get("template_vrf").details.get("rpConfigs", []) |
| 166 |
mso.exit_json() |
| 167 |
|
| 168 |
mso_schema.set_template_vrf_rp(ip, False) |
| 169 |
|
| 170 |
rp = mso_schema.schema_objects.get("template_vrf_rp") |
| 171 |
ops = [] |
| 172 |
|
| 173 |
mso.existing = mso.previous = rp.details if rp else mso.existing |
| 174 |
|
| 175 |
if state == "absent": |
| 176 |
if mso.existing: |
| 177 |
mso.sent = mso.existing = {} |
| 178 |
ops.append(dict(op="remove", path="{0}/{1}".format(rps_path, rp.index))) |
| 179 |
|
| 180 |
elif state == "present": |
| 181 |
|
| 182 |
payload = dict(ipAddress=ip, rpType=rp_type) |
| 183 |
|
| 184 |
if multicast_route_map_policy: |
| 185 |
payload["mcastRtMapPolicyRef"] = get_route_map_uuid( |
| 186 |
mso, mso_schema.schema_objects.get("template").details.get("tenantId"), multicast_route_map_policy |
| 187 |
) |
| 188 |
|
| 189 |
mso.sanitize(payload, collate=True) |
| 190 |
|
| 191 |
if mso.existing: |
| 192 |
rp_path = "{0}/{1}".format(rps_path, rp.index) |
| 193 |
if rp_type != mso.existing.get("rpType"): |
| 194 |
ops.append(dict(op="replace", path=rp_path + "/rpType", value=rp_type)) |
| 195 |
if multicast_route_map_policy and payload.get("mcastRtMapPolicyRef") != mso.existing.get("mcastRtMapPolicyRef"): |
| 196 |
ops.append( |
| 197 |
dict( |
| 198 |
op="replace" if mso.existing.get("mcastRtMapPolicyRef") is not None else "add", |
| 199 |
path=rp_path + "/mcastRtMapPolicyRef", |
| 200 |
value=payload.get("mcastRtMapPolicyRef"), |
| 201 |
) |
| 202 |
) |
| 203 |
if multicast_route_map_policy == "": |
| 204 |
ops.append(dict(op="remove", path=rp_path + "/mcastRtMapPolicyRef")) |
| 205 |
else: |
| 206 |
ops.append(dict(op="add", path=rps_path + "/-", value=mso.sent)) |
| 207 |
|
| 208 |
mso.existing = mso.proposed |
| 209 |
|
| 210 |
if not module.check_mode: |
| 211 |
mso.request(mso_schema.path, method="PATCH", data=ops) |
| 212 |
|
| 213 |
mso.exit_json() |
| 214 |
|
| 215 |
|
| 216 |
def get_route_map_uuid(mso, tenant_id, multicast_route_map_policy): |
| 217 |
|
| 218 |
# Only tenant type templates contain the correct route map policies |
| 219 |
# Retrieves the list templates that contain the same tenant because only route map policies for the tenant assigned to the schema are options to be chosen |
| 220 |
templates = [ |
| 221 |
MSOTemplate(mso, template_id=template.get("templateId")) for template in MSOTemplate(mso, "tenant").template if template.get("tenantId") == tenant_id |
| 222 |
] |
| 223 |
|
| 224 |
# NDO restricts route map policies in the same tenant to have the same name thus we can loop through the route map policies to find the correct uuid |
| 225 |
for template in templates: |
| 226 |
for route_map_policy in template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("mcastRouteMapPolicies", []): |
| 227 |
if route_map_policy.get("name") == multicast_route_map_policy: |
| 228 |
return route_map_policy.get("uuid") |
| 229 |
|
| 230 |
mso.fail_json(msg="Multicast Route Map Policy '{0}' not found.".format(multicast_route_map_policy)) |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
main() |