| 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_community_term |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage Match Community Terms on Cisco Nexus Dashboard Orchestrator (NDO). |
| 19 |
description: |
| 20 |
- Manage Match Community Terms 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 |
name: |
| 48 |
description: |
| 49 |
- The name of the Match Community Term. |
| 50 |
type: str |
| 51 |
aliases: [ match_community_term ] |
| 52 |
description: |
| 53 |
description: |
| 54 |
- The description of the Match Community Term. |
| 55 |
type: str |
| 56 |
aliases: [ match_community_term_description ] |
| 57 |
match_community_factors: |
| 58 |
description: |
| 59 |
- The list of Match Community Factors. |
| 60 |
type: list |
| 61 |
elements: dict |
| 62 |
suboptions: |
| 63 |
community_factor: |
| 64 |
description: |
| 65 |
- The Community Factor to match. |
| 66 |
- e.g., regular:as2-nn2:4:15, extended:as4-nn2:5:16, |
| 67 |
extended:color:35, no-export, no-advertise, etc. |
| 68 |
type: str |
| 69 |
aliases: [ community ] |
| 70 |
scope: |
| 71 |
description: |
| 72 |
- The scope of the Match Community Factor. |
| 73 |
- Defaults to C(transitive) when unset during creation. |
| 74 |
type: str |
| 75 |
choices: [ transitive, non_transitive ] |
| 76 |
description: |
| 77 |
description: |
| 78 |
- The description of the Match Community Factor. |
| 79 |
type: str |
| 80 |
aliases: [ match_community_description ] |
| 81 |
state: |
| 82 |
description: |
| 83 |
- Use C(absent) for removing. |
| 84 |
- Use C(query) for listing an object or multiple objects. |
| 85 |
- Use C(present) for creating or updating. |
| 86 |
type: str |
| 87 |
choices: [ absent, query, present ] |
| 88 |
default: query |
| 89 |
notes: |
| 90 |
- The O(template) must exist before using this module in your playbook. |
| 91 |
Use M(cisco.mso.ndo_template) to create the Tenant Policy template. |
| 92 |
- The O(match_rule_policy) must exist before using this module in your playbook. |
| 93 |
Use M(cisco.mso.ndo_match_rule_policy) to create the Match Rule Policy. |
| 94 |
seealso: |
| 95 |
- module: cisco.mso.ndo_template |
| 96 |
- module: cisco.mso.ndo_match_rule_policy |
| 97 |
extends_documentation_fragment: cisco.mso.modules |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = r""" |
| 101 |
- name: Create a Match Community Term |
| 102 |
cisco.mso.ndo_match_rule_community_term: |
| 103 |
host: mso_host |
| 104 |
username: admin |
| 105 |
password: SomeSecretPassword |
| 106 |
template: ansible_test |
| 107 |
match_rule_policy: ansible_match_rule_policy |
| 108 |
name: ansible_match_community_term |
| 109 |
description: Ansible Match Community Term |
| 110 |
match_community_factors: |
| 111 |
- community_factor: no-export |
| 112 |
scope: transitive |
| 113 |
description: no export and transitive |
| 114 |
state: present |
| 115 |
register: create_match_name |
| 116 |
|
| 117 |
- name: Update a Match Community Term using Match Rule Policy's name |
| 118 |
cisco.mso.ndo_match_rule_community_term: |
| 119 |
host: mso_host |
| 120 |
username: admin |
| 121 |
password: SomeSecretPassword |
| 122 |
template: ansible_test |
| 123 |
match_rule_policy: ansible_match_rule_policy |
| 124 |
name: ansible_match_community_term |
| 125 |
description: Updated Ansible Match Community Term |
| 126 |
match_community_factors: |
| 127 |
- community_factor: no-export |
| 128 |
scope: non_transitive |
| 129 |
description: no export and non-transitive |
| 130 |
- community_factor: no-advertise |
| 131 |
scope: transitive |
| 132 |
description: no advertise and transitive |
| 133 |
state: present |
| 134 |
|
| 135 |
- name: Update a Match Community Term using Match Rule Policy's UUID |
| 136 |
cisco.mso.ndo_match_rule_community_term: |
| 137 |
host: mso_host |
| 138 |
username: admin |
| 139 |
password: SomeSecretPassword |
| 140 |
template: ansible_test |
| 141 |
match_rule_policy_uuid: "{{ create_match_name.current.matchRulePolicyUuid }}" |
| 142 |
name: ansible_match_community_term |
| 143 |
description: Updated Ansible Match Community Term |
| 144 |
match_community_factors: |
| 145 |
- community_factor: no-export |
| 146 |
scope: non_transitive |
| 147 |
description: no export and non-transitive |
| 148 |
- community_factor: no-advertise |
| 149 |
scope: transitive |
| 150 |
description: no advertise and transitive |
| 151 |
state: present |
| 152 |
|
| 153 |
- name: Update a Match Community Term by removing a Match Community |
| 154 |
cisco.mso.ndo_match_rule_community_term: |
| 155 |
host: mso_host |
| 156 |
username: admin |
| 157 |
password: SomeSecretPassword |
| 158 |
template: ansible_test |
| 159 |
match_rule_policy: ansible_match_rule_policy |
| 160 |
name: ansible_match_community_term |
| 161 |
description: Updated Ansible Match Community Term |
| 162 |
match_community_factors: |
| 163 |
- community_factor: no-advertise |
| 164 |
scope: transitive |
| 165 |
description: no advertise and transitive |
| 166 |
state: present |
| 167 |
|
| 168 |
- name: Update a Match Community Term by removing all Match Communities |
| 169 |
cisco.mso.ndo_match_rule_community_term: |
| 170 |
host: mso_host |
| 171 |
username: admin |
| 172 |
password: SomeSecretPassword |
| 173 |
template: ansible_test |
| 174 |
match_rule_policy: ansible_match_rule_policy |
| 175 |
name: ansible_match_community_term |
| 176 |
description: Updated Ansible Match Community Term |
| 177 |
match_community_factors: [] |
| 178 |
state: present |
| 179 |
|
| 180 |
- name: Query a Match Community Term using Match Rule Policy's name |
| 181 |
cisco.mso.ndo_match_rule_community_term: |
| 182 |
host: mso_host |
| 183 |
username: admin |
| 184 |
password: SomeSecretPassword |
| 185 |
template: ansible_test |
| 186 |
match_rule_policy: ansible_match_rule_policy |
| 187 |
name: ansible_match_community_term |
| 188 |
state: query |
| 189 |
register: query_with_name |
| 190 |
|
| 191 |
- name: Query a Match Community Term using Match Rule Policy's UUID |
| 192 |
cisco.mso.ndo_match_rule_community_term: |
| 193 |
host: mso_host |
| 194 |
username: admin |
| 195 |
password: SomeSecretPassword |
| 196 |
template: ansible_test |
| 197 |
match_rule_policy_uuid: "{{ create_match_name.current.matchRulePolicyUuid }}" |
| 198 |
name: ansible_match_community_term |
| 199 |
state: query |
| 200 |
register: query_with_uuid |
| 201 |
|
| 202 |
- name: Query all Match Community Terms in a Match Rule Policy |
| 203 |
cisco.mso.ndo_match_rule_community_term: |
| 204 |
host: mso_host |
| 205 |
username: admin |
| 206 |
password: SomeSecretPassword |
| 207 |
template: ansible_test |
| 208 |
match_rule_policy: ansible_match_rule_policy |
| 209 |
state: query |
| 210 |
register: query_all_objects |
| 211 |
|
| 212 |
- name: Delete a Match Community Term using Match Rule Policy's name |
| 213 |
cisco.mso.ndo_match_rule_community_term: |
| 214 |
host: mso_host |
| 215 |
username: admin |
| 216 |
password: SomeSecretPassword |
| 217 |
template: ansible_test |
| 218 |
match_rule_policy: ansible_match_rule_policy |
| 219 |
name: ansible_match_community_term |
| 220 |
state: absent |
| 221 |
|
| 222 |
- name: Delete a Match Community Term using Match Rule Policy's UUID |
| 223 |
cisco.mso.ndo_match_rule_community_term: |
| 224 |
host: mso_host |
| 225 |
username: admin |
| 226 |
password: SomeSecretPassword |
| 227 |
template_id: ansible_test |
| 228 |
match_rule_policy_uuid: "{{ create_match_name.current.matchRulePolicyUuid }}" |
| 229 |
name: ansible_match_community_term |
| 230 |
state: absent |
| 231 |
""" |
| 232 |
|
| 233 |
RETURN = r""" |
| 234 |
""" |
| 235 |
|
| 236 |
from ansible.module_utils.basic import AnsibleModule |
| 237 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 238 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 239 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import MATCH_COMMUNITY_SCOPE_MAP |
| 240 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 241 |
import copy |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
argument_spec = mso_argument_spec() |
| 246 |
argument_spec.update( |
| 247 |
template=dict(type="str"), |
| 248 |
template_id=dict(type="str"), |
| 249 |
match_rule_policy=dict(type="str"), |
| 250 |
match_rule_policy_uuid=dict(type="str"), |
| 251 |
name=dict(type="str", aliases=["match_community_term"]), |
| 252 |
description=dict(type="str", aliases=["match_community_term_description"]), |
| 253 |
match_community_factors=dict( |
| 254 |
type="list", |
| 255 |
elements="dict", |
| 256 |
options=dict( |
| 257 |
community_factor=dict(type="str", aliases=["community"]), |
| 258 |
scope=dict(type="str", choices=list(MATCH_COMMUNITY_SCOPE_MAP)), |
| 259 |
description=dict(type="str", aliases=["match_community_description"]), |
| 260 |
), |
| 261 |
), |
| 262 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 263 |
) |
| 264 |
|
| 265 |
module = AnsibleModule( |
| 266 |
argument_spec=argument_spec, |
| 267 |
supports_check_mode=True, |
| 268 |
mutually_exclusive=[ |
| 269 |
("template", "template_id"), |
| 270 |
["match_rule_policy", "match_rule_policy_uuid"], |
| 271 |
], |
| 272 |
required_if=[ |
| 273 |
["state", "absent", ["name"]], |
| 274 |
["state", "present", ["name"]], |
| 275 |
], |
| 276 |
required_one_of=[ |
| 277 |
["template", "template_id"], |
| 278 |
["match_rule_policy", "match_rule_policy_uuid"], |
| 279 |
], |
| 280 |
) |
| 281 |
|
| 282 |
mso = MSOModule(module) |
| 283 |
mso_templates = MSOTemplates(mso) |
| 284 |
|
| 285 |
template_name = module.params.get("template") |
| 286 |
template_id = module.params.get("template_id") |
| 287 |
match_rule_policy = module.params.get("match_rule_policy") |
| 288 |
match_rule_policy_uuid = module.params.get("match_rule_policy_uuid") |
| 289 |
name = module.params.get("name") |
| 290 |
description = module.params.get("description") |
| 291 |
match_community_factors = module.params.get("match_community_factors") |
| 292 |
if not isinstance(match_community_factors, list): |
| 293 |
match_community_factors = [] |
| 294 |
from_name = module.params.get("from_name") |
| 295 |
to_name = module.params.get("to_name") |
| 296 |
state = module.params.get("state") |
| 297 |
|
| 298 |
mso_template = mso_templates.get_template("tenant", template_name, template_id) |
| 299 |
mso_template.validate_template("tenantPolicy") |
| 300 |
|
| 301 |
match_rule_policy_object = mso_template.get_match_rule_policy_object(match_rule_policy_uuid, match_rule_policy) |
| 302 |
if not match_rule_policy_object: |
| 303 |
mso.fail_json( |
| 304 |
msg="The Match Rule Policy with the following {0[0]} '{0[1]}' does not exist".format( |
| 305 |
("UUID", match_rule_policy_uuid) if match_rule_policy_uuid else ("name", match_rule_policy) |
| 306 |
) |
| 307 |
) |
| 308 |
match_identifiers = {"name": name} |
| 309 |
match = mso_template.get_direct_child_object(match_rule_policy_object, "Match Community Term", "matchCommunityTermsList", match_identifiers) |
| 310 |
|
| 311 |
if name and match: |
| 312 |
mso.existing = mso.previous = copy.deepcopy( |
| 313 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, match.details) |
| 314 |
) # Query a specific object |
| 315 |
elif match: |
| 316 |
mso.existing = [ |
| 317 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, obj) for obj in match |
| 318 |
] # Query all objects |
| 319 |
|
| 320 |
match_name_path = "/tenantPolicyTemplate/template/matchRulePolicies/{0}/matchCommunityTermsList/{1}".format( |
| 321 |
match_rule_policy_object.index, |
| 322 |
match.index if match else "-", |
| 323 |
) |
| 324 |
|
| 325 |
ops = [] |
| 326 |
|
| 327 |
if state == "present": |
| 328 |
|
| 329 |
mso_values = { |
| 330 |
"name": name, |
| 331 |
"matchCommunityList": [ |
| 332 |
{ |
| 333 |
"community": match_community.get("community_factor"), |
| 334 |
"scope": MATCH_COMMUNITY_SCOPE_MAP.get(match_community.get("scope")), |
| 335 |
"description": match_community.get("description"), |
| 336 |
} |
| 337 |
for match_community in match_community_factors |
| 338 |
], |
| 339 |
"description": description, |
| 340 |
"fromPfxLen": from_name, |
| 341 |
"toPfxLen": to_name, |
| 342 |
} |
| 343 |
|
| 344 |
if match: |
| 345 |
append_update_ops_data(ops, match.details, match_name_path, mso_values) |
| 346 |
mso.sanitize(mso_values, collate=True) |
| 347 |
else: |
| 348 |
mso.sanitize(mso_values) |
| 349 |
ops.append(dict(op="add", path=match_name_path, value=mso.sent)) |
| 350 |
|
| 351 |
elif state == "absent": |
| 352 |
if match: |
| 353 |
ops.append(dict(op="remove", path=match_name_path)) |
| 354 |
|
| 355 |
if not module.check_mode and ops: |
| 356 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 357 |
match_rule_policy_object = mso_template.get_match_rule_policy_object(match_rule_policy_uuid, match_rule_policy, search_object=response) |
| 358 |
match = mso_template.get_direct_child_object(match_rule_policy_object, "Match Community Term", "matchCommunityTermsList", match_identifiers) |
| 359 |
if match: |
| 360 |
mso.existing = mso_template.update_match_rule_policy_child_object_with_template_and_parent( |
| 361 |
match_rule_policy_object.details, match.details |
| 362 |
) # When the state is present |
| 363 |
else: |
| 364 |
mso.existing = {} # When the state is absent |
| 365 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 366 |
if mso.proposed: |
| 367 |
mso_template.update_match_rule_policy_child_object_with_template_and_parent(match_rule_policy_object.details, mso.proposed) |
| 368 |
mso.existing = mso.proposed if state == "present" else {} |
| 369 |
mso.exit_json() |
| 370 |
|
| 371 |
|
| 372 |
if __name__ == "__main__": |
| 373 |
main() |