| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Gaspard Micol (@gmicol) <[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_match_rule_prefix |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage Match Prefix List on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Match Prefix List inside a Match Rule Policy on Cisco Nexus Dashboard Orchestrator (NDO). |
| 21 |
- This module is only supported on ND v4.1 (NDO v5.1) and later. |
| 22 |
author: |
| 23 |
- Gaspard Micol (@gmicol) |
| 24 |
options: |
| 25 |
template: |
| 26 |
description: |
| 27 |
- The name of the template. |
| 28 |
- The template must be a Tenant Policy template. |
| 29 |
- This parameter or O(template_id) is required. |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- The ID of the template. |
| 34 |
- The template must be a Tenant Policy template. |
| 35 |
- This parameter or O(template) is required. |
| 36 |
type: str |
| 37 |
match_rule_policy: |
| 38 |
description: |
| 39 |
- The name of the Match Rule Policy. |
| 40 |
- This parameter or O(match_rule_policy_uuid) is required. |
| 41 |
type: str |
| 42 |
match_rule_policy_uuid: |
| 43 |
description: |
| 44 |
- The UUID of the Match Rule Policy. |
| 45 |
- This parameter or O(match_rule_policy) is required. |
| 46 |
type: str |
| 47 |
prefix: |
| 48 |
description: |
| 49 |
- The Prefix IP to match. |
| 50 |
type: str |
| 51 |
description: |
| 52 |
description: |
| 53 |
- The description of the Match Prefix. |
| 54 |
type: str |
| 55 |
aggregate: |
| 56 |
description: |
| 57 |
- The aggregate flag enabling route aggregation. |
| 58 |
- Defaults to C(false) when unset during creation. |
| 59 |
type: bool |
| 60 |
from_prefix: |
| 61 |
description: |
| 62 |
- The subnet value from which to aggregate. |
| 63 |
- The value must be below O(to_prefix) and above the subnet of the Prefix IP. |
| 64 |
- The value must be between 0 and 32. |
| 65 |
- Defaults to C(0) when unset during creation. |
| 66 |
type: int |
| 67 |
aliases: [ from ] |
| 68 |
to_prefix: |
| 69 |
description: |
| 70 |
- The subnet value to which to aggregate. |
| 71 |
- The value must be above O(from_prefix) and above the subnet of the Prefix IP. |
| 72 |
- The value must be between 0 and 32. |
| 73 |
- Defaults to C(0) when unset during creation. |
| 74 |
type: int |
| 75 |
aliases: [ to ] |
| 76 |
state: |
| 77 |
description: |
| 78 |
- Use C(absent) for removing. |
| 79 |
- Use C(query) for listing an object or multiple objects. |
| 80 |
- Use C(present) for creating or updating. |
| 81 |
type: str |
| 82 |
choices: [ absent, query, present ] |
| 83 |
default: query |
| 84 |
notes: |
| 85 |
- The O(template) must exist before using this module in your playbook. |
| 86 |
Use M(cisco.mso.ndo_template) to create the Tenant Policy template. |
| 87 |
- The O(match_rule_policy) must exist before using this module in your playbook. |
| 88 |
Use M(cisco.mso.ndo_match_rule_policy) to create the Match Rule Policy. |
| 89 |
seealso: |
| 90 |
- module: cisco.mso.ndo_template |
| 91 |
- module: cisco.mso.ndo_match_rule_policy |
| 92 |
extends_documentation_fragment: cisco.mso.modules |
| 93 |
""" |
| 94 |
|
| 95 |
EXAMPLES = r""" |
| 96 |
- name: Create a Match Prefix |
| 97 |
cisco.mso.ndo_match_rule_prefix: |
| 98 |
host: mso_host |
| 99 |
username: admin |
| 100 |
password: SomeSecretPassword |
| 101 |
template: ansible_test |
| 102 |
match_rule_policy: ansible_match_rule_policy |
| 103 |
prefix: 10.10.10.1/24 |
| 104 |
description: Ansible Match Prefix |
| 105 |
aggregate: false |
| 106 |
from_prefix: 0 |
| 107 |
to_prefix: 0 |
| 108 |
state: present |
| 109 |
register: create_match_prefix |
| 110 |
|
| 111 |
- name: Update a Match Prefix using Match Rule Policy's name |
| 112 |
cisco.mso.ndo_match_rule_prefix: |
| 113 |
host: mso_host |
| 114 |
username: admin |
| 115 |
password: SomeSecretPassword |
| 116 |
template: ansible_test |
| 117 |
match_rule_policy: ansible_match_rule_policy |
| 118 |
prefix: 10.10.10.1/24 |
| 119 |
description: Updated Ansible Match Prefix |
| 120 |
aggregate: true |
| 121 |
from_prefix: 25 |
| 122 |
to_prefix: 31 |
| 123 |
state: present |
| 124 |
|
| 125 |
- name: Update a Match Prefix using Match Rule Policy's UUID |
| 126 |
cisco.mso.ndo_match_rule_prefix: |
| 127 |
host: mso_host |
| 128 |
username: admin |
| 129 |
password: SomeSecretPassword |
| 130 |
template: ansible_test |
| 131 |
match_rule_policy_uuid: "{{ create_match_prefix.current.matchRulePolicyUuid }}" |
| 132 |
prefix: 10.10.10.1/24 |
| 133 |
description: Updated Ansible Match Prefix |
| 134 |
aggregate: true |
| 135 |
from_prefix: 25 |
| 136 |
to_prefix: 31 |
| 137 |
state: present |
| 138 |
|
| 139 |
- name: Query a Match Prefix using Match Rule Policy's name |
| 140 |
cisco.mso.ndo_match_rule_prefix: |
| 141 |
host: mso_host |
| 142 |
username: admin |
| 143 |
password: SomeSecretPassword |
| 144 |
template: ansible_test |
| 145 |
match_rule_policy: ansible_match_rule_policy |
| 146 |
prefix: 10.10.10.1/24 |
| 147 |
state: query |
| 148 |
register: query_with_name |
| 149 |
|
| 150 |
- name: Query a Match Prefix using Match Rule Policy's UUID |
| 151 |
cisco.mso.ndo_match_rule_prefix: |
| 152 |
host: mso_host |
| 153 |
username: admin |
| 154 |
password: SomeSecretPassword |
| 155 |
template: ansible_test |
| 156 |
match_rule_policy_uuid: "{{ create_match_prefix.current.matchRulePolicyUuid }}" |
| 157 |
prefix: 10.10.10.1/24 |
| 158 |
state: query |
| 159 |
register: query_with_uuid |
| 160 |
|
| 161 |
- name: Query all Match Prefixes in a Match Rule Policy |
| 162 |
cisco.mso.ndo_match_rule_prefix: |
| 163 |
host: mso_host |
| 164 |
username: admin |
| 165 |
password: SomeSecretPassword |
| 166 |
template: ansible_test |
| 167 |
match_rule_policy: ansible_match_rule_policy |
| 168 |
state: query |
| 169 |
register: query_all_objects |
| 170 |
|
| 171 |
- name: Delete a Match Prefix using Match Rule Policy's name |
| 172 |
cisco.mso.ndo_match_rule_prefix: |
| 173 |
host: mso_host |
| 174 |
username: admin |
| 175 |
password: SomeSecretPassword |
| 176 |
template: ansible_test |
| 177 |
match_rule_policy: ansible_match_rule_policy |
| 178 |
prefix: 10.10.10.1/24 |
| 179 |
state: absent |
| 180 |
|
| 181 |
- name: Delete a Match Prefix using Match Rule Policy's UUID |
| 182 |
cisco.mso.ndo_match_rule_prefix: |
| 183 |
host: mso_host |
| 184 |
username: admin |
| 185 |
password: SomeSecretPassword |
| 186 |
template_id: ansible_test |
| 187 |
match_rule_policy_uuid: "{{ create_match_prefix.current.matchRulePolicyUuid }}" |
| 188 |
prefix: 10.10.10.1/24 |
| 189 |
state: absent |
| 190 |
""" |
| 191 |
|
| 192 |
RETURN = r""" |
| 193 |
""" |
| 194 |
|
| 195 |
from ansible.module_utils.basic import AnsibleModule |
| 196 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 197 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 198 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 199 |
import copy |
| 200 |
|
| 201 |
|
| 202 |
def main(): |
| 203 |
argument_spec = mso_argument_spec() |
| 204 |
argument_spec.update( |
| 205 |
template=dict(type="str"), |
| 206 |
template_id=dict(type="str"), |
| 207 |
match_rule_policy=dict(type="str"), |
| 208 |
match_rule_policy_uuid=dict(type="str"), |
| 209 |
prefix=dict(type="str"), |
| 210 |
description=dict(type="str"), |
| 211 |
aggregate=dict(type="bool"), |
| 212 |
from_prefix=dict(type="int", aliases=["from"]), |
| 213 |
to_prefix=dict(type="int", aliases=["to"]), |
| 214 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 215 |
) |
| 216 |
|
| 217 |
module = AnsibleModule( |
| 218 |
argument_spec=argument_spec, |
| 219 |
supports_check_mode=True, |
| 220 |
mutually_exclusive=[ |
| 221 |
("template", "template_id"), |
| 222 |
["match_rule_policy", "match_rule_policy_uuid"], |
| 223 |
], |
| 224 |
required_if=[ |
| 225 |
["state", "absent", ["prefix"]], |
| 226 |
["state", "present", ["prefix"]], |
| 227 |
], |
| 228 |
required_one_of=[ |
| 229 |
["template", "template_id"], |
| 230 |
["match_rule_policy", "match_rule_policy_uuid"], |
| 231 |
], |
| 232 |
) |
| 233 |
|
| 234 |
mso = MSOModule(module) |
| 235 |
mso_templates = MSOTemplates(mso) |
| 236 |
|
| 237 |
template_name = module.params.get("template") |
| 238 |
template_id = module.params.get("template_id") |
| 239 |
match_rule_policy = module.params.get("match_rule_policy") |
| 240 |
match_rule_policy_uuid = module.params.get("match_rule_policy_uuid") |
| 241 |
prefix = module.params.get("prefix") |
| 242 |
description = module.params.get("description") |
| 243 |
aggregate = module.params.get("aggregate") |
| 244 |
from_prefix = module.params.get("from_prefix") |
| 245 |
to_prefix = module.params.get("to_prefix") |
| 246 |
state = module.params.get("state") |
| 247 |
|
| 248 |
mso_template = mso_templates.get_template("tenant", template_name, template_id) |
| 249 |
mso_template.validate_template("tenantPolicy") |
| 250 |
|
| 251 |
match_rule_policy_object = mso_template.get_match_rule_policy_object(match_rule_policy_uuid, match_rule_policy) |
| 252 |
if not match_rule_policy_object: |
| 253 |
mso.fail_json( |
| 254 |
msg="The Match Rule Policy with the following {0[0]} '{0[1]}' does not exist".format( |
| 255 |
("UUID", match_rule_policy_uuid) if match_rule_policy_uuid else ("name", match_rule_policy) |
| 256 |
) |
| 257 |
) |
| 258 |
match_identifiers = {"prefix": prefix} |
| 259 |
match = mso_template.get_direct_child_object(match_rule_policy_object, "Match Prefix", "matchPrefixList", match_identifiers) |
| 260 |
|
| 261 |
if prefix and match: |
| 262 |
mso.existing = mso.previous = copy.deepcopy( |
| 263 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, match.details) |
| 264 |
) # Query a specific object |
| 265 |
elif match: |
| 266 |
mso.existing = [ |
| 267 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, obj) for obj in match |
| 268 |
] # Query all objects |
| 269 |
|
| 270 |
match_prefix_path = "/tenantPolicyTemplate/template/matchRulePolicies/{0}/matchPrefixList/{1}".format( |
| 271 |
match_rule_policy_object.index, |
| 272 |
match.index if match else "-", |
| 273 |
) |
| 274 |
|
| 275 |
ops = [] |
| 276 |
|
| 277 |
if state == "present": |
| 278 |
|
| 279 |
mso_values = { |
| 280 |
"prefix": prefix, |
| 281 |
"aggregate": aggregate, |
| 282 |
"description": description, |
| 283 |
"fromPfxLen": from_prefix, |
| 284 |
"toPfxLen": to_prefix, |
| 285 |
} |
| 286 |
|
| 287 |
if match: |
| 288 |
append_update_ops_data(ops, match.details, match_prefix_path, mso_values) |
| 289 |
mso.sanitize(mso_values, collate=True) |
| 290 |
else: |
| 291 |
mso.sanitize(mso_values) |
| 292 |
ops.append(dict(op="add", path=match_prefix_path, value=mso.sent)) |
| 293 |
|
| 294 |
elif state == "absent": |
| 295 |
if match: |
| 296 |
ops.append(dict(op="remove", path=match_prefix_path)) |
| 297 |
|
| 298 |
if not module.check_mode and ops: |
| 299 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 300 |
match_rule_policy_object = mso_template.get_match_rule_policy_object(match_rule_policy_uuid, match_rule_policy, search_object=response) |
| 301 |
match = mso_template.get_direct_child_object(match_rule_policy_object, "Match Prefix", "matchPrefixList", match_identifiers) |
| 302 |
if match: |
| 303 |
mso.existing = mso_template.update_match_rule_policy_child_object_with_template_and_parent( |
| 304 |
match_rule_policy_object.details, match.details |
| 305 |
) # When the state is present |
| 306 |
else: |
| 307 |
mso.existing = {} # When the state is absent |
| 308 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 309 |
if mso.proposed: |
| 310 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, mso.proposed) |
| 311 |
mso.existing = mso.proposed if state == "present" else {} |
| 312 |
mso.exit_json() |
| 313 |
|
| 314 |
|
| 315 |
if __name__ == "__main__": |
| 316 |
main() |