| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: ome_template_network_vlan |
| 19 |
short_description: Set tagged and untagged vlans to native network card supported by a template on OpenManage Enterprise |
| 20 |
version_added: "2.0.0" |
| 21 |
description: "This module allows to set tagged and untagged vlans to native network card supported by a template |
| 22 |
on OpenManage Enterprise." |
| 23 |
extends_documentation_fragment: |
| 24 |
- dellemc.openmanage.ome_auth_options |
| 25 |
options: |
| 26 |
template_name: |
| 27 |
description: |
| 28 |
- Name of the template. |
| 29 |
- It is mutually exclusive with I(template_id). |
| 30 |
type: str |
| 31 |
template_id: |
| 32 |
description: |
| 33 |
- Id of the template. |
| 34 |
- It is mutually exclusive with I(template_name). |
| 35 |
type: int |
| 36 |
nic_identifier: |
| 37 |
description: Display name of NIC port in the template for VLAN configuration. |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
propagate_vlan: |
| 41 |
description: |
| 42 |
- To deploy the modified VLAN settings immediately without rebooting the server. |
| 43 |
- This option will be applied only when there are changes to the VLAN configuration. |
| 44 |
default: true |
| 45 |
type: bool |
| 46 |
version_added: 3.4.0 |
| 47 |
untagged_networks: |
| 48 |
description: List of untagged networks and their corresponding NIC ports. |
| 49 |
elements: dict |
| 50 |
type: list |
| 51 |
suboptions: |
| 52 |
port: |
| 53 |
description: NIC port number of the untagged VLAN. |
| 54 |
required: true |
| 55 |
type: int |
| 56 |
untagged_network_id: |
| 57 |
description: |
| 58 |
- ID of the untagged VLAN |
| 59 |
- Enter 0 to clear the untagged VLAN from the port. |
| 60 |
- This option is mutually exclusive with I(untagged_network_name) |
| 61 |
- To get the VLAN network ID use the API U( https://I(hostname)/api/NetworkConfigurationService/Networks) |
| 62 |
type: int |
| 63 |
untagged_network_name: |
| 64 |
description: |
| 65 |
- name of the vlan for untagging |
| 66 |
- provide 0 for clearing the untagging for this I(port) |
| 67 |
- This parameter is mutually exclusive with I(untagged_network_id) |
| 68 |
type: str |
| 69 |
tagged_networks: |
| 70 |
description: List of tagged VLANs and their corresponding NIC ports. |
| 71 |
type: list |
| 72 |
elements: dict |
| 73 |
suboptions: |
| 74 |
port: |
| 75 |
description: NIC port number of the tagged VLAN |
| 76 |
required: true |
| 77 |
type: int |
| 78 |
tagged_network_ids: |
| 79 |
description: |
| 80 |
- List of IDs of the tagged VLANs |
| 81 |
- Enter [] to remove the tagged VLAN from a port. |
| 82 |
- List of I(tagged_network_ids) is combined with list of I(tagged_network_names) when adding tagged VLANs to a port. |
| 83 |
- To get the VLAN network ID use the API U( https://I(hostname)/api/NetworkConfigurationService/Networks) |
| 84 |
type: list |
| 85 |
elements: int |
| 86 |
tagged_network_names: |
| 87 |
description: |
| 88 |
- List of names of tagged VLANs |
| 89 |
- Enter [] to remove the tagged VLAN from a port. |
| 90 |
- List of I(tagged_network_names) is combined with list of I(tagged_network_ids) when adding tagged VLANs to a port. |
| 91 |
type: list |
| 92 |
elements: str |
| 93 |
requirements: |
| 94 |
- "python >= 3.9.6" |
| 95 |
author: |
| 96 |
- "Jagadeesh N V(@jagadeeshnv)" |
| 97 |
notes: |
| 98 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 99 |
- This module supports C(check_mode). |
| 100 |
''' |
| 101 |
|
| 102 |
EXAMPLES = r''' |
| 103 |
--- |
| 104 |
- name: Add tagged or untagged VLANs to a template using VLAN ID and name |
| 105 |
dellemc.openmanage.ome_template_network_vlan: |
| 106 |
hostname: "192.168.0.1" |
| 107 |
username: "username" |
| 108 |
password: "password" |
| 109 |
ca_path: "/path/to/ca_cert.pem" |
| 110 |
template_id: 78 |
| 111 |
nic_identifier: NIC Slot 4 |
| 112 |
untagged_networks: |
| 113 |
- port: 1 |
| 114 |
untagged_network_id: 127656 |
| 115 |
- port: 2 |
| 116 |
untagged_network_name: vlan2 |
| 117 |
tagged_networks: |
| 118 |
- port: 1 |
| 119 |
tagged_network_ids: |
| 120 |
- 12767 |
| 121 |
- 12768 |
| 122 |
- port: 4 |
| 123 |
tagged_network_ids: |
| 124 |
- 12767 |
| 125 |
- 12768 |
| 126 |
tagged_network_names: |
| 127 |
- vlan3 |
| 128 |
- port: 2 |
| 129 |
tagged_network_names: |
| 130 |
- vlan4 |
| 131 |
- vlan1 |
| 132 |
|
| 133 |
- name: Clear the tagged and untagged VLANs from a template |
| 134 |
dellemc.openmanage.ome_template_network_vlan: |
| 135 |
hostname: "192.168.0.1" |
| 136 |
username: "username" |
| 137 |
password: "password" |
| 138 |
ca_path: "/path/to/ca_cert.pem" |
| 139 |
template_id: 78 |
| 140 |
nic_identifier: NIC Slot 4 |
| 141 |
untagged_networks: |
| 142 |
# For removing the untagged VLANs for the port 1 and 2 |
| 143 |
- port: 1 |
| 144 |
untagged_network_id: 0 |
| 145 |
- port: 2 |
| 146 |
untagged_network_name: 0 |
| 147 |
tagged_networks: |
| 148 |
# For removing the tagged VLANs for port 1, 4 and 2 |
| 149 |
- port: 1 |
| 150 |
tagged_network_ids: [] |
| 151 |
- port: 4 |
| 152 |
tagged_network_ids: [] |
| 153 |
tagged_network_names: [] |
| 154 |
- port: 2 |
| 155 |
tagged_network_names: [] |
| 156 |
''' |
| 157 |
|
| 158 |
RETURN = r''' |
| 159 |
--- |
| 160 |
msg: |
| 161 |
type: str |
| 162 |
description: Overall status of the template vlan operation. |
| 163 |
returned: always |
| 164 |
sample: "Successfully applied the network settings to template." |
| 165 |
error_info: |
| 166 |
description: Details of the HTTP Error. |
| 167 |
returned: on HTTP error |
| 168 |
type: dict |
| 169 |
sample: { |
| 170 |
"error": { |
| 171 |
"@Message.ExtendedInfo": [ |
| 172 |
{ |
| 173 |
"Message": "Unable to complete the request because |
| 174 |
TemplateId does not exist or is not applicable for the |
| 175 |
resource URI.", |
| 176 |
"MessageArgs": [ |
| 177 |
"TemplateId" |
| 178 |
], |
| 179 |
"MessageId": "CGEN1004", |
| 180 |
"RelatedProperties": [], |
| 181 |
"Resolution": "Check the request resource URI. Refer to |
| 182 |
the OpenManage Enterprise-Modular User's Guide for more |
| 183 |
information about resource URI and its properties.", |
| 184 |
"Severity": "Critical" |
| 185 |
} |
| 186 |
], |
| 187 |
"code": "Base.1.0.GeneralError", |
| 188 |
"message": "A general error has occurred. See ExtendedInfo for more information." |
| 189 |
} |
| 190 |
} |
| 191 |
''' |
| 192 |
|
| 193 |
import json |
| 194 |
from ssl import SSLError |
| 195 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 196 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 197 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 198 |
|
| 199 |
NETWORK_HIERARCHY_VIEW = 4 |
| 200 |
UPDATE_NETWORK_CONFIG = "TemplateService/Actions/TemplateService.UpdateNetworkConfig" |
| 201 |
TEMPLATE_ATTRIBUTE_VIEW = "TemplateService/Templates({0})/Views({1}" \ |
| 202 |
")/AttributeViewDetails" |
| 203 |
VLAN_NETWORKS = "NetworkConfigurationService/Networks?$top=9999" |
| 204 |
TEMPLATE_VIEW = "TemplateService/Templates" |
| 205 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 206 |
CHANGES_FOUND = "Changes found to be applied." |
| 207 |
SUCCESS_MSG = "Successfully applied the network settings to the template." |
| 208 |
KEY_ATTR_NAME = 'DisplayName' |
| 209 |
SUB_GRP_ATTR_NAME = 'SubAttributeGroups' |
| 210 |
GRP_ATTR_NAME = 'Attributes' |
| 211 |
GRP_NAME_ID_ATTR_NAME = 'GroupNameId' |
| 212 |
CUSTOM_ID_ATTR_NAME = 'CustomId' |
| 213 |
|
| 214 |
|
| 215 |
def get_template_details(module, rest_obj): |
| 216 |
id = module.params.get('template_id') |
| 217 |
query_param = {"$filter": "Id eq {0}".format(id)} |
| 218 |
srch = 'Id' |
| 219 |
if not id: |
| 220 |
id = module.params.get('template_name') |
| 221 |
query_param = {"$filter": "Name eq '{0}'".format(id)} |
| 222 |
srch = 'Name' |
| 223 |
resp = rest_obj.invoke_request('GET', TEMPLATE_VIEW, query_param=query_param) |
| 224 |
if resp.success and resp.json_data.get('value'): |
| 225 |
tlist = resp.json_data.get('value', []) |
| 226 |
for xtype in tlist: |
| 227 |
if xtype.get(srch) == id: |
| 228 |
return xtype |
| 229 |
module.exit_json(msg="Template with {0} '{1}' not found.".format(srch, id), failed=True) |
| 230 |
|
| 231 |
|
| 232 |
def get_vlan_name_id_map(rest_obj): |
| 233 |
k = "Name" |
| 234 |
v = "Id" |
| 235 |
d = {} |
| 236 |
resp = rest_obj.invoke_request('GET', VLAN_NETWORKS) |
| 237 |
if resp.success and resp.json_data.get('value'): |
| 238 |
tlist = resp.json_data.get('value', []) |
| 239 |
for xtype in tlist: |
| 240 |
d[xtype[k]] = xtype[v] |
| 241 |
return d |
| 242 |
|
| 243 |
|
| 244 |
def get_template_vlan_info(module, rest_obj, template_id): |
| 245 |
port_id_map = {} |
| 246 |
port_untagged_map = {} |
| 247 |
port_tagged_map = {} |
| 248 |
port_nic_bond_map = {} |
| 249 |
nic_bonding_tech = "" |
| 250 |
resp = rest_obj.invoke_request('GET', TEMPLATE_ATTRIBUTE_VIEW.format( |
| 251 |
template_id, NETWORK_HIERARCHY_VIEW)) |
| 252 |
if resp.success: |
| 253 |
nic_id = module.params.get("nic_identifier") |
| 254 |
nic_model = resp.json_data.get('AttributeGroups', []) |
| 255 |
|
| 256 |
for xnic in nic_model: |
| 257 |
if xnic.get(KEY_ATTR_NAME) == "NICModel": |
| 258 |
nic_group = xnic.get('SubAttributeGroups', []) |
| 259 |
if xnic.get(KEY_ATTR_NAME) == "NicBondingTechnology": |
| 260 |
nic_bonding_list = xnic.get("Attributes", []) |
| 261 |
for xbnd in nic_bonding_list: |
| 262 |
if xbnd.get(KEY_ATTR_NAME).lower() == "nic bonding technology": |
| 263 |
nic_bonding_tech = xbnd.get('Value') |
| 264 |
nic_found = False |
| 265 |
for nic in nic_group: |
| 266 |
if nic_id == nic.get(KEY_ATTR_NAME): |
| 267 |
nic_found = True |
| 268 |
for port in nic.get(SUB_GRP_ATTR_NAME): |
| 269 |
for partition in port.get(SUB_GRP_ATTR_NAME): |
| 270 |
for attribute in partition.get(GRP_ATTR_NAME): |
| 271 |
if attribute.get(CUSTOM_ID_ATTR_NAME) != 0: |
| 272 |
port_number = port.get(GRP_NAME_ID_ATTR_NAME) |
| 273 |
port_id_map[port_number] = attribute.get(CUSTOM_ID_ATTR_NAME) |
| 274 |
if attribute.get(KEY_ATTR_NAME).lower() == "vlan untagged": |
| 275 |
port_untagged_map[port_number] = int(attribute['Value']) |
| 276 |
if attribute.get(KEY_ATTR_NAME).lower() == "vlan tagged": |
| 277 |
port_tagged_map[port_number] = [] |
| 278 |
if attribute['Value']: |
| 279 |
port_tagged_map[port_number] = \ |
| 280 |
list(map(int, (attribute['Value']).replace(" ", "").split(","))) |
| 281 |
if attribute.get(KEY_ATTR_NAME).lower() == "nic bonding enabled": |
| 282 |
port_nic_bond_map[port_number] = attribute['Value'] |
| 283 |
if not nic_found: |
| 284 |
module.exit_json(msg="NIC with name '{0}' not found for template with id {1}".format(nic_id, template_id), failed=True) |
| 285 |
return port_id_map, port_untagged_map, port_tagged_map, port_nic_bond_map, nic_bonding_tech |
| 286 |
|
| 287 |
|
| 288 |
def compare_nested_dict(modify_setting_payload, existing_setting_payload): |
| 289 |
"""compare existing and requested setting values of identity pool in case of modify operations |
| 290 |
if both are same return True""" |
| 291 |
for key, val in modify_setting_payload.items(): |
| 292 |
if existing_setting_payload.get(key) is None: |
| 293 |
return False |
| 294 |
elif isinstance(val, dict): |
| 295 |
if not compare_nested_dict(val, existing_setting_payload.get(key)): |
| 296 |
return False |
| 297 |
elif val != existing_setting_payload.get(key): |
| 298 |
return False |
| 299 |
return True |
| 300 |
|
| 301 |
|
| 302 |
def get_vlan_payload(module, rest_obj, untag_dict, tagged_dict): |
| 303 |
payload = {} |
| 304 |
template = get_template_details(module, rest_obj) |
| 305 |
payload["TemplateId"] = template["Id"] |
| 306 |
payload["IdentityPoolId"] = template["IdentityPoolId"] |
| 307 |
|
| 308 |
port_id_map, port_untagged_map, port_tagged_map, port_nic_bond_map, nic_bonding_tech =\ |
| 309 |
get_template_vlan_info(module, rest_obj, template['Id']) |
| 310 |
payload["BondingTechnology"] = nic_bonding_tech |
| 311 |
payload["PropagateVlan"] = module.params.get('propagate_vlan') |
| 312 |
untag_equal_dict = compare_nested_dict(untag_dict, port_untagged_map) |
| 313 |
tag_equal_dict = compare_nested_dict(tagged_dict, port_tagged_map) |
| 314 |
if untag_equal_dict and tag_equal_dict: |
| 315 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 316 |
vlan_attributes = [] |
| 317 |
for pk, pv in port_id_map.items(): |
| 318 |
mdict = {} |
| 319 |
if pk in untag_dict or pk in tagged_dict: |
| 320 |
mdict["Untagged"] = untag_dict.pop(pk, port_untagged_map.get(pk)) |
| 321 |
mdict["Tagged"] = tagged_dict.pop(pk, port_tagged_map.get(pk)) |
| 322 |
mdict["ComponentId"] = port_id_map.get(pk) |
| 323 |
mdict["IsNicBonded"] = port_nic_bond_map.get(pk) |
| 324 |
if mdict: |
| 325 |
vlan_attributes.append(mdict) |
| 326 |
if untag_dict: |
| 327 |
module.exit_json(msg="Invalid port(s) {0} found for untagged VLAN".format(untag_dict.keys()), failed=True) |
| 328 |
if tagged_dict: |
| 329 |
module.exit_json(msg="Invalid port(s) {0} found for tagged VLAN".format(tagged_dict.keys()), failed=True) |
| 330 |
if module.check_mode: |
| 331 |
module.exit_json(changed=True, msg=CHANGES_FOUND) |
| 332 |
payload["VlanAttributes"] = vlan_attributes |
| 333 |
return payload |
| 334 |
|
| 335 |
|
| 336 |
def get_key(val, my_dict): |
| 337 |
for key, value in my_dict.items(): |
| 338 |
if val == value: |
| 339 |
return key |
| 340 |
return None |
| 341 |
|
| 342 |
|
| 343 |
def validate_vlans(module, rest_obj): |
| 344 |
vlan_name_id_map = get_vlan_name_id_map(rest_obj) |
| 345 |
vlan_name_id_map["0"] = 0 |
| 346 |
tagged_list = module.params.get("tagged_networks") |
| 347 |
untag_list = module.params.get("untagged_networks") |
| 348 |
untag_dict = {} |
| 349 |
if untag_list: |
| 350 |
for utg in untag_list: |
| 351 |
p = utg["port"] |
| 352 |
if utg.get("untagged_network_id") is not None: |
| 353 |
if p in untag_dict: |
| 354 |
module.exit_json(msg="port {0} is repeated for " |
| 355 |
"untagged_network_id".format(p), failed=True) |
| 356 |
vlan = utg.get("untagged_network_id") |
| 357 |
if vlan not in vlan_name_id_map.values(): |
| 358 |
module.exit_json(msg="untagged_network_id: {0} is not a " |
| 359 |
"valid vlan id for port {1}". |
| 360 |
format(vlan, p), failed=True) |
| 361 |
untag_dict[p] = vlan |
| 362 |
if utg.get("untagged_network_name"): |
| 363 |
vlan = utg.get("untagged_network_name") |
| 364 |
if vlan in vlan_name_id_map: |
| 365 |
if p in untag_dict: |
| 366 |
module.exit_json(msg="port {0} is repeated for " |
| 367 |
"untagged_network_name".format(p), failed=True) |
| 368 |
untag_dict[p] = vlan_name_id_map.get(vlan) |
| 369 |
else: |
| 370 |
module.exit_json(msg="{0} is not a valid vlan name for port {1}".format(vlan, p), failed=True) |
| 371 |
vlan_name_id_map.pop("0") |
| 372 |
tagged_dict = {} |
| 373 |
if tagged_list: |
| 374 |
for tg in tagged_list: |
| 375 |
p = tg["port"] |
| 376 |
tg_list = [] |
| 377 |
empty_list = False |
| 378 |
tgnids = tg.get("tagged_network_ids") |
| 379 |
if isinstance(tgnids, list): |
| 380 |
if len(tgnids) == 0: |
| 381 |
empty_list = True |
| 382 |
for vl in tgnids: |
| 383 |
if vl not in vlan_name_id_map.values(): |
| 384 |
module.exit_json(msg="{0} is not a valid vlan id " |
| 385 |
"port {1}".format(vl, p), failed=True) |
| 386 |
tg_list.append(vl) |
| 387 |
tgnames = tg.get("tagged_network_names") |
| 388 |
if isinstance(tgnames, list): |
| 389 |
if len(tgnames) == 0: |
| 390 |
empty_list = True |
| 391 |
for vln in tgnames: |
| 392 |
if vln not in vlan_name_id_map: |
| 393 |
module.exit_json(msg="{0} is not a valid vlan name " |
| 394 |
"port {1}".format(vln, p), failed=True) |
| 395 |
tg_list.append(vlan_name_id_map.get(vln)) |
| 396 |
if not tg_list and not empty_list: |
| 397 |
module.exit_json(msg="No tagged_networks provided or valid tagged_networks not found for port {0}" |
| 398 |
.format(p), failed=True) |
| 399 |
tagged_dict[p] = list(set(tg_list)) |
| 400 |
for k, v in untag_dict.items(): |
| 401 |
if v in tagged_dict.get(k, []): |
| 402 |
module.exit_json(msg="vlan {0}('{1}') cannot be in both tagged and untagged list for port {2}". |
| 403 |
format(v, get_key(v, vlan_name_id_map), k), failed=True) |
| 404 |
return untag_dict, tagged_dict |
| 405 |
|
| 406 |
|
| 407 |
def main(): |
| 408 |
port_untagged_spec = {"port": {"required": True, "type": "int"}, |
| 409 |
"untagged_network_id": {"type": "int"}, |
| 410 |
"untagged_network_name": {"type": "str"}} |
| 411 |
port_tagged_spec = {"port": {"required": True, "type": "int"}, |
| 412 |
"tagged_network_ids": {"type": "list", "elements": "int"}, |
| 413 |
"tagged_network_names": {"type": "list", "elements": "str"}} |
| 414 |
specs = { |
| 415 |
"template_name": {"required": False, "type": "str"}, |
| 416 |
"template_id": {"required": False, "type": "int"}, |
| 417 |
"nic_identifier": {"required": True, "type": "str"}, |
| 418 |
"untagged_networks": {"required": False, "type": "list", "elements": "dict", "options": port_untagged_spec, |
| 419 |
"mutually_exclusive": [("untagged_network_id", "untagged_network_name")]}, |
| 420 |
"tagged_networks": {"required": False, "type": "list", "elements": "dict", "options": port_tagged_spec}, |
| 421 |
"propagate_vlan": {"type": "bool", "default": True} |
| 422 |
} |
| 423 |
|
| 424 |
module = OmeAnsibleModule( |
| 425 |
argument_spec=specs, |
| 426 |
required_one_of=[("template_id", "template_name"), |
| 427 |
("untagged_networks", "tagged_networks")], |
| 428 |
mutually_exclusive=[("template_id", "template_name")], |
| 429 |
supports_check_mode=True |
| 430 |
) |
| 431 |
try: |
| 432 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 433 |
untag_dict, tagged_dict = validate_vlans(module, rest_obj) |
| 434 |
payload = get_vlan_payload(module, rest_obj, untag_dict, tagged_dict) |
| 435 |
resp = rest_obj.invoke_request("POST", UPDATE_NETWORK_CONFIG, data=payload) |
| 436 |
if resp.success: |
| 437 |
module.exit_json(msg=SUCCESS_MSG, changed=True) |
| 438 |
except HTTPError as err: |
| 439 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 440 |
except URLError as err: |
| 441 |
module.exit_json(msg=str(err), unreachable=True) |
| 442 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError, OSError) as err: |
| 443 |
module.exit_json(msg=str(err), failed=True) |
| 444 |
|
| 445 |
|
| 446 |
if __name__ == "__main__": |
| 447 |
main() |