| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Sabari Jaganathan (@sajagana) <[email protected]> |
| 5 |
|
| 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: ndo_route_map_policy_route_control |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage Route Map Policy for Route Control on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Route Map Policy for Route Control on Cisco Nexus Dashboard Orchestrator (NDO). |
| 21 |
- This module is only supported on ND v4.1 and later. |
| 22 |
author: |
| 23 |
- Sabari Jaganathan (@sajagana) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a tenant template. |
| 29 |
- This parameter or O(template_id) is required. |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- The ID of the tenant template. |
| 34 |
- This parameter or O(template) is required. |
| 35 |
type: str |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name of the Route Map Policy for Route Control. |
| 39 |
type: str |
| 40 |
uuid: |
| 41 |
description: |
| 42 |
- The UUID of the Route Map Policy for Route Control. |
| 43 |
- This parameter is required when the O(name) needs to be updated. |
| 44 |
type: str |
| 45 |
description: |
| 46 |
description: |
| 47 |
- The description of the Route Map Policy for Route Control. |
| 48 |
type: str |
| 49 |
match_type: |
| 50 |
description: |
| 51 |
- The match type of the Route Map Policy for Route Control. |
| 52 |
- Defaults to C(routing_policy_only) when unset during creation. |
| 53 |
type: str |
| 54 |
choices: [ prefix_and_routing_policy, routing_policy_only ] |
| 55 |
state: |
| 56 |
description: |
| 57 |
- Use C(absent) for removing. |
| 58 |
- Use C(query) for listing an object or multiple objects. |
| 59 |
- Use C(present) for creating or updating. |
| 60 |
type: str |
| 61 |
choices: [ absent, query, present ] |
| 62 |
default: query |
| 63 |
notes: |
| 64 |
- The O(template) must exist before using this module in your playbook. |
| 65 |
Use M(cisco.mso.ndo_template) to create the Tenant template. |
| 66 |
seealso: |
| 67 |
- module: cisco.mso.ndo_template |
| 68 |
extends_documentation_fragment: cisco.mso.modules |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Add Route Map Policy for Route Control |
| 73 |
cisco.mso.ndo_route_map_policy_route_control: |
| 74 |
host: mso_host |
| 75 |
username: admin |
| 76 |
password: SomeSecretPassword |
| 77 |
template: ansible_tenant_template |
| 78 |
name: route_map_policy_1 |
| 79 |
state: present |
| 80 |
register: add_route_map_policy |
| 81 |
|
| 82 |
- name: Update Route Map Policy for Route Control name using UUID |
| 83 |
cisco.mso.ndo_route_map_policy_route_control: |
| 84 |
host: mso_host |
| 85 |
username: admin |
| 86 |
password: SomeSecretPassword |
| 87 |
template: ansible_tenant_template |
| 88 |
name: route_map_policy_1_updated |
| 89 |
uuid: "{{ add_route_map_policy.current.uuid }}" |
| 90 |
match_type: prefix_and_routing_policy |
| 91 |
state: present |
| 92 |
|
| 93 |
- name: Query Route Map Policy for Route Control using name |
| 94 |
cisco.mso.ndo_route_map_policy_route_control: |
| 95 |
host: mso_host |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
template: ansible_tenant_template |
| 99 |
name: route_map_policy_1_updated |
| 100 |
state: query |
| 101 |
register: query_one_with_name |
| 102 |
|
| 103 |
- name: Query Route Map Policy for Route Control using UUID |
| 104 |
cisco.mso.ndo_route_map_policy_route_control: |
| 105 |
host: mso_host |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
template: ansible_tenant_template |
| 109 |
uuid: "{{ add_route_map_policy.current.uuid }}" |
| 110 |
state: query |
| 111 |
register: query_one_with_uuid |
| 112 |
|
| 113 |
- name: Query all Route Map Policy for Route Controls |
| 114 |
cisco.mso.ndo_route_map_policy_route_control: |
| 115 |
host: mso_host |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
template: ansible_tenant_template |
| 119 |
state: query |
| 120 |
register: query_all |
| 121 |
|
| 122 |
- name: Remove Route Map Policy for Route Control using name |
| 123 |
cisco.mso.ndo_route_map_policy_route_control: |
| 124 |
host: mso_host |
| 125 |
username: admin |
| 126 |
password: SomeSecretPassword |
| 127 |
template: ansible_tenant_template |
| 128 |
name: route_map_policy_1_updated |
| 129 |
state: absent |
| 130 |
|
| 131 |
- name: Remove Route Map Policy for Route Control using UUID |
| 132 |
cisco.mso.ndo_route_map_policy_route_control: |
| 133 |
host: mso_host |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
template: ansible_tenant_template |
| 137 |
uuid: "{{ add_route_map_policy.current.uuid }}" |
| 138 |
state: absent |
| 139 |
""" |
| 140 |
|
| 141 |
RETURN = r""" |
| 142 |
""" |
| 143 |
|
| 144 |
|
| 145 |
import copy |
| 146 |
from ansible.module_utils.basic import AnsibleModule |
| 147 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import ROUTE_MAP_POLICY_MATCH_TYPE |
| 148 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 149 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 150 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 151 |
|
| 152 |
|
| 153 |
def main(): |
| 154 |
argument_spec = mso_argument_spec() |
| 155 |
argument_spec.update( |
| 156 |
template=dict(type="str"), |
| 157 |
template_id=dict(type="str"), |
| 158 |
name=dict(type="str"), |
| 159 |
uuid=dict(type="str"), |
| 160 |
description=dict(type="str"), |
| 161 |
match_type=dict(type="str", choices=list(ROUTE_MAP_POLICY_MATCH_TYPE)), |
| 162 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 163 |
) |
| 164 |
|
| 165 |
module = AnsibleModule( |
| 166 |
argument_spec=argument_spec, |
| 167 |
supports_check_mode=True, |
| 168 |
required_if=[ |
| 169 |
["state", "absent", ["name", "uuid"], True], |
| 170 |
["state", "present", ["name", "uuid"], True], |
| 171 |
], |
| 172 |
required_one_of=[ |
| 173 |
["template", "template_id"], |
| 174 |
], |
| 175 |
) |
| 176 |
|
| 177 |
mso = MSOModule(module) |
| 178 |
mso_templates = MSOTemplates(mso) |
| 179 |
|
| 180 |
template_name = mso.params.get("template") |
| 181 |
template_id = mso.params.get("template_id") |
| 182 |
name = mso.params.get("name") |
| 183 |
uuid = mso.params.get("uuid") |
| 184 |
description = mso.params.get("description") |
| 185 |
match_type = ROUTE_MAP_POLICY_MATCH_TYPE.get(mso.params.get("match_type")) |
| 186 |
state = mso.params.get("state") |
| 187 |
|
| 188 |
ops = [] |
| 189 |
match = None |
| 190 |
path = None |
| 191 |
|
| 192 |
mso_template = mso_templates.get_template("tenant", template_name, template_id) |
| 193 |
mso_template.validate_template("tenantPolicy") |
| 194 |
|
| 195 |
match = mso_template.get_route_map_policy(uuid, name) |
| 196 |
if (uuid or name) and match: # Query a specific object |
| 197 |
mso.existing = mso.previous = copy.deepcopy(mso_template.update_config_with_template_and_references(match.details)) |
| 198 |
elif match: # Query all objects |
| 199 |
mso.existing = [mso_template.update_config_with_template_and_references(obj) for obj in match] |
| 200 |
|
| 201 |
if state != "query": |
| 202 |
path = "/tenantPolicyTemplate/template/routeMapPolicies/{0}".format(match.index if match else "-") |
| 203 |
|
| 204 |
if state == "present": |
| 205 |
mso_values = { |
| 206 |
"name": name, |
| 207 |
"description": description, |
| 208 |
"routeMapType": match_type, |
| 209 |
} |
| 210 |
if match: |
| 211 |
append_update_ops_data(ops, match.details, path, mso_values) |
| 212 |
mso.sanitize(mso_values, collate=True) |
| 213 |
else: |
| 214 |
mso.sanitize(mso_values) |
| 215 |
ops.append(dict(op="add", path=path, value=mso.sent)) |
| 216 |
|
| 217 |
elif state == "absent" and match: |
| 218 |
ops.append(dict(op="remove", path=path)) |
| 219 |
|
| 220 |
if mso.proposed: |
| 221 |
mso.proposed = copy.deepcopy(mso.proposed) |
| 222 |
mso_template.update_config_with_template_and_references(mso.proposed) |
| 223 |
|
| 224 |
if not module.check_mode and ops: |
| 225 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 226 |
match = mso_template.get_route_map_policy(uuid, name, template_object=response) |
| 227 |
if match: |
| 228 |
mso.existing = mso_template.update_config_with_template_and_references(match.details) # When the state is present |
| 229 |
else: |
| 230 |
mso.existing = {} # When the state is absent |
| 231 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 232 |
mso.existing = mso.proposed if state == "present" else {} |
| 233 |
|
| 234 |
mso.exit_json() |
| 235 |
|
| 236 |
|
| 237 |
if __name__ == "__main__": |
| 238 |
main() |