| 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_network_vlan |
| 19 |
short_description: Create, modify & delete a VLAN |
| 20 |
version_added: "2.1.0" |
| 21 |
description: |
| 22 |
- This module allows to, |
| 23 |
- Create a VLAN on OpenManage Enterprise. |
| 24 |
- Modify or delete an existing VLAN on OpenManage Enterprise. |
| 25 |
extends_documentation_fragment: |
| 26 |
- dellemc.openmanage.ome_auth_options |
| 27 |
options: |
| 28 |
state: |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- C(present) creates a new VLAN or modifies an existing VLAN. |
| 32 |
- C(absent) deletes an existing VLAN. |
| 33 |
- I(WARNING) Deleting a VLAN can impact the network infrastructure. |
| 34 |
choices: [present, absent] |
| 35 |
default: present |
| 36 |
name: |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
description: Provide the I(name) of the VLAN to be created, deleted or modified. |
| 40 |
new_name: |
| 41 |
type: str |
| 42 |
description: Provide the I(name) of the VLAN to be modified. |
| 43 |
description: |
| 44 |
type: str |
| 45 |
description: Short description of the VLAN to be created or modified. |
| 46 |
vlan_minimum: |
| 47 |
type: int |
| 48 |
description: |
| 49 |
- The minimum VLAN value of the range. |
| 50 |
vlan_maximum: |
| 51 |
type: int |
| 52 |
description: |
| 53 |
- The maximum VLAN value of the range. |
| 54 |
- A single value VLAN is created if the vlan_maximum and vlan_minmum values are the same. |
| 55 |
type: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- Types of supported VLAN networks. |
| 59 |
- "For the description of each network type, |
| 60 |
use API U(https://I(hostname)/api/NetworkConfigurationService/NetworkTypes)." |
| 61 |
choices: ['General Purpose (Bronze)', 'General Purpose (Silver)', 'General Purpose (Gold)', |
| 62 |
'General Purpose (Platinum)', 'Cluster Interconnect', 'Hypervisor Management', |
| 63 |
'Storage - iSCSI', 'Storage - FCoE', 'Storage - Data Replication', |
| 64 |
'VM Migration', 'VMWare FT Logging'] |
| 65 |
requirements: |
| 66 |
- "python >= 3.9.6" |
| 67 |
author: |
| 68 |
- "Jagadeesh N V(@jagadeeshnv)" |
| 69 |
- "Meenakshi Dembi(@meenakshidembi691)" |
| 70 |
notes: |
| 71 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 72 |
- This module supports C(check_mode). |
| 73 |
''' |
| 74 |
|
| 75 |
EXAMPLES = r''' |
| 76 |
--- |
| 77 |
- name: Create a VLAN range |
| 78 |
dellemc.openmanage.ome_network_vlan: |
| 79 |
hostname: "{{hostname}}" |
| 80 |
username: "{{username}}" |
| 81 |
password: "{{password}}" |
| 82 |
ca_path: "/path/to/ca_cert.pem" |
| 83 |
state: present |
| 84 |
name: "vlan1" |
| 85 |
description: "VLAN desc" |
| 86 |
type: "General Purpose (Bronze)" |
| 87 |
vlan_minimum: 35 |
| 88 |
vlan_maximum: 40 |
| 89 |
tags: create_vlan_range |
| 90 |
|
| 91 |
- name: Create a VLAN with a single value |
| 92 |
dellemc.openmanage.ome_network_vlan: |
| 93 |
hostname: "{{hostname}}" |
| 94 |
username: "{{username}}" |
| 95 |
password: "{{password}}" |
| 96 |
ca_path: "/path/to/ca_cert.pem" |
| 97 |
state: present |
| 98 |
name: "vlan2" |
| 99 |
description: "VLAN desc" |
| 100 |
type: "General Purpose (Bronze)" |
| 101 |
vlan_minimum: 127 |
| 102 |
vlan_maximum: 127 |
| 103 |
tags: create_vlan_single |
| 104 |
|
| 105 |
- name: Modify a VLAN |
| 106 |
dellemc.openmanage.ome_network_vlan: |
| 107 |
hostname: "{{hostname}}" |
| 108 |
username: "{{username}}" |
| 109 |
password: "{{password}}" |
| 110 |
ca_path: "/path/to/ca_cert.pem" |
| 111 |
state: present |
| 112 |
name: "vlan1" |
| 113 |
new_name: "vlan_gold1" |
| 114 |
description: "new description" |
| 115 |
type: "General Purpose (Gold)" |
| 116 |
vlan_minimum: 45 |
| 117 |
vlan_maximum: 50 |
| 118 |
tags: modify_vlan |
| 119 |
|
| 120 |
- name: Delete a VLAN |
| 121 |
dellemc.openmanage.ome_network_vlan: |
| 122 |
hostname: "{{hostname}}" |
| 123 |
username: "{{username}}" |
| 124 |
password: "{{password}}" |
| 125 |
ca_path: "/path/to/ca_cert.pem" |
| 126 |
state: "absent" |
| 127 |
name: "vlan1" |
| 128 |
tags: delete_vlan |
| 129 |
''' |
| 130 |
|
| 131 |
RETURN = r''' |
| 132 |
--- |
| 133 |
msg: |
| 134 |
type: str |
| 135 |
description: Overall status of the VLAN operation. |
| 136 |
returned: always |
| 137 |
sample: "Successfully created the VLAN." |
| 138 |
vlan_status: |
| 139 |
type: dict |
| 140 |
description: Details of the VLAN that is either created or modified. |
| 141 |
returned: when I(state=present) |
| 142 |
sample: { |
| 143 |
"@odata.context": "/api/$metadata#NetworkConfigurationService.Network", |
| 144 |
"@odata.type": "#NetworkConfigurationService.Network", |
| 145 |
"@odata.id": "/api/NetworkConfigurationService/Networks(1234)", |
| 146 |
"Id": 1234, |
| 147 |
"Name": "vlan1", |
| 148 |
"Description": "VLAN description", |
| 149 |
"VlanMaximum": 130, |
| 150 |
"VlanMinimum": 140, |
| 151 |
"Type": 1, |
| 152 |
"CreatedBy": "admin", |
| 153 |
"CreationTime": "2020-01-01 05:54:36.113", |
| 154 |
"UpdatedBy": null, |
| 155 |
"UpdatedTime": "2020-01-01 05:54:36.113", |
| 156 |
"InternalRefNWUUId": "6d6effcc-eca4-44bd-be07-1234ab5cd67e" |
| 157 |
} |
| 158 |
error_info: |
| 159 |
description: Details of the HTTP Error. |
| 160 |
returned: on HTTP error |
| 161 |
type: dict |
| 162 |
sample: { |
| 163 |
"code": "Base.1.0.GeneralError", |
| 164 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 165 |
"@Message.ExtendedInfo": [ |
| 166 |
{ |
| 167 |
"MessageId": "CTEM1043", |
| 168 |
"RelatedProperties": [], |
| 169 |
"Message": "Unable to create or update the network because the entered VLAN minimum 0 |
| 170 |
is not within a valid range ( 1 - 4000 or 4021 - 4094 ).", |
| 171 |
"MessageArgs": [ |
| 172 |
"0", |
| 173 |
"1", |
| 174 |
"4000", |
| 175 |
"4021", |
| 176 |
"4094" |
| 177 |
], |
| 178 |
"Severity": "Warning", |
| 179 |
"Resolution": "Enter a valid VLAN minimum as identified in the message and retry the operation." |
| 180 |
} |
| 181 |
] |
| 182 |
} |
| 183 |
''' |
| 184 |
|
| 185 |
from ssl import SSLError |
| 186 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 187 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 188 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 189 |
|
| 190 |
VLAN_CONFIG = "NetworkConfigurationService/Networks" |
| 191 |
VLAN_ID_CONFIG = "NetworkConfigurationService/Networks({Id})" |
| 192 |
VLAN_TYPES = "NetworkConfigurationService/NetworkTypes" |
| 193 |
VLAN_RANGE_OVERLAP = "Unable to create or update the VLAN because the entered range" \ |
| 194 |
" overlaps with {vlan_name} with the range {vlan_min}-{vlan_max}." |
| 195 |
VLAN_VALUE_MSG = "VLAN-minimum value is greater than VLAN-maximum value." |
| 196 |
CHECK_MODE_MSG = "Changes found to be applied." |
| 197 |
|
| 198 |
|
| 199 |
def format_payload(src_dict): |
| 200 |
address_payload_map = { |
| 201 |
"name": "Name", |
| 202 |
"vlan_maximum": "VlanMaximum", |
| 203 |
"vlan_minimum": "VlanMinimum", |
| 204 |
"type": "Type" |
| 205 |
} |
| 206 |
if src_dict: |
| 207 |
return dict([(address_payload_map[key], val) for key, val in src_dict.items() if key in address_payload_map]) |
| 208 |
|
| 209 |
|
| 210 |
def get_item_id(rest_obj, name, uri): |
| 211 |
resp = rest_obj.invoke_request('GET', uri) |
| 212 |
tlist = [] |
| 213 |
if resp.success and resp.json_data.get('value'): |
| 214 |
tlist = resp.json_data.get('value', []) |
| 215 |
for xtype in tlist: |
| 216 |
if xtype.get('Name', "") == name: |
| 217 |
return xtype.get('Id'), tlist |
| 218 |
return 0, tlist |
| 219 |
|
| 220 |
|
| 221 |
def check_overlapping_vlan_range(payload, vlans): |
| 222 |
current_vlan = None |
| 223 |
for xtype in vlans: |
| 224 |
overlap = list(range(max(xtype.get('VlanMinimum', 0), payload["VlanMinimum"]), |
| 225 |
min(xtype.get('VlanMaximum', 0), payload["VlanMaximum"]) + 1)) |
| 226 |
if overlap: |
| 227 |
current_vlan = xtype |
| 228 |
break |
| 229 |
return current_vlan |
| 230 |
|
| 231 |
|
| 232 |
def create_vlan(module, rest_obj, vlans): |
| 233 |
payload = format_payload(module.params) |
| 234 |
if not all(payload.values()): |
| 235 |
module.exit_json(msg="The vlan_minimum, vlan_maximum and type values are required for " |
| 236 |
"creating a VLAN.", failed=True) |
| 237 |
if payload["VlanMinimum"] > payload["VlanMaximum"]: |
| 238 |
module.exit_json(msg=VLAN_VALUE_MSG, failed=True) |
| 239 |
overlap = check_overlapping_vlan_range(payload, vlans) |
| 240 |
if overlap: |
| 241 |
module.exit_json(msg=VLAN_RANGE_OVERLAP.format(vlan_name=overlap["Name"], vlan_min=overlap["VlanMinimum"], |
| 242 |
vlan_max=overlap["VlanMaximum"]), failed=True) |
| 243 |
if module.check_mode: |
| 244 |
module.exit_json(changed=True, msg=CHECK_MODE_MSG) |
| 245 |
if module.params.get("description"): |
| 246 |
payload["Description"] = module.params.get("description") |
| 247 |
payload["Type"], types = get_item_id(rest_obj, module.params["type"], VLAN_TYPES) |
| 248 |
if not payload["Type"]: |
| 249 |
module.exit_json(msg="Network type '{0}' not found.".format(module.params["type"]), failed=True) |
| 250 |
resp = rest_obj.invoke_request("POST", VLAN_CONFIG, data=payload) |
| 251 |
module.exit_json(msg="Successfully created the VLAN.", vlan_status=resp.json_data, changed=True) |
| 252 |
|
| 253 |
|
| 254 |
def delete_vlan(module, rest_obj, vlan_id): |
| 255 |
if module.check_mode: |
| 256 |
module.exit_json(changed=True, msg=CHECK_MODE_MSG) |
| 257 |
rest_obj.invoke_request("DELETE", VLAN_ID_CONFIG.format(Id=vlan_id)) |
| 258 |
module.exit_json(msg="Successfully deleted the VLAN.", changed=True) |
| 259 |
|
| 260 |
|
| 261 |
def modify_vlan(module, rest_obj, vlan_id, vlans): |
| 262 |
payload = format_payload(module.params) |
| 263 |
payload["Description"] = module.params.get("description") |
| 264 |
if module.params.get("type"): |
| 265 |
payload["Type"], types = get_item_id(rest_obj, module.params["type"], VLAN_TYPES) |
| 266 |
if not payload["Type"]: |
| 267 |
module.exit_json(msg="Network type '{0}' not found.".format(module.params["type"]), failed=True) |
| 268 |
if module.params.get("new_name"): |
| 269 |
payload["Name"] = module.params["new_name"] |
| 270 |
current_setting = {} |
| 271 |
for i in range(len(vlans)): |
| 272 |
if vlans[i]['Id'] == vlan_id: |
| 273 |
current_setting = vlans.pop(i) |
| 274 |
break |
| 275 |
diff = 0 |
| 276 |
for config, pload in payload.items(): |
| 277 |
pval = payload.get(config) |
| 278 |
if pval is not None: |
| 279 |
if current_setting.get(config) != pval: |
| 280 |
payload[config] = pval |
| 281 |
diff += 1 |
| 282 |
else: |
| 283 |
payload[config] = current_setting.get(config) |
| 284 |
if payload["VlanMinimum"] > payload["VlanMaximum"]: |
| 285 |
module.exit_json(msg=VLAN_VALUE_MSG, failed=True) |
| 286 |
overlap = check_overlapping_vlan_range(payload, vlans) |
| 287 |
if overlap: |
| 288 |
module.exit_json(msg=VLAN_RANGE_OVERLAP.format(vlan_name=overlap["Name"], vlan_min=overlap["VlanMinimum"], |
| 289 |
vlan_max=overlap["VlanMaximum"]), failed=True) |
| 290 |
if diff == 0: |
| 291 |
if module.check_mode: |
| 292 |
module.exit_json(msg="No changes found to be applied to the VLAN configuration.") |
| 293 |
module.exit_json(msg="No changes found to be applied as the entered values are the same as the" |
| 294 |
" current configuration.", vlan_status=current_setting) |
| 295 |
if module.check_mode: |
| 296 |
module.exit_json(changed=True, msg=CHECK_MODE_MSG) |
| 297 |
payload["Id"] = vlan_id |
| 298 |
resp = rest_obj.invoke_request("PUT", VLAN_ID_CONFIG.format(Id=vlan_id), data=payload) |
| 299 |
module.exit_json(msg="Successfully updated the VLAN.", vlan_status=resp.json_data, changed=True) |
| 300 |
|
| 301 |
|
| 302 |
def check_existing_vlan(module, rest_obj): |
| 303 |
vlan_id, vlans = get_item_id(rest_obj, module.params["name"], VLAN_CONFIG + "?$top=9999") |
| 304 |
return vlan_id, vlans |
| 305 |
|
| 306 |
|
| 307 |
def main(): |
| 308 |
specs = { |
| 309 |
"state": {"required": False, "choices": ['present', 'absent'], "default": "present"}, |
| 310 |
"name": {"required": True, "type": "str"}, |
| 311 |
"new_name": {"required": False, "type": "str"}, |
| 312 |
"description": {"required": False, "type": "str"}, |
| 313 |
"vlan_minimum": {"required": False, "type": "int"}, |
| 314 |
"vlan_maximum": {"required": False, "type": "int"}, |
| 315 |
"type": {"required": False, "type": "str", |
| 316 |
"choices": ['General Purpose (Bronze)', 'General Purpose (Silver)', 'General Purpose (Gold)', |
| 317 |
'General Purpose (Platinum)', 'Cluster Interconnect', 'Hypervisor Management', |
| 318 |
'Storage - iSCSI', 'Storage - FCoE', 'Storage - Data Replication', 'VM Migration', |
| 319 |
'VMWare FT Logging']} |
| 320 |
} |
| 321 |
|
| 322 |
module = OmeAnsibleModule( |
| 323 |
argument_spec=specs, |
| 324 |
required_if=[['state', 'present', ('new_name', 'description', 'vlan_minimum', 'vlan_maximum', 'type',), True]], |
| 325 |
supports_check_mode=True |
| 326 |
) |
| 327 |
try: |
| 328 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 329 |
vlan_id, vlans = check_existing_vlan(module, rest_obj) |
| 330 |
if module.params["state"] == "present": |
| 331 |
if vlan_id: |
| 332 |
modify_vlan(module, rest_obj, vlan_id, vlans) |
| 333 |
create_vlan(module, rest_obj, vlans) |
| 334 |
else: |
| 335 |
if vlan_id: |
| 336 |
delete_vlan(module, rest_obj, vlan_id) |
| 337 |
if module.check_mode: |
| 338 |
module.exit_json(msg="No changes found to be applied to the VLAN configuration.") |
| 339 |
module.exit_json(msg="VLAN {0} does not exist.".format(module.params["name"])) |
| 340 |
except HTTPError as err: |
| 341 |
module.exit_json(msg=str(err), failed=True) |
| 342 |
except URLError as err: |
| 343 |
module.exit_json(msg=str(err), unreachable=True) |
| 344 |
except (IOError, ValueError, TypeError, ConnectionError, SSLValidationError, SSLError, OSError) as err: |
| 345 |
module.exit_json(msg=str(err), failed=True) |
| 346 |
|
| 347 |
|
| 348 |
if __name__ == "__main__": |
| 349 |
main() |