| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Shreyas Srish (@shrsr) <[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_fex_device |
| 17 |
short_description: Manage FEX Devices on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage FEX Devices on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is supported on ND v3.2 (NDO v4.4) and later. |
| 21 |
author: |
| 22 |
- Shreyas Srish (@shrsr) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a fabric resource policy template. |
| 28 |
- This parameter or O(template_id) is required. |
| 29 |
type: str |
| 30 |
template_id: |
| 31 |
description: |
| 32 |
- The ID of the template. |
| 33 |
- The template must be a fabric resource policy template. |
| 34 |
- This parameter or O(template) is required. |
| 35 |
type: str |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name of the FEX Device. |
| 39 |
type: str |
| 40 |
aliases: [ fex_device_name ] |
| 41 |
uuid: |
| 42 |
description: |
| 43 |
- The UUID of the FEX Device. |
| 44 |
- This parameter is required when the O(name) needs to be updated. |
| 45 |
- This parameter can be used instead of O(name) when an existing FEX device is updated or queried. |
| 46 |
type: str |
| 47 |
aliases: [ fex_device_uuid ] |
| 48 |
description: |
| 49 |
description: |
| 50 |
- The description of the FEX Device. |
| 51 |
type: str |
| 52 |
fex_id: |
| 53 |
description: |
| 54 |
- The FEX ID. |
| 55 |
- The value must be in the range 101 - 199. |
| 56 |
- This parameter is required when O(state=present). |
| 57 |
type: int |
| 58 |
aliases: [ fex_device_id ] |
| 59 |
nodes: |
| 60 |
description: |
| 61 |
- The list of node IDs where the FEX Device will be deployed. |
| 62 |
- Each element can either be a single node ID or a range of IDs. |
| 63 |
- This parameter is required when O(state=present). |
| 64 |
type: list |
| 65 |
elements: str |
| 66 |
interfaces: |
| 67 |
description: |
| 68 |
- The interface names connected to the FEX Device. |
| 69 |
- This parameter is required when O(state=present). |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
state: |
| 73 |
description: |
| 74 |
- Use C(absent) for removing. |
| 75 |
- Use C(query) for listing an object or multiple objects. |
| 76 |
- Use C(present) for creating or updating. |
| 77 |
type: str |
| 78 |
choices: [ absent, query, present ] |
| 79 |
default: query |
| 80 |
notes: |
| 81 |
- The O(template) must exist before using this module in your playbook. |
| 82 |
Use M(cisco.mso.ndo_template) to create the Fabric Policy template. |
| 83 |
seealso: |
| 84 |
- module: cisco.mso.ndo_template |
| 85 |
extends_documentation_fragment: cisco.mso.modules |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = r""" |
| 89 |
- name: Create a FEX Device |
| 90 |
cisco.mso.ndo_fex_device: |
| 91 |
host: mso_host |
| 92 |
username: admin |
| 93 |
password: SomeSecretPassword |
| 94 |
template: ansible_test_template |
| 95 |
name: ansible_test_fex_device |
| 96 |
description: "FEX Device for Ansible" |
| 97 |
fex_id: 102 |
| 98 |
nodes: [101, 102, 103-105] |
| 99 |
interfaces: ["1/1"] |
| 100 |
state: present |
| 101 |
register: create_fex_device |
| 102 |
|
| 103 |
- name: Query a FEX Device by name |
| 104 |
cisco.mso.ndo_fex_device: |
| 105 |
host: mso_host |
| 106 |
username: admin |
| 107 |
password: SomeSecretPassword |
| 108 |
template: ansible_test_template |
| 109 |
name: ansible_fex_device |
| 110 |
state: query |
| 111 |
register: query_name |
| 112 |
|
| 113 |
- name: Query a FEX Device by UUID |
| 114 |
cisco.mso.ndo_fex_device: |
| 115 |
host: mso_host |
| 116 |
username: admin |
| 117 |
password: SomeSecretPassword |
| 118 |
template: ansible_test_template |
| 119 |
uuid: '{{ create_fex_device.current.uuid }}' |
| 120 |
state: query |
| 121 |
register: query_uuid |
| 122 |
|
| 123 |
- name: Query all FEX Devices |
| 124 |
cisco.mso.ndo_fex_device: |
| 125 |
host: mso_host |
| 126 |
username: admin |
| 127 |
password: SomeSecretPassword |
| 128 |
template: ansible_test_template |
| 129 |
state: query |
| 130 |
register: query_all |
| 131 |
|
| 132 |
- name: Delete a FEX Device by name |
| 133 |
cisco.mso.ndo_fex_device: |
| 134 |
host: mso_host |
| 135 |
username: admin |
| 136 |
password: SomeSecretPassword |
| 137 |
template: ansible_test_template |
| 138 |
name: ansible_test_fex_device |
| 139 |
state: absent |
| 140 |
|
| 141 |
- name: Delete a FEX Device by UUID |
| 142 |
cisco.mso.ndo_fex_device: |
| 143 |
host: mso_host |
| 144 |
username: admin |
| 145 |
password: SomeSecretPassword |
| 146 |
template: ansible_test_template |
| 147 |
uuid: '{{ create_fex_device.current.uuid }}' |
| 148 |
state: absent |
| 149 |
""" |
| 150 |
|
| 151 |
RETURN = r""" |
| 152 |
""" |
| 153 |
|
| 154 |
from ansible.module_utils.basic import AnsibleModule |
| 155 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import ( |
| 156 |
MSOModule, |
| 157 |
mso_argument_spec, |
| 158 |
) |
| 159 |
from ansible_collections.cisco.mso.plugins.module_utils.template import ( |
| 160 |
MSOTemplate, |
| 161 |
KVPair, |
| 162 |
) |
| 163 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import ( |
| 164 |
append_update_ops_data, |
| 165 |
) |
| 166 |
import copy |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
argument_spec = mso_argument_spec() |
| 171 |
argument_spec.update( |
| 172 |
template=dict(type="str"), |
| 173 |
template_id=dict(type="str"), |
| 174 |
name=dict(type="str", aliases=["fex_device_name"]), |
| 175 |
uuid=dict(type="str", aliases=["fex_device_uuid"]), |
| 176 |
description=dict(type="str"), |
| 177 |
fex_id=dict(type="int", aliases=["fex_device_id"]), |
| 178 |
nodes=dict(type="list", elements="str"), |
| 179 |
interfaces=dict(type="list", elements="str"), |
| 180 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 181 |
) |
| 182 |
|
| 183 |
module = AnsibleModule( |
| 184 |
argument_spec=argument_spec, |
| 185 |
supports_check_mode=True, |
| 186 |
required_if=[ |
| 187 |
["state", "present", ["name", "uuid"], True], |
| 188 |
["state", "absent", ["name", "uuid"], True], |
| 189 |
["state", "present", ["fex_id", "nodes", "interfaces"]], |
| 190 |
], |
| 191 |
required_one_of=[["template", "template_id"]], |
| 192 |
mutually_exclusive=[ |
| 193 |
["template", "template_id"], |
| 194 |
], |
| 195 |
) |
| 196 |
|
| 197 |
mso = MSOModule(module) |
| 198 |
|
| 199 |
template = module.params.get("template") |
| 200 |
template_id = module.params.get("template_id") |
| 201 |
name = module.params.get("name") |
| 202 |
uuid = module.params.get("uuid") |
| 203 |
description = module.params.get("description") |
| 204 |
fex_id = module.params.get("fex_id") |
| 205 |
nodes = module.params.get("nodes") |
| 206 |
interfaces = module.params.get("interfaces") |
| 207 |
if interfaces: |
| 208 |
interfaces = ",".join(interfaces) |
| 209 |
state = module.params.get("state") |
| 210 |
|
| 211 |
ops = [] |
| 212 |
match = None |
| 213 |
fex_device_path = None |
| 214 |
|
| 215 |
mso_template = MSOTemplate(mso, "fabric_resource", template, template_id) |
| 216 |
mso_template.validate_template("fabricResource") |
| 217 |
object_description = "Fex Device" |
| 218 |
path = "/fabricResourceTemplate/template/fexDevices" |
| 219 |
|
| 220 |
existing_fex_devices = mso_template.template.get("fabricResourceTemplate", {}).get("template", {}).get("fexDevices") or [] |
| 221 |
|
| 222 |
if name or uuid: |
| 223 |
match = mso_template.get_object_by_key_value_pairs( |
| 224 |
object_description, |
| 225 |
existing_fex_devices, |
| 226 |
[KVPair("uuid", uuid) if uuid else KVPair("name", name)], |
| 227 |
) |
| 228 |
if match: |
| 229 |
fex_device_path = "{0}/{1}".format(path, match.index) |
| 230 |
mso_template.update_config_with_template_and_references(match.details) |
| 231 |
mso.existing = mso.previous = copy.deepcopy(match.details) |
| 232 |
else: |
| 233 |
mso.existing = mso.previous = [mso_template.update_config_with_template_and_references(device) for device in existing_fex_devices] |
| 234 |
|
| 235 |
if state == "present": |
| 236 |
mso_values = dict( |
| 237 |
name=name, |
| 238 |
description=description, |
| 239 |
fexId=fex_id, |
| 240 |
nodes=nodes, |
| 241 |
interfaces=interfaces, |
| 242 |
) |
| 243 |
|
| 244 |
if match: |
| 245 |
append_update_ops_data(ops, match.details, fex_device_path, mso_values) |
| 246 |
mso.sanitize(match.details, collate=True) |
| 247 |
else: |
| 248 |
mso.sanitize(mso_values) |
| 249 |
ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent)) |
| 250 |
|
| 251 |
elif state == "absent": |
| 252 |
if match: |
| 253 |
ops.append(dict(op="remove", path=fex_device_path)) |
| 254 |
|
| 255 |
if not module.check_mode and ops: |
| 256 |
response = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 257 |
fex_devices = response.get("fabricResourceTemplate", {}).get("template", {}).get("fexDevices") or [] |
| 258 |
match = mso_template.get_object_by_key_value_pairs(object_description, fex_devices, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]) |
| 259 |
if match: |
| 260 |
mso_template.update_config_with_template_and_references(match.details) |
| 261 |
mso.existing = match.details |
| 262 |
else: |
| 263 |
mso.existing = {} |
| 264 |
elif module.check_mode and state != "query": |
| 265 |
mso_template.update_config_with_template_and_references(mso.proposed) |
| 266 |
mso.existing = mso.proposed if state == "present" else {} |
| 267 |
|
| 268 |
mso.exit_json() |
| 269 |
|
| 270 |
|
| 271 |
if __name__ == "__main__": |
| 272 |
main() |