| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, 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_l3out_annotation |
| 17 |
short_description: Manage L3Outs Annotation on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage L3Outs Annotation on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Sabari Jaganathan (@sajagana) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a L3Out template. |
| 28 |
type: str |
| 29 |
aliases: [ l3out_template ] |
| 30 |
required: true |
| 31 |
l3out: |
| 32 |
description: |
| 33 |
- The name of the L3Out. |
| 34 |
type: str |
| 35 |
aliases: [ l3out_name ] |
| 36 |
required: true |
| 37 |
annotation_key: |
| 38 |
description: |
| 39 |
- The key of the Annotation object. |
| 40 |
type: str |
| 41 |
aliases: [ key ] |
| 42 |
annotation_value: |
| 43 |
description: |
| 44 |
- The value of the Annotation object. |
| 45 |
type: str |
| 46 |
aliases: [ value ] |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Use C(absent) for removing. |
| 50 |
- Use C(query) for listing an object or multiple objects. |
| 51 |
- Use C(present) for creating or updating. |
| 52 |
type: str |
| 53 |
choices: [ absent, query, present ] |
| 54 |
default: query |
| 55 |
extends_documentation_fragment: cisco.mso.modules |
| 56 |
notes: |
| 57 |
- The O(template) must exist before using this module in your playbook. |
| 58 |
Use M(cisco.mso.ndo_template) to create the L3Out template. |
| 59 |
- The O(l3out) must exist before using this module in your playbook. |
| 60 |
Use M(cisco.mso.ndo_l3out_template) to create the L3Out. |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: Add an annotation with key and value |
| 65 |
cisco.mso.ndo_l3out_annotation: |
| 66 |
host: mso_host |
| 67 |
username: admin |
| 68 |
password: SomeSecretPassword |
| 69 |
template: ansible_l3out_template |
| 70 |
l3out: "L3OutAnnotation" |
| 71 |
annotation_key: "annotation_key_1" |
| 72 |
annotation_value: "annotation_value_1" |
| 73 |
state: present |
| 74 |
|
| 75 |
- name: Update an annotation with value |
| 76 |
cisco.mso.ndo_l3out_annotation: |
| 77 |
host: mso_host |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
template: ansible_l3out_template |
| 81 |
l3out: "L3OutAnnotation" |
| 82 |
annotation_key: "annotation_key_1_updated" |
| 83 |
annotation_value: "annotation_value_1" |
| 84 |
state: present |
| 85 |
|
| 86 |
- name: Query a specific annotation with key |
| 87 |
cisco.mso.ndo_l3out_annotation: |
| 88 |
host: mso_host |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
template: ansible_l3out_template |
| 92 |
l3out: "L3OutAnnotation" |
| 93 |
annotation_key: "annotation_key_1_updated" |
| 94 |
state: query |
| 95 |
register: query_one |
| 96 |
|
| 97 |
- name: Query all annotations |
| 98 |
cisco.mso.ndo_l3out_annotation: |
| 99 |
host: mso_host |
| 100 |
username: admin |
| 101 |
password: SomeSecretPassword |
| 102 |
template: ansible_l3out_template |
| 103 |
l3out: "L3OutAnnotation" |
| 104 |
state: query |
| 105 |
register: query_all |
| 106 |
|
| 107 |
- name: Delete an annotation |
| 108 |
cisco.mso.ndo_l3out_annotation: |
| 109 |
host: mso_host |
| 110 |
username: admin |
| 111 |
password: SomeSecretPassword |
| 112 |
template: ansible_l3out_template |
| 113 |
l3out: "L3OutAnnotation" |
| 114 |
annotation_key: "annotation_key_1_updated" |
| 115 |
state: absent |
| 116 |
""" |
| 117 |
|
| 118 |
RETURN = r""" |
| 119 |
""" |
| 120 |
|
| 121 |
|
| 122 |
import copy |
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 125 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 126 |
|
| 127 |
|
| 128 |
def get_annotation_object(mso_template_object, l3out_name, annotation_key): |
| 129 |
l3outs = mso_template_object.template.get("l3outTemplate", {}).get("l3outs", []) |
| 130 |
l3out_object = mso_template_object.get_object_by_key_value_pairs("L3Outs", l3outs, [KVPair("name", l3out_name)], True) |
| 131 |
annotations = l3out_object.details.get("tagAnnotations", []) |
| 132 |
if annotation_key: |
| 133 |
annotation = mso_template_object.get_object_by_key_value_pairs("L3Out Annotations", annotations, [KVPair("tagKey", annotation_key)]) |
| 134 |
return l3out_object, annotation |
| 135 |
|
| 136 |
return l3out_object, annotations |
| 137 |
|
| 138 |
|
| 139 |
def main(): |
| 140 |
argument_spec = mso_argument_spec() |
| 141 |
argument_spec.update( |
| 142 |
template=dict(type="str", required=True, aliases=["l3out_template"]), |
| 143 |
l3out=dict(type="str", required=True, aliases=["l3out_name"]), |
| 144 |
annotation_key=dict(type="str", aliases=["key"], no_log=False), |
| 145 |
annotation_value=dict(type="str", aliases=["value"]), |
| 146 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 147 |
) |
| 148 |
|
| 149 |
module = AnsibleModule( |
| 150 |
argument_spec=argument_spec, |
| 151 |
supports_check_mode=True, |
| 152 |
required_if=[ |
| 153 |
["state", "absent", ["annotation_key"]], |
| 154 |
["state", "present", ["annotation_key", "annotation_value"]], |
| 155 |
], |
| 156 |
) |
| 157 |
|
| 158 |
mso = MSOModule(module) |
| 159 |
|
| 160 |
template = module.params.get("template") |
| 161 |
l3out = module.params.get("l3out") |
| 162 |
annotation_key = module.params.get("annotation_key") |
| 163 |
annotation_value = module.params.get("annotation_value") |
| 164 |
state = module.params.get("state") |
| 165 |
|
| 166 |
l3out_template_object = MSOTemplate(mso, "l3out", template) |
| 167 |
l3out_template_object.validate_template("l3out") |
| 168 |
|
| 169 |
l3out_object, annotation = get_annotation_object(l3out_template_object, l3out, annotation_key) |
| 170 |
|
| 171 |
if annotation_key: |
| 172 |
if annotation is not None: |
| 173 |
mso.existing = mso.previous = copy.deepcopy(annotation.details) # Query a specific Annotation |
| 174 |
else: |
| 175 |
mso.existing = annotation # Query all |
| 176 |
|
| 177 |
path = "/l3outTemplate/l3outs/{0}/tagAnnotations".format(l3out_object.index) |
| 178 |
|
| 179 |
ops = [] |
| 180 |
if state == "present": |
| 181 |
if mso.existing: |
| 182 |
proposed_payload = copy.deepcopy(mso.existing) |
| 183 |
if annotation_value and proposed_payload.get("tagValue") != annotation_value: |
| 184 |
ops.append(dict(op="replace", path="{0}/{1}/tagValue".format(path, annotation.index), value=annotation_value)) |
| 185 |
proposed_payload["tagValue"] = annotation_value |
| 186 |
mso.sanitize(proposed_payload) |
| 187 |
else: |
| 188 |
payload = {"tagKey": annotation_key, "tagValue": annotation_value} |
| 189 |
ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload))) |
| 190 |
mso.sanitize(payload) |
| 191 |
elif state == "absent": |
| 192 |
if mso.existing: |
| 193 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, annotation.index))) |
| 194 |
|
| 195 |
if not module.check_mode and ops: |
| 196 |
l3out_template_object.template = mso.request(l3out_template_object.template_path, method="PATCH", data=ops) |
| 197 |
l3out_object, annotation = get_annotation_object(l3out_template_object, l3out, annotation_key) |
| 198 |
|
| 199 |
if annotation: |
| 200 |
mso.existing = annotation.details # When the state is present |
| 201 |
else: |
| 202 |
mso.existing = {} # When the state is absent |
| 203 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 204 |
mso.existing = mso.proposed if state == "present" else {} |
| 205 |
|
| 206 |
mso.exit_json() |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |