| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]> |
| 5 |
# Copyright: (c) 2023, Anvitha Jain (@anvitha-jain) <[email protected]> |
| 6 |
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 14 |
|
| 15 |
DOCUMENTATION = r""" |
| 16 |
--- |
| 17 |
module: mso_schema_template_vrf |
| 18 |
short_description: Manage VRFs in schema templates |
| 19 |
description: |
| 20 |
- Manage VRFs in schema templates on Cisco ACI Multi-Site. |
| 21 |
author: |
| 22 |
- Akini Ross (@akinross) |
| 23 |
- Anvitha Jain (@anvitha-jain) |
| 24 |
- Dag Wieers (@dagwieers) |
| 25 |
options: |
| 26 |
schema: |
| 27 |
description: |
| 28 |
- The name of the schema. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
template: |
| 32 |
description: |
| 33 |
- The name of the template. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
vrf: |
| 37 |
description: |
| 38 |
- The name of the VRF to manage. |
| 39 |
type: str |
| 40 |
aliases: [ name ] |
| 41 |
display_name: |
| 42 |
description: |
| 43 |
- The name as displayed on the MSO web interface. |
| 44 |
type: str |
| 45 |
layer3_multicast: |
| 46 |
description: |
| 47 |
- Whether to enable L3 multicast. |
| 48 |
type: bool |
| 49 |
vzany: |
| 50 |
description: |
| 51 |
- Whether to enable vzAny. |
| 52 |
type: bool |
| 53 |
ip_data_plane_learning: |
| 54 |
description: |
| 55 |
- Whether IP data plane learning is enabled or disabled. |
| 56 |
- The APIC defaults to C(enabled) when unset during creation. |
| 57 |
type: str |
| 58 |
choices: [ disabled, enabled ] |
| 59 |
preferred_group: |
| 60 |
description: |
| 61 |
- Whether to enable preferred Endpoint Group. |
| 62 |
- The APIC defaults to C(false) when unset during creation. |
| 63 |
type: bool |
| 64 |
site_aware_policy_enforcement: |
| 65 |
description: |
| 66 |
- Whether to enable Site-aware Policy Enforcement Mode. |
| 67 |
- Enabling or Disabling Site-aware Policy Enforcement Mode will cause temporary traffic disruption. |
| 68 |
- Enabling Site-aware Policy Enforcement Mode will increase TCAM usage for existing contracts. |
| 69 |
- Site-aware Policy Enforcement needs to be enabled in the consumer and provider VRF when an inter-VRF contract is required. |
| 70 |
- Contract permit logging cannot be used when Site-aware Policy Enforcement Mode is enabled. |
| 71 |
type: bool |
| 72 |
bd_enforcement_status: |
| 73 |
description: |
| 74 |
- Whether to enable BD Enforcement Status. |
| 75 |
type: bool |
| 76 |
state: |
| 77 |
description: |
| 78 |
- Use C(present) or C(absent) for adding or removing. |
| 79 |
- Use C(query) for listing an object or multiple objects. |
| 80 |
type: str |
| 81 |
choices: [ absent, present, query ] |
| 82 |
default: present |
| 83 |
extends_documentation_fragment: cisco.mso.modules |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Add a new VRF |
| 88 |
cisco.mso.mso_schema_template_vrf: |
| 89 |
host: mso_host |
| 90 |
username: admin |
| 91 |
password: SomeSecretPassword |
| 92 |
schema: Schema 1 |
| 93 |
template: Template 1 |
| 94 |
vrf: VRF 1 |
| 95 |
state: present |
| 96 |
|
| 97 |
- name: Remove an VRF |
| 98 |
cisco.mso.mso_schema_template_vrf: |
| 99 |
host: mso_host |
| 100 |
username: admin |
| 101 |
password: SomeSecretPassword |
| 102 |
schema: Schema 1 |
| 103 |
template: Template 1 |
| 104 |
vrf: VRF1 |
| 105 |
state: absent |
| 106 |
|
| 107 |
- name: Query a specific VRFs |
| 108 |
cisco.mso.mso_schema_template_vrf: |
| 109 |
host: mso_host |
| 110 |
username: admin |
| 111 |
password: SomeSecretPassword |
| 112 |
schema: Schema 1 |
| 113 |
template: Template 1 |
| 114 |
vrf: VRF1 |
| 115 |
state: query |
| 116 |
register: query_result |
| 117 |
|
| 118 |
- name: Query all VRFs |
| 119 |
cisco.mso.mso_schema_template_vrf: |
| 120 |
host: mso_host |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
schema: Schema 1 |
| 124 |
template: Template 1 |
| 125 |
state: query |
| 126 |
register: query_result |
| 127 |
""" |
| 128 |
|
| 129 |
RETURN = r""" |
| 130 |
""" |
| 131 |
|
| 132 |
from ansible.module_utils.basic import AnsibleModule |
| 133 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 134 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 135 |
|
| 136 |
|
| 137 |
def main(): |
| 138 |
argument_spec = mso_argument_spec() |
| 139 |
argument_spec.update( |
| 140 |
schema=dict(type="str", required=True), |
| 141 |
template=dict(type="str", required=True), |
| 142 |
vrf=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects |
| 143 |
display_name=dict(type="str"), |
| 144 |
layer3_multicast=dict(type="bool"), |
| 145 |
vzany=dict(type="bool"), |
| 146 |
preferred_group=dict(type="bool"), |
| 147 |
ip_data_plane_learning=dict(type="str", choices=["enabled", "disabled"]), |
| 148 |
site_aware_policy_enforcement=dict(type="bool"), |
| 149 |
bd_enforcement_status=dict(type="bool"), |
| 150 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 151 |
) |
| 152 |
|
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec=argument_spec, |
| 155 |
supports_check_mode=True, |
| 156 |
required_if=[ |
| 157 |
["state", "absent", ["vrf"]], |
| 158 |
["state", "present", ["vrf"]], |
| 159 |
], |
| 160 |
) |
| 161 |
|
| 162 |
schema = module.params.get("schema") |
| 163 |
template = module.params.get("template").replace(" ", "") |
| 164 |
vrf = module.params.get("vrf") |
| 165 |
display_name = module.params.get("display_name") |
| 166 |
layer3_multicast = module.params.get("layer3_multicast") |
| 167 |
vzany = module.params.get("vzany") |
| 168 |
ip_data_plane_learning = module.params.get("ip_data_plane_learning") |
| 169 |
preferred_group = module.params.get("preferred_group") |
| 170 |
site_aware_policy_enforcement = module.params.get("site_aware_policy_enforcement") |
| 171 |
bd_enforcement_status = module.params.get("bd_enforcement_status") |
| 172 |
state = module.params.get("state") |
| 173 |
|
| 174 |
vrfs_path = "/templates/{0}/vrfs".format(template) |
| 175 |
|
| 176 |
mso = MSOModule(module) |
| 177 |
|
| 178 |
mso_schema = MSOSchema(mso, schema, template) |
| 179 |
mso_schema.set_template(template) |
| 180 |
|
| 181 |
if state == "query": |
| 182 |
if vrf: |
| 183 |
mso_schema.set_template_vrf(vrf) |
| 184 |
mso.existing = mso_schema.schema_objects.get("template_vrf").details |
| 185 |
else: |
| 186 |
mso.existing = mso_schema.schema_objects.get("template").details.get("vrfs", []) |
| 187 |
mso.exit_json() |
| 188 |
|
| 189 |
mso_schema.set_template_vrf(vrf, False) |
| 190 |
|
| 191 |
template_vrf = mso_schema.schema_objects.get("template_vrf") |
| 192 |
ops = [] |
| 193 |
|
| 194 |
mso.previous = mso.existing = template_vrf.details if template_vrf else mso.existing |
| 195 |
|
| 196 |
if state == "absent": |
| 197 |
if mso.existing: |
| 198 |
mso.sent = mso.existing = {} |
| 199 |
ops.append(dict(op="remove", path="{0}/{1}".format(vrfs_path, template_vrf.details.get("name")))) |
| 200 |
|
| 201 |
elif state == "present": |
| 202 |
if display_name is None and not mso.existing: |
| 203 |
display_name = vrf |
| 204 |
|
| 205 |
payload = dict( |
| 206 |
name=vrf, |
| 207 |
displayName=display_name, |
| 208 |
l3MCast=layer3_multicast, |
| 209 |
vzAnyEnabled=vzany, |
| 210 |
preferredGroup=preferred_group, |
| 211 |
ipDataPlaneLearning=ip_data_plane_learning, |
| 212 |
) |
| 213 |
|
| 214 |
if site_aware_policy_enforcement is not None: |
| 215 |
payload["siteAwarePolicyEnforcementMode"] = site_aware_policy_enforcement |
| 216 |
if bd_enforcement_status is not None: |
| 217 |
payload["bdEnfStatus"] = bd_enforcement_status |
| 218 |
|
| 219 |
if template_vrf and template_vrf.details.get("vrfRef"): |
| 220 |
# Add vrfRef from the details to ensure idempotency succeeds |
| 221 |
payload["vrfRef"] = template_vrf.details["vrfRef"] |
| 222 |
|
| 223 |
mso.sanitize(payload, collate=True) |
| 224 |
|
| 225 |
if mso.existing: |
| 226 |
vrf_path = "{0}/{1}".format(vrfs_path, template_vrf.details.get("name")) |
| 227 |
if display_name is not None and display_name != mso.existing.get("displayName"): |
| 228 |
ops.append(dict(op="replace", path=vrf_path + "/displayName", value=display_name)) |
| 229 |
if layer3_multicast is not None and layer3_multicast != mso.existing.get("l3MCast"): |
| 230 |
ops.append(dict(op="replace", path=vrf_path + "/l3MCast", value=layer3_multicast)) |
| 231 |
if vzany is not None and vzany != mso.existing.get("vzAnyEnabled"): |
| 232 |
ops.append(dict(op="replace", path=vrf_path + "/vzAnyEnabled", value=vzany)) |
| 233 |
if preferred_group is not None and preferred_group != mso.existing.get("preferredGroup"): |
| 234 |
ops.append(dict(op="replace", path=vrf_path + "/preferredGroup", value=preferred_group)) |
| 235 |
if ip_data_plane_learning is not None and ip_data_plane_learning != mso.existing.get("ipDataPlaneLearning"): |
| 236 |
ops.append(dict(op="replace", path=vrf_path + "/ipDataPlaneLearning", value=ip_data_plane_learning)) |
| 237 |
if site_aware_policy_enforcement is not None and site_aware_policy_enforcement != mso.existing.get("siteAwarePolicyEnforcementMode"): |
| 238 |
ops.append(dict(op="replace", path=vrf_path + "/siteAwarePolicyEnforcementMode", value=site_aware_policy_enforcement)) |
| 239 |
if bd_enforcement_status is not None and bd_enforcement_status != mso.existing.get("bdEnfStatus"): |
| 240 |
ops.append(dict(op="replace", path=vrf_path + "/bdEnfStatus", value=bd_enforcement_status)) |
| 241 |
else: |
| 242 |
ops.append(dict(op="add", path=vrfs_path + "/-", value=mso.sent)) |
| 243 |
|
| 244 |
mso.existing = mso.proposed |
| 245 |
|
| 246 |
if not module.check_mode and ops: |
| 247 |
mso.request(mso_schema.path, method="PATCH", data=ops) |
| 248 |
|
| 249 |
mso.exit_json() |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == "__main__": |
| 253 |
main() |