| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Bruno Calogero <[email protected]> |
| 5 |
# Copyright: (c) 2023, Gaspard Micol <[email protected]> |
| 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": "certified"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_fabric_node |
| 17 |
short_description: Manage Fabric Node Members (fabric:NodeIdentP) |
| 18 |
description: |
| 19 |
- Manage Fabric Node Members on Cisco ACI fabrics. |
| 20 |
options: |
| 21 |
pod_id: |
| 22 |
description: |
| 23 |
- The pod id of the new Fabric Node Member. |
| 24 |
type: int |
| 25 |
serial: |
| 26 |
description: |
| 27 |
- Serial Number for the new Fabric Node Member. |
| 28 |
type: str |
| 29 |
aliases: [ serial_number ] |
| 30 |
node_id: |
| 31 |
description: |
| 32 |
- Node ID Number for the new Fabric Node Member. |
| 33 |
type: int |
| 34 |
switch: |
| 35 |
description: |
| 36 |
- Switch Name for the new Fabric Node Member. |
| 37 |
type: str |
| 38 |
aliases: [ name, switch_name ] |
| 39 |
description: |
| 40 |
description: |
| 41 |
- Description for the new Fabric Node Member. |
| 42 |
type: str |
| 43 |
aliases: [ descr ] |
| 44 |
role: |
| 45 |
description: |
| 46 |
- Role for the new Fabric Node Member. |
| 47 |
type: str |
| 48 |
aliases: [ role_name ] |
| 49 |
choices: [ leaf, spine, unspecified ] |
| 50 |
node_type: |
| 51 |
description: |
| 52 |
- Type for the new Fabric Node Member. |
| 53 |
type: str |
| 54 |
choices: [ tier_2, remote, virtual, unspecified ] |
| 55 |
remote_leaf_pool_id: |
| 56 |
description: |
| 57 |
- External Pool Id of the remote leaf. |
| 58 |
- I(remote_leaf_pool_id) is incompatible with I(node_type) other than C(remote). |
| 59 |
- I(remote_leaf_pool_id) is required if I(node_type) is C(remote). |
| 60 |
type: str |
| 61 |
aliases: [ pool_id ] |
| 62 |
state: |
| 63 |
description: |
| 64 |
- Use C(present) or C(absent) for adding or removing. |
| 65 |
- Use C(query) for listing an object or multiple objects. |
| 66 |
type: str |
| 67 |
choices: [ absent, present, query ] |
| 68 |
default: present |
| 69 |
name_alias: |
| 70 |
description: |
| 71 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 72 |
type: str |
| 73 |
extends_documentation_fragment: |
| 74 |
- cisco.aci.aci |
| 75 |
- cisco.aci.annotation |
| 76 |
|
| 77 |
seealso: |
| 78 |
- name: APIC Management Information Model reference |
| 79 |
description: More information about the internal APIC class B(fabric:NodeIdentP). |
| 80 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 81 |
author: |
| 82 |
- Bruno Calogero (@brunocalogero) |
| 83 |
- Gaspard Micol (@gmicol) |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Add fabric node |
| 88 |
cisco.aci.aci_fabric_node: |
| 89 |
host: apic |
| 90 |
username: admin |
| 91 |
password: SomeSecretPassword |
| 92 |
serial: FDO2031124L |
| 93 |
node_id: 1011 |
| 94 |
switch: fab4-sw1011 |
| 95 |
state: present |
| 96 |
delegate_to: localhost |
| 97 |
|
| 98 |
- name: Remove fabric node |
| 99 |
cisco.aci.aci_fabric_node: |
| 100 |
host: apic |
| 101 |
username: admin |
| 102 |
password: SomeSecretPassword |
| 103 |
serial: FDO2031124L |
| 104 |
node_id: 1011 |
| 105 |
state: absent |
| 106 |
delegate_to: localhost |
| 107 |
|
| 108 |
- name: Query fabric nodes |
| 109 |
cisco.aci.aci_fabric_node: |
| 110 |
host: apic |
| 111 |
username: admin |
| 112 |
password: SomeSecretPassword |
| 113 |
state: query |
| 114 |
delegate_to: localhost |
| 115 |
register: query_result |
| 116 |
""" |
| 117 |
|
| 118 |
RETURN = r""" |
| 119 |
current: |
| 120 |
description: The existing configuration from the APIC after the module has finished |
| 121 |
returned: success |
| 122 |
type: list |
| 123 |
sample: |
| 124 |
[ |
| 125 |
{ |
| 126 |
"fvTenant": { |
| 127 |
"attributes": { |
| 128 |
"descr": "Production environment", |
| 129 |
"dn": "uni/tn-production", |
| 130 |
"name": "production", |
| 131 |
"nameAlias": "", |
| 132 |
"ownerKey": "", |
| 133 |
"ownerTag": "" |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
] |
| 138 |
error: |
| 139 |
description: The error information as returned from the APIC |
| 140 |
returned: failure |
| 141 |
type: dict |
| 142 |
sample: |
| 143 |
{ |
| 144 |
"code": "122", |
| 145 |
"text": "unknown managed object class foo" |
| 146 |
} |
| 147 |
raw: |
| 148 |
description: The raw output returned by the APIC REST API (xml or json) |
| 149 |
returned: parse error |
| 150 |
type: str |
| 151 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 152 |
sent: |
| 153 |
description: The actual/minimal configuration pushed to the APIC |
| 154 |
returned: info |
| 155 |
type: list |
| 156 |
sample: |
| 157 |
{ |
| 158 |
"fvTenant": { |
| 159 |
"attributes": { |
| 160 |
"descr": "Production environment" |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
previous: |
| 165 |
description: The original configuration from the APIC before the module has started |
| 166 |
returned: info |
| 167 |
type: list |
| 168 |
sample: |
| 169 |
[ |
| 170 |
{ |
| 171 |
"fvTenant": { |
| 172 |
"attributes": { |
| 173 |
"descr": "Production", |
| 174 |
"dn": "uni/tn-production", |
| 175 |
"name": "production", |
| 176 |
"nameAlias": "", |
| 177 |
"ownerKey": "", |
| 178 |
"ownerTag": "" |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
] |
| 183 |
proposed: |
| 184 |
description: The assembled configuration from the user-provided parameters |
| 185 |
returned: info |
| 186 |
type: dict |
| 187 |
sample: |
| 188 |
{ |
| 189 |
"fvTenant": { |
| 190 |
"attributes": { |
| 191 |
"descr": "Production environment", |
| 192 |
"name": "production" |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
filter_string: |
| 197 |
description: The filter string used for the request |
| 198 |
returned: failure or debug |
| 199 |
type: str |
| 200 |
sample: '?rsp-prop-include=config-only' |
| 201 |
method: |
| 202 |
description: The HTTP method used for the request to the APIC |
| 203 |
returned: failure or debug |
| 204 |
type: str |
| 205 |
sample: POST |
| 206 |
response: |
| 207 |
description: The HTTP response from the APIC |
| 208 |
returned: failure or debug |
| 209 |
type: str |
| 210 |
sample: OK (30 bytes) |
| 211 |
status: |
| 212 |
description: The HTTP status from the APIC |
| 213 |
returned: failure or debug |
| 214 |
type: int |
| 215 |
sample: 200 |
| 216 |
url: |
| 217 |
description: The HTTP url used for the request to the APIC |
| 218 |
returned: failure or debug |
| 219 |
type: str |
| 220 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 221 |
""" |
| 222 |
|
| 223 |
from ansible.module_utils.basic import AnsibleModule |
| 224 |
from ansible_collections.cisco.aci.plugins.module_utils.constants import NODE_TYPE_MAPPING |
| 225 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 226 |
|
| 227 |
|
| 228 |
# NOTE: (This problem is also present on the APIC GUI) |
| 229 |
# NOTE: When specifying a C(role) the new Fabric Node Member will be created but Role on GUI will be "unknown", hence not what seems to be a module problem |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
argument_spec = aci_argument_spec() |
| 234 |
argument_spec.update(aci_annotation_spec()) |
| 235 |
argument_spec.update( |
| 236 |
description=dict(type="str", aliases=["descr"]), |
| 237 |
node_id=dict(type="int"), # Not required for querying all objects |
| 238 |
pod_id=dict(type="int"), |
| 239 |
role=dict(type="str", choices=["leaf", "spine", "unspecified"], aliases=["role_name"]), |
| 240 |
node_type=dict(type="str", choices=list(NODE_TYPE_MAPPING.keys())), |
| 241 |
remote_leaf_pool_id=dict(type="str", aliases=["pool_id"]), |
| 242 |
serial=dict(type="str", aliases=["serial_number"]), # Not required for querying all objects |
| 243 |
switch=dict(type="str", aliases=["name", "switch_name"]), |
| 244 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 245 |
name_alias=dict(type="str"), |
| 246 |
) |
| 247 |
|
| 248 |
module = AnsibleModule( |
| 249 |
argument_spec=argument_spec, |
| 250 |
supports_check_mode=True, |
| 251 |
required_if=[ |
| 252 |
["state", "absent", ["node_id", "serial"]], |
| 253 |
["state", "present", ["node_id", "serial"]], |
| 254 |
["node_type", "remote", ["remote_leaf_pool_id"]], |
| 255 |
], |
| 256 |
) |
| 257 |
|
| 258 |
pod_id = module.params.get("pod_id") |
| 259 |
serial = module.params.get("serial") |
| 260 |
node_id = module.params.get("node_id") |
| 261 |
switch = module.params.get("switch") |
| 262 |
description = module.params.get("description") |
| 263 |
role = module.params.get("role") |
| 264 |
node_type = module.params.get("node_type") |
| 265 |
remote_leaf_pool_id = module.params.get("remote_leaf_pool_id") |
| 266 |
state = module.params.get("state") |
| 267 |
name_alias = module.params.get("name_alias") |
| 268 |
|
| 269 |
aci = ACIModule(module) |
| 270 |
|
| 271 |
if node_type != "remote" and remote_leaf_pool_id: |
| 272 |
module.fail_json(msg="External Pool Id is not compatible with a node type other than 'remote'.") |
| 273 |
|
| 274 |
node_type = NODE_TYPE_MAPPING.get(node_type) |
| 275 |
|
| 276 |
aci.construct_url( |
| 277 |
root_class=dict( |
| 278 |
aci_class="fabricNodeIdentP", |
| 279 |
aci_rn="controller/nodeidentpol/nodep-{0}".format(serial), |
| 280 |
module_object=serial, |
| 281 |
target_filter={"serial": serial}, |
| 282 |
) |
| 283 |
) |
| 284 |
|
| 285 |
aci.get_existing() |
| 286 |
|
| 287 |
if state == "present": |
| 288 |
aci.payload( |
| 289 |
aci_class="fabricNodeIdentP", |
| 290 |
class_config=dict( |
| 291 |
descr=description, |
| 292 |
name=switch, |
| 293 |
nodeId=node_id, |
| 294 |
podId=pod_id, |
| 295 |
# NOTE: Originally we were sending 'rn', but now we need 'dn' for idempotency |
| 296 |
dn="uni/controller/nodeidentpol/nodep-{0}".format(serial), |
| 297 |
role=role, |
| 298 |
nodeType=node_type, |
| 299 |
extPoolId=remote_leaf_pool_id, |
| 300 |
serial=serial, |
| 301 |
nameAlias=name_alias, |
| 302 |
), |
| 303 |
) |
| 304 |
|
| 305 |
aci.get_diff(aci_class="fabricNodeIdentP") |
| 306 |
|
| 307 |
aci.post_config() |
| 308 |
|
| 309 |
elif state == "absent": |
| 310 |
aci.delete_config() |
| 311 |
|
| 312 |
aci.exit_json(**aci.result) |
| 313 |
|
| 314 |
|
| 315 |
if __name__ == "__main__": |
| 316 |
main() |