| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import (absolute_import, division, print_function) |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = """ |
| 14 |
--- |
| 15 |
module: ome_device_mgmt_network |
| 16 |
short_description: Configure network settings of devices on OpenManage |
| 17 |
Enterprise Modular |
| 18 |
description: |
| 19 |
- This module allows to configure network settings on Chassis, Servers, |
| 20 |
and I/O Modules on OpenManage Enterprise Modular. |
| 21 |
version_added: 4.2.0 |
| 22 |
author: |
| 23 |
- Jagadeesh N V(@jagadeeshnv) |
| 24 |
- Bhavneet Sharma (@Bhavneet-Sharma) |
| 25 |
extends_documentation_fragment: |
| 26 |
- dellemc.openmanage.omem_auth_options |
| 27 |
options: |
| 28 |
device_service_tag: |
| 29 |
description: |
| 30 |
- Service tag of the device. |
| 31 |
- This option is mutually exclusive with I(device_id). |
| 32 |
type: str |
| 33 |
device_id: |
| 34 |
description: |
| 35 |
- ID of the device. |
| 36 |
- This option is mutually exclusive with I(device_service_tag). |
| 37 |
type: int |
| 38 |
enable_nic: |
| 39 |
description: |
| 40 |
- Enable or disable Network Interface Card (NIC) configuration of |
| 41 |
the device. |
| 42 |
- This option is not applicable to I/O Module. |
| 43 |
type: bool |
| 44 |
default: true |
| 45 |
delay: |
| 46 |
description: |
| 47 |
- The time in seconds, after which settings are applied. |
| 48 |
- This option is applicable only for Chassis. |
| 49 |
type: int |
| 50 |
default: 0 |
| 51 |
ipv4_configuration: |
| 52 |
description: |
| 53 |
- IPv4 network configuration. |
| 54 |
- "C(WARNING) Ensure that you have an alternate interface to access |
| 55 |
OpenManage Enterprise Modular because these options can change the |
| 56 |
current IPv4 address for I(hostname)." |
| 57 |
type: dict |
| 58 |
suboptions: |
| 59 |
enable_ipv4: |
| 60 |
description: |
| 61 |
- Enable or disable access to the network using IPv4. |
| 62 |
type: bool |
| 63 |
required: true |
| 64 |
enable_dhcp: |
| 65 |
description: |
| 66 |
- "Enable or disable the automatic request to obtain an IPv4 address |
| 67 |
from the IPv4 Dynamic Host Configuration Protocol (DHCP) server." |
| 68 |
- "C(NOTE) If this option is C(true), the values provided for |
| 69 |
I(static_ip_address), I(static_subnet_mask), and I(static_gateway) |
| 70 |
are not applied for these fields. However, the module may report |
| 71 |
changes." |
| 72 |
type: bool |
| 73 |
static_ip_address: |
| 74 |
description: |
| 75 |
- Static IPv4 address. |
| 76 |
- This option is applicable when I(enable_dhcp) is false. |
| 77 |
type: str |
| 78 |
static_subnet_mask: |
| 79 |
description: |
| 80 |
- Static IPv4 subnet mask address. |
| 81 |
- This option is applicable when I(enable_dhcp) is false. |
| 82 |
type: str |
| 83 |
static_gateway: |
| 84 |
description: |
| 85 |
- Static IPv4 gateway address. |
| 86 |
- This option is applicable when I(enable_dhcp) is false. |
| 87 |
type: str |
| 88 |
use_dhcp_to_obtain_dns_server_address: |
| 89 |
description: |
| 90 |
- This option allows to automatically request and obtain IPv4 address |
| 91 |
for the DNS Server from the DHCP server. |
| 92 |
- This option is applicable when I(enable_dhcp) is true. |
| 93 |
- "C(NOTE) If this option is C(true), the values provided for |
| 94 |
I(static_preferred_dns_server) and I(static_alternate_dns_server) |
| 95 |
are not applied for these fields. However, the module may report |
| 96 |
changes." |
| 97 |
type: bool |
| 98 |
static_preferred_dns_server: |
| 99 |
description: |
| 100 |
- Static IPv4 DNS preferred server. |
| 101 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is |
| 102 |
false. |
| 103 |
type: str |
| 104 |
static_alternate_dns_server: |
| 105 |
description: |
| 106 |
- Static IPv4 DNS alternate server. |
| 107 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is |
| 108 |
false. |
| 109 |
type: str |
| 110 |
ipv6_configuration: |
| 111 |
description: |
| 112 |
- IPv6 network configuration. |
| 113 |
- "C(WARNING) Ensure that you have an alternate interface to access |
| 114 |
OpenManage Enterprise Modular because these options can change the |
| 115 |
current IPv6 address for I(hostname)." |
| 116 |
type: dict |
| 117 |
suboptions: |
| 118 |
enable_ipv6: |
| 119 |
description: Enable or disable access to the network using the IPv6. |
| 120 |
type: bool |
| 121 |
required: true |
| 122 |
enable_auto_configuration: |
| 123 |
description: |
| 124 |
- "Enable or disable the automatic request to obtain an IPv6 address |
| 125 |
from the IPv6 DHCP server or router advertisements(RA)." |
| 126 |
- "If I(enable_auto_configuration) is C(true), OpenManage Enterprise |
| 127 |
Modular retrieves IP configuration (IPv6 address, prefix, and |
| 128 |
gateway address) from a DHCPv6 server on the existing network." |
| 129 |
- "C(NOTE) If this option is C(true), the values provided for |
| 130 |
I(static_ip_address), I(static_prefix_length), and |
| 131 |
I(static_gateway) are not applied for these fields. However, the |
| 132 |
module may report changes." |
| 133 |
type: bool |
| 134 |
static_ip_address: |
| 135 |
description: |
| 136 |
- Static IPv6 address. |
| 137 |
- This option is applicable when I(enable_auto_configuration) is |
| 138 |
false. |
| 139 |
type: str |
| 140 |
static_prefix_length: |
| 141 |
description: |
| 142 |
- Static IPv6 prefix length. |
| 143 |
- This option is applicable when I(enable_auto_configuration) is |
| 144 |
false. |
| 145 |
type: int |
| 146 |
static_gateway: |
| 147 |
description: |
| 148 |
- Static IPv6 gateway address. |
| 149 |
- This option is applicable when I(enable_auto_configuration) is |
| 150 |
false. |
| 151 |
type: str |
| 152 |
use_dhcpv6_to_obtain_dns_server_address: |
| 153 |
description: |
| 154 |
- This option allows to automatically request and obtain a IPv6 |
| 155 |
address for the DNS server from the DHCP server. |
| 156 |
- This option is applicable when I(enable_auto_configuration) is true. |
| 157 |
- "C(NOTE) If this option is C(true), the values provided for |
| 158 |
I(static_preferred_dns_server) and I(static_alternate_dns_server) |
| 159 |
are not applied for these fields. However, the module may report |
| 160 |
changes." |
| 161 |
type: bool |
| 162 |
static_preferred_dns_server: |
| 163 |
description: |
| 164 |
- Static IPv6 DNS preferred server. |
| 165 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is |
| 166 |
false. |
| 167 |
type: str |
| 168 |
static_alternate_dns_server: |
| 169 |
description: |
| 170 |
- Static IPv6 DNS alternate server. |
| 171 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is |
| 172 |
false. |
| 173 |
type: str |
| 174 |
management_vlan: |
| 175 |
description: |
| 176 |
- VLAN configuration. |
| 177 |
type: dict |
| 178 |
suboptions: |
| 179 |
enable_vlan: |
| 180 |
description: |
| 181 |
- Enable or disable VLAN for management. |
| 182 |
- The VLAN configuration cannot be updated if the I(register_with_dns) |
| 183 |
field under I(dns_configuration) is true. |
| 184 |
- "C(WARNING) Ensure that the network cable is connected to the |
| 185 |
correct port after the VLAN configuration is changed. If not, the |
| 186 |
VLAN configuration changes may not be applied." |
| 187 |
required: true |
| 188 |
type: bool |
| 189 |
vlan_id: |
| 190 |
description: |
| 191 |
- VLAN ID. |
| 192 |
- "The valid VLAN IDs are: 1 to 4000, and 4021 to 4094." |
| 193 |
- This option is applicable when I(enable_vlan) is true. |
| 194 |
type: int |
| 195 |
dns_configuration: |
| 196 |
description: Domain Name System(DNS) settings. |
| 197 |
type: dict |
| 198 |
suboptions: |
| 199 |
register_with_dns: |
| 200 |
description: |
| 201 |
- Register/Unregister I(dns_name) on the DNS Server. |
| 202 |
- C(WARNING) This option cannot be updated if VLAN configuration |
| 203 |
changes. |
| 204 |
type: bool |
| 205 |
use_dhcp_for_dns_domain_name: |
| 206 |
description: Get the I(dns_domain_name) using a DHCP server. |
| 207 |
type: bool |
| 208 |
dns_name: |
| 209 |
description: |
| 210 |
- DNS name for I(hostname). |
| 211 |
- This is applicable when I(register_with_dns) is true. |
| 212 |
type: str |
| 213 |
dns_domain_name: |
| 214 |
description: |
| 215 |
- Static DNS domain name. |
| 216 |
- This is applicable when I(use_dhcp_for_dns_domain_name) is false. |
| 217 |
type: str |
| 218 |
auto_negotiation: |
| 219 |
description: |
| 220 |
- Enables or disables the auto negation of the network speed. |
| 221 |
- "C(NOTE): Setting I(auto_negotiation) to false and choosing a |
| 222 |
network port speed may result in the chassis loosing link to the top |
| 223 |
of rack network switch, or to the neighboring chassis in case of MCM |
| 224 |
mode. It is recommended that the I(auto_negotiation) is set to |
| 225 |
C(true) for most use cases." |
| 226 |
- This is applicable when I(use_dhcp_for_dns_domain_name) is false. |
| 227 |
- This is applicable only for Chassis. |
| 228 |
type: bool |
| 229 |
network_speed: |
| 230 |
description: |
| 231 |
- The speed of the network port. |
| 232 |
- This is applicable when I(auto_negotiation) is false. |
| 233 |
- C(10_MB) to select network speed of 10 MB. |
| 234 |
- C(100_MB) to select network speed of 100 MB. |
| 235 |
- This is applicable only for Chassis. |
| 236 |
choices: |
| 237 |
- 10_MB |
| 238 |
- 100_MB |
| 239 |
type: str |
| 240 |
dns_server_settings: |
| 241 |
description: |
| 242 |
- DNS server settings. |
| 243 |
- This is applicable only for I/O Module. |
| 244 |
type: dict |
| 245 |
suboptions: |
| 246 |
preferred_dns_server: |
| 247 |
description: |
| 248 |
- Enter the IP address of the preferred DNS server. |
| 249 |
type: str |
| 250 |
alternate_dns_server1: |
| 251 |
description: |
| 252 |
- Enter the IP address of the first alternate DNS server. |
| 253 |
type: str |
| 254 |
alternate_dns_server2: |
| 255 |
description: |
| 256 |
- Enter the IP address of the second alternate DNS server. |
| 257 |
type: str |
| 258 |
requirements: |
| 259 |
- "python >= 3.9.6" |
| 260 |
notes: |
| 261 |
- Run this module from a system that has direct access to Dell OpenManage |
| 262 |
Enterprise Modular. |
| 263 |
- This module supports C(check_mode). |
| 264 |
""" |
| 265 |
|
| 266 |
EXAMPLES = """ |
| 267 |
--- |
| 268 |
- name: Network settings for chassis |
| 269 |
dellemc.openmanage.ome_device_mgmt_network: |
| 270 |
hostname: 192.168.0.1 |
| 271 |
username: "username" |
| 272 |
password: "password" |
| 273 |
ca_path: "/path/to/ca_cert.pem" |
| 274 |
device_service_tag: CHAS123 |
| 275 |
ipv4_configuration: |
| 276 |
enable_ipv4: true |
| 277 |
enable_dhcp: false |
| 278 |
static_ip_address: 192.168.0.2 |
| 279 |
static_subnet_mask: 255.255.254.0 |
| 280 |
static_gateway: 192.168.0.3 |
| 281 |
use_dhcp_to_obtain_dns_server_address: false |
| 282 |
static_preferred_dns_server: 192.168.0.4 |
| 283 |
static_alternate_dns_server: 192.168.0.5 |
| 284 |
ipv6_configuration: |
| 285 |
enable_ipv6: true |
| 286 |
enable_auto_configuration: false |
| 287 |
static_ip_address: 2626:f2f2:f081:9:1c1c:f1f1:4747:1 |
| 288 |
static_prefix_length: 10 |
| 289 |
static_gateway: ffff::2607:f2b1:f081:9 |
| 290 |
use_dhcpv6_to_obtain_dns_server_address: false |
| 291 |
static_preferred_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:3 |
| 292 |
static_alternate_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:4 |
| 293 |
dns_configuration: |
| 294 |
register_with_dns: true |
| 295 |
use_dhcp_for_dns_domain_name: false |
| 296 |
dns_name: "MX-SVCTAG" |
| 297 |
dns_domain_name: "dnslocaldomain" |
| 298 |
auto_negotiation: false |
| 299 |
network_speed: 100_MB |
| 300 |
|
| 301 |
- name: Network settings for server |
| 302 |
dellemc.openmanage.ome_device_mgmt_network: |
| 303 |
hostname: 192.168.0.1 |
| 304 |
username: "username" |
| 305 |
password: "password" |
| 306 |
ca_path: "/path/to/ca_cert.pem" |
| 307 |
device_service_tag: SRVR123 |
| 308 |
ipv4_configuration: |
| 309 |
enable_ipv4: true |
| 310 |
enable_dhcp: false |
| 311 |
static_ip_address: 192.168.0.2 |
| 312 |
static_subnet_mask: 255.255.254.0 |
| 313 |
static_gateway: 192.168.0.3 |
| 314 |
use_dhcp_to_obtain_dns_server_address: false |
| 315 |
static_preferred_dns_server: 192.168.0.4 |
| 316 |
static_alternate_dns_server: 192.168.0.5 |
| 317 |
ipv6_configuration: |
| 318 |
enable_ipv6: true |
| 319 |
enable_auto_configuration: false |
| 320 |
static_ip_address: 2626:f2f2:f081:9:1c1c:f1f1:4747:1 |
| 321 |
static_prefix_length: 10 |
| 322 |
static_gateway: ffff::2607:f2b1:f081:9 |
| 323 |
use_dhcpv6_to_obtain_dns_server_address: false |
| 324 |
static_preferred_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:3 |
| 325 |
static_alternate_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:4 |
| 326 |
|
| 327 |
- name: Network settings for I/O module |
| 328 |
dellemc.openmanage.ome_device_mgmt_network: |
| 329 |
hostname: 192.168.0.1 |
| 330 |
username: "username" |
| 331 |
password: "password" |
| 332 |
ca_path: "/path/to/ca_cert.pem" |
| 333 |
device_service_tag: IOM1234 |
| 334 |
ipv4_configuration: |
| 335 |
enable_ipv4: true |
| 336 |
enable_dhcp: false |
| 337 |
static_ip_address: 192.168.0.2 |
| 338 |
static_subnet_mask: 255.255.254.0 |
| 339 |
static_gateway: 192.168.0.3 |
| 340 |
ipv6_configuration: |
| 341 |
enable_ipv6: true |
| 342 |
enable_auto_configuration: false |
| 343 |
static_ip_address: 2626:f2f2:f081:9:1c1c:f1f1:4747:1 |
| 344 |
static_prefix_length: 10 |
| 345 |
static_gateway: ffff::2607:f2b1:f081:9 |
| 346 |
dns_server_settings: |
| 347 |
preferred_dns_server: 192.168.0.4 |
| 348 |
alternate_dns_server1: 192.168.0.5 |
| 349 |
|
| 350 |
- name: Management VLAN configuration of chassis using device id |
| 351 |
dellemc.openmanage.ome_device_mgmt_network: |
| 352 |
hostname: "192.168.0.1" |
| 353 |
username: "username" |
| 354 |
password: "password" |
| 355 |
ca_path: "/path/to/ca_cert.pem" |
| 356 |
device_id: 12345 |
| 357 |
management_vlan: |
| 358 |
enable_vlan: true |
| 359 |
vlan_id: 2345 |
| 360 |
dns_configuration: |
| 361 |
register_with_dns: false |
| 362 |
""" |
| 363 |
|
| 364 |
RETURN = """ |
| 365 |
--- |
| 366 |
msg: |
| 367 |
type: str |
| 368 |
description: Overall status of the network config operation. |
| 369 |
returned: always |
| 370 |
sample: Successfully applied the network settings. |
| 371 |
error_info: |
| 372 |
description: Details of the HTTP Error. |
| 373 |
returned: on HTTP error |
| 374 |
type: dict |
| 375 |
sample: { |
| 376 |
"error": { |
| 377 |
"code": "Base.1.0.GeneralError", |
| 378 |
"message": "A general error has occurred. See ExtendedInfo for more |
| 379 |
information.", |
| 380 |
"@Message.ExtendedInfo": [ |
| 381 |
{ |
| 382 |
"MessageId": "CGEN1004", |
| 383 |
"RelatedProperties": [], |
| 384 |
"Message": "Unable to complete the request because IPV4 Settings |
| 385 |
Capability is not Supported does not |
| 386 |
exist or is not applicable for the resource URI.", |
| 387 |
"MessageArgs": [ |
| 388 |
"IPV4 Settings Capability is not Supported" |
| 389 |
], |
| 390 |
"Severity": "Critical", |
| 391 |
"Resolution": "Check the request resource URI. Refer to the |
| 392 |
OpenManage Enterprise-Modular User's Guide for |
| 393 |
more information about resource URI and its |
| 394 |
properties." |
| 395 |
} |
| 396 |
] |
| 397 |
} |
| 398 |
} |
| 399 |
""" |
| 400 |
|
| 401 |
import json |
| 402 |
import socket |
| 403 |
import copy |
| 404 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 405 |
from ansible.module_utils.urls import ConnectionError |
| 406 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import ( |
| 407 |
RestOME, OmeAnsibleModule) |
| 408 |
from ansible.module_utils.common.dict_transformations import recursive_diff |
| 409 |
|
| 410 |
DEVICE_URI = "DeviceService/Devices" |
| 411 |
MGMT_DOMAIN = "ManagementDomainService/Domains" |
| 412 |
LEAD_CONFIG = "ApplicationService/Network/AddressConfiguration" |
| 413 |
NETWORK_SETTINGS = "DeviceService/Devices({0})/Settings('Network')" |
| 414 |
DEVICE_NOT_FOUND = "Device with {0} '{1}' not found." |
| 415 |
NON_CONFIG_NETWORK = "Network settings for {0} is not configurable." |
| 416 |
SUCCESS_MSG = "Successfully applied the network settings." |
| 417 |
INVALID_IP = "Invalid {0} address provided for the {1}" |
| 418 |
DNS_SETT_ERR1 = "'SecondaryDNS' requires 'PrimaryDNS' to be provided." |
| 419 |
DNS_SETT_ERR2 = "'TertiaryDNS' requires both 'PrimaryDNS' and 'SecondaryDNS' to be provided." |
| 420 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 421 |
CHANGES_FOUND = "Changes found to be applied." |
| 422 |
SERVER = 1000 |
| 423 |
CHASSIS = 2000 |
| 424 |
IO_MODULE = 4000 |
| 425 |
API_TIMEOUT = 120 |
| 426 |
|
| 427 |
|
| 428 |
def validate_ip_address(address): |
| 429 |
try: |
| 430 |
socket.inet_aton(address) |
| 431 |
except socket.error: |
| 432 |
return False |
| 433 |
return address.count('.') == 3 |
| 434 |
|
| 435 |
|
| 436 |
def validate_ip_v6_address(address): |
| 437 |
try: |
| 438 |
socket.inet_pton(socket.AF_INET6, address) |
| 439 |
except socket.error: |
| 440 |
return False |
| 441 |
return True |
| 442 |
|
| 443 |
|
| 444 |
def validate_ipaddress(module, ip_type, config, var_list, ip_func): |
| 445 |
ipv_input = module.params.get(config) |
| 446 |
if ipv_input: |
| 447 |
for ipname in var_list: |
| 448 |
val = ipv_input.get(ipname) |
| 449 |
if val and not ip_func(val): |
| 450 |
module.exit_json(msg=INVALID_IP.format(ip_type, ipname), |
| 451 |
failed=True) |
| 452 |
|
| 453 |
|
| 454 |
def validate_input(module): |
| 455 |
ip_addr = ["static_ip_address", "static_gateway", |
| 456 |
"static_preferred_dns_server", "static_alternate_dns_server"] |
| 457 |
validate_ipaddress(module, "IPv6", "ipv6_configuration", ip_addr, |
| 458 |
validate_ip_v6_address) |
| 459 |
ip_addr.append("static_subnet_mask") |
| 460 |
validate_ipaddress(module, "IPv4", "ipv4_configuration", ip_addr, |
| 461 |
validate_ip_address) |
| 462 |
ipv6 = module.params.get("ipv6_configuration") |
| 463 |
dns_settings = module.params.get("dns_server_settings") |
| 464 |
if dns_settings: |
| 465 |
for k, v in dns_settings.items(): |
| 466 |
if v is not None: |
| 467 |
if not validate_ip_address(v) and not validate_ip_v6_address(v): |
| 468 |
module.exit_json(msg=INVALID_IP.format("IP", k), |
| 469 |
failed=True) |
| 470 |
|
| 471 |
if ipv6 and ipv6.get("static_prefix_length"): |
| 472 |
ipv6["static_prefix_length"] = str(ipv6["static_prefix_length"]) |
| 473 |
vlan = module.params.get("management_vlan") |
| 474 |
if vlan and vlan.get("vlan_id"): |
| 475 |
vlan["vlan_id"] = str(vlan["vlan_id"]) |
| 476 |
|
| 477 |
|
| 478 |
def get_device_details(module, rest_obj): |
| 479 |
device_id = module.params.get('device_id') |
| 480 |
srch = 'Id' |
| 481 |
query_param = {"$filter": "{0} eq {1}".format(srch, device_id)} |
| 482 |
if not device_id: |
| 483 |
device_id = module.params.get('device_service_tag') |
| 484 |
srch = 'Identifier' |
| 485 |
query_param = {"$filter": "{0} eq '{1}'".format(srch, device_id)} |
| 486 |
resp = rest_obj.invoke_request('GET', DEVICE_URI, query_param=query_param) |
| 487 |
if resp.success and resp.json_data.get('value'): |
| 488 |
tlist = resp.json_data.get('value', []) |
| 489 |
for xtype in tlist: |
| 490 |
if xtype.get(srch) == device_id: |
| 491 |
dvc = xtype |
| 492 |
return dvc |
| 493 |
module.exit_json(msg=DEVICE_NOT_FOUND.format(srch, device_id), failed=True) |
| 494 |
|
| 495 |
|
| 496 |
def transform_diff(params, translator, sub_payload, bool_trans=None): |
| 497 |
df = {} |
| 498 |
inp_dict = {} |
| 499 |
for k, v in translator.items(): |
| 500 |
inp = params.get(k) |
| 501 |
if inp is not None: |
| 502 |
if isinstance(inp, bool) and bool_trans: |
| 503 |
inp = bool_trans.get(inp) |
| 504 |
inp_dict[v] = inp |
| 505 |
id_diff = recursive_diff(inp_dict, sub_payload) |
| 506 |
if id_diff and id_diff[0]: |
| 507 |
df = id_diff[0] |
| 508 |
sub_payload.update(inp_dict) |
| 509 |
return df |
| 510 |
|
| 511 |
|
| 512 |
def validate_dependency_ipv4(ipv4): |
| 513 |
remove_list = [] |
| 514 |
dhcp = ["static_preferred_dns_server", "static_alternate_dns_server"] |
| 515 |
static = ["static_ip_address", "static_gateway", "static_subnet_mask"] |
| 516 |
bools = ["enable_dhcp", "use_dhcp_to_obtain_dns_server_address"] |
| 517 |
if ipv4.get("use_dhcp_to_obtain_dns_server_address") is True: |
| 518 |
remove_list.extend(dhcp) |
| 519 |
if ipv4.get("enable_dhcp") is True: |
| 520 |
remove_list.extend(static) |
| 521 |
if ipv4.get("enable_ipv4") is False: |
| 522 |
remove_list.extend(dhcp) |
| 523 |
remove_list.extend(static) |
| 524 |
remove_list.extend(bools) |
| 525 |
for item in remove_list: |
| 526 |
ipv4.pop(item, None) |
| 527 |
|
| 528 |
|
| 529 |
def validate_dependency_ipv6(ipv6): |
| 530 |
removev6_list = [] |
| 531 |
dhcp = ["static_preferred_dns_server", "static_alternate_dns_server"] |
| 532 |
static = ["static_ip_address", "static_gateway", "static_prefix_length"] |
| 533 |
bools = ["enable_auto_configuration", |
| 534 |
"use_dhcpv6_to_obtain_dns_server_address"] |
| 535 |
if ipv6.get("use_dhcpv6_to_obtain_dns_server_address") is True: |
| 536 |
removev6_list.extend(dhcp) |
| 537 |
if ipv6.get("enable_auto_configuration") is True: |
| 538 |
removev6_list.extend(static) |
| 539 |
if ipv6.get("enable_ipv6") is False: |
| 540 |
removev6_list.extend(dhcp) |
| 541 |
removev6_list.extend(static) |
| 542 |
removev6_list.extend(bools) |
| 543 |
for itemv6 in removev6_list: |
| 544 |
ipv6.pop(itemv6, None) |
| 545 |
|
| 546 |
|
| 547 |
def validate_dependency(mparams): |
| 548 |
params = copy.deepcopy(mparams) |
| 549 |
ipv4 = params.get('ipv4_configuration') |
| 550 |
if ipv4: |
| 551 |
validate_dependency_ipv4(ipv4) |
| 552 |
ipv6 = params.get('ipv6_configuration') |
| 553 |
if ipv6: |
| 554 |
validate_dependency_ipv6(ipv6) |
| 555 |
vlan = params.get('management_vlan') |
| 556 |
if vlan and vlan.get('enable_vlan') is False: |
| 557 |
vlan.pop('vlan_id', None) |
| 558 |
dns = params.get('dns_configuration') |
| 559 |
if dns: |
| 560 |
if dns.get('auto_negotiation') is True: |
| 561 |
dns.pop('network_speed', None) |
| 562 |
if dns.get('use_dhcp_for_dns_domain_name') is True: |
| 563 |
dns.pop('dns_domain_name', None) |
| 564 |
return params |
| 565 |
|
| 566 |
|
| 567 |
def update_chassis_payload(module, payload): |
| 568 |
ipv4 = { |
| 569 |
"enable_dhcp": "EnableDHCP", |
| 570 |
"enable_ipv4": "EnableIPv4", |
| 571 |
"static_alternate_dns_server": "StaticAlternateDNSServer", |
| 572 |
"static_gateway": "StaticGateway", |
| 573 |
"static_ip_address": "StaticIPAddress", |
| 574 |
"static_preferred_dns_server": "StaticPreferredDNSServer", |
| 575 |
"static_subnet_mask": "StaticSubnetMask", |
| 576 |
"use_dhcp_to_obtain_dns_server_address": "UseDHCPObtainDNSServerAddresses" |
| 577 |
} |
| 578 |
ipv6 = { |
| 579 |
"enable_auto_configuration": "EnableAutoconfiguration", |
| 580 |
"enable_ipv6": "EnableIPv6", |
| 581 |
"static_alternate_dns_server": "StaticAlternateDNSServer", |
| 582 |
"static_gateway": "StaticGateway", |
| 583 |
"static_ip_address": "StaticIPv6Address", |
| 584 |
"static_preferred_dns_server": "StaticPreferredDNSServer", |
| 585 |
"static_prefix_length": "StaticPrefixLength", |
| 586 |
"use_dhcpv6_to_obtain_dns_server_address": "UseDHCPv6ObtainDNSServerAddresses" |
| 587 |
} |
| 588 |
dns = { |
| 589 |
"auto_negotiation": "AutoNegotiation", |
| 590 |
"dns_domain_name": "DnsDomainName", |
| 591 |
"dns_name": "DnsName", |
| 592 |
"network_speed": "NetworkSpeed", |
| 593 |
"register_with_dns": "RegisterDNS", |
| 594 |
"use_dhcp_for_dns_domain_name": "UseDHCPForDomainName" |
| 595 |
} |
| 596 |
vlan = {"enable_vlan": "EnableVLAN", "vlan_id": "MgmtVLANId"} |
| 597 |
gnrl = payload.get('GeneralSettings') |
| 598 |
diff = {} |
| 599 |
mparams = validate_dependency(module.params) |
| 600 |
enable_nic = mparams.get('enable_nic') |
| 601 |
delay = mparams.get('delay') |
| 602 |
if enable_nic: |
| 603 |
if mparams.get('ipv4_configuration'): |
| 604 |
df = transform_diff(mparams.get('ipv4_configuration'), ipv4, |
| 605 |
payload.get('Ipv4Settings')) |
| 606 |
diff.update(df) |
| 607 |
if mparams.get('ipv6_configuration'): |
| 608 |
df = transform_diff(mparams.get('ipv6_configuration'), ipv6, |
| 609 |
payload.get('Ipv6Settings')) |
| 610 |
diff.update(df) |
| 611 |
if mparams.get('dns_configuration'): |
| 612 |
df = transform_diff(mparams.get('dns_configuration'), dns, |
| 613 |
payload.get('GeneralSettings')) |
| 614 |
diff.update(df) |
| 615 |
if mparams.get('management_vlan'): |
| 616 |
df = transform_diff(mparams.get('management_vlan'), vlan, payload) |
| 617 |
diff.update(df) |
| 618 |
if gnrl.get('EnableNIC') != enable_nic: |
| 619 |
gnrl['EnableNIC'] = enable_nic |
| 620 |
diff.update({'EnableNIC': enable_nic}) |
| 621 |
if delay != gnrl.get('Delay'): |
| 622 |
gnrl['Delay'] = delay |
| 623 |
diff.update({'Delay': delay}) |
| 624 |
return diff |
| 625 |
|
| 626 |
|
| 627 |
def update_server_payload(module, payload): |
| 628 |
ipv4 = { |
| 629 |
"enable_dhcp": "enableDHCPIPv4", |
| 630 |
"enable_ipv4": "enableIPv4", |
| 631 |
"static_alternate_dns_server": "staticAlternateDNSIPv4", |
| 632 |
"static_gateway": "staticGatewayIPv4", |
| 633 |
"static_ip_address": "staticIPAddressIPv4", |
| 634 |
"static_preferred_dns_server": "staticPreferredDNSIPv4", |
| 635 |
"static_subnet_mask": "staticSubnetMaskIPv4", |
| 636 |
"use_dhcp_to_obtain_dns_server_address": "useDHCPToObtainDNSIPv4" |
| 637 |
} |
| 638 |
ipv6 = { |
| 639 |
"enable_auto_configuration": "enableAutoConfigurationIPv6", |
| 640 |
"enable_ipv6": "enableIPv6", |
| 641 |
"static_alternate_dns_server": "staticAlternateDNSIPv6", |
| 642 |
"static_gateway": "staticGatewayIPv6", |
| 643 |
"static_ip_address": "staticIPAddressIPv6", |
| 644 |
"static_preferred_dns_server": "staticPreferredDNSIPv6", |
| 645 |
"static_prefix_length": "staticPrefixLengthIPv6", |
| 646 |
"use_dhcpv6_to_obtain_dns_server_address": "useDHCPToObtainDNSIPv6" |
| 647 |
} |
| 648 |
vlan = {"enable_vlan": "vlanEnable", "vlan_id": "vlanId"} |
| 649 |
diff = {} |
| 650 |
mparams = validate_dependency(module.params) |
| 651 |
enable_nic = mparams.get('enable_nic') |
| 652 |
bool_trans = {True: 'Enabled', False: 'Disabled'} |
| 653 |
if enable_nic: |
| 654 |
if mparams.get('ipv4_configuration'): |
| 655 |
df = transform_diff(mparams.get('ipv4_configuration'), ipv4, |
| 656 |
payload, bool_trans) |
| 657 |
diff.update(df) |
| 658 |
if mparams.get('ipv6_configuration'): |
| 659 |
df = transform_diff(mparams.get('ipv6_configuration'), ipv6, |
| 660 |
payload, bool_trans) |
| 661 |
diff.update(df) |
| 662 |
if mparams.get('management_vlan'): |
| 663 |
df = transform_diff(mparams.get('management_vlan'), vlan, |
| 664 |
payload, bool_trans) |
| 665 |
diff.update(df) |
| 666 |
enable_nic = bool_trans.get(enable_nic) |
| 667 |
if payload.get('enableNIC') != enable_nic: |
| 668 |
payload['enableNIC'] = enable_nic |
| 669 |
diff.update({'enableNIC': enable_nic}) |
| 670 |
return diff |
| 671 |
|
| 672 |
|
| 673 |
def update_iom_payload(module, payload): |
| 674 |
ipv4 = { |
| 675 |
"enable_dhcp": "EnableDHCP", |
| 676 |
"enable_ipv4": "EnableIPv4", |
| 677 |
"static_gateway": "StaticGateway", |
| 678 |
"static_ip_address": "StaticIPAddress", |
| 679 |
"static_subnet_mask": "StaticSubnetMask", |
| 680 |
} |
| 681 |
ipv6 = { |
| 682 |
"enable_ipv6": "EnableIPv6", |
| 683 |
"static_gateway": "StaticGateway", |
| 684 |
"static_ip_address": "StaticIPv6Address", |
| 685 |
"static_prefix_length": "StaticPrefixLength", |
| 686 |
"enable_auto_configuration": "UseDHCPv6" |
| 687 |
} |
| 688 |
dns = {"preferred_dns_server": "PrimaryDNS", |
| 689 |
"alternate_dns_server1": "SecondaryDNS", |
| 690 |
"alternate_dns_server2": "TertiaryDNS"} |
| 691 |
vlan = {"enable_vlan": "EnableMgmtVLANId", "vlan_id": "MgmtVLANId"} |
| 692 |
diff = {} |
| 693 |
mparams = validate_dependency(module.params) |
| 694 |
if mparams.get('ipv4_configuration'): |
| 695 |
df = transform_diff(mparams.get('ipv4_configuration'), ipv4, |
| 696 |
payload.get('IomIPv4Settings')) |
| 697 |
diff.update(df) |
| 698 |
if mparams.get('ipv6_configuration'): |
| 699 |
df = transform_diff(mparams.get('ipv6_configuration'), ipv6, |
| 700 |
payload.get('IomIPv6Settings')) |
| 701 |
diff.update(df) |
| 702 |
if mparams.get('management_vlan'): |
| 703 |
df = transform_diff(mparams.get('management_vlan'), vlan, payload) |
| 704 |
diff.update(df) |
| 705 |
if mparams.get('dns_server_settings'): |
| 706 |
df = transform_diff(mparams.get('dns_server_settings'), dns, |
| 707 |
payload.get('IomDNSSettings')) |
| 708 |
dns_iom = payload.get('IomDNSSettings') |
| 709 |
if dns_iom.get("SecondaryDNS") and not dns_iom.get("PrimaryDNS"): |
| 710 |
module.exit_json(msg=DNS_SETT_ERR1, failed=True) |
| 711 |
if dns_iom.get("TertiaryDNS") and (not dns_iom.get("PrimaryDNS") or not dns_iom.get("SecondaryDNS")): |
| 712 |
module.exit_json(msg=DNS_SETT_ERR2, failed=True) |
| 713 |
diff.update(df) |
| 714 |
return diff |
| 715 |
|
| 716 |
|
| 717 |
def get_network_payload(module, rest_obj, dvc): |
| 718 |
resp = rest_obj.invoke_request('GET', |
| 719 |
NETWORK_SETTINGS.format(dvc.get('Id'))) |
| 720 |
got_payload = resp.json_data |
| 721 |
payload = rest_obj.strip_substr_dict(got_payload) |
| 722 |
update_dict = { |
| 723 |
CHASSIS: update_chassis_payload, |
| 724 |
SERVER: update_server_payload, |
| 725 |
IO_MODULE: update_iom_payload |
| 726 |
} |
| 727 |
diff = update_dict[dvc.get('Type')](module, payload) |
| 728 |
|
| 729 |
if not diff: |
| 730 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 731 |
if module.check_mode: |
| 732 |
module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 733 |
return payload |
| 734 |
|
| 735 |
|
| 736 |
def main(): |
| 737 |
ipv4_options = {"enable_ipv4": {"required": True, "type": 'bool'}, |
| 738 |
"enable_dhcp": {"type": 'bool'}, |
| 739 |
"static_ip_address": {"type": 'str'}, |
| 740 |
"static_subnet_mask": {"type": 'str'}, |
| 741 |
"static_gateway": {"type": 'str'}, |
| 742 |
"use_dhcp_to_obtain_dns_server_address": {"type": 'bool'}, |
| 743 |
"static_preferred_dns_server": {"type": 'str'}, |
| 744 |
"static_alternate_dns_server": {"type": 'str'}} |
| 745 |
ipv6_options = {"enable_ipv6": {"required": True, "type": 'bool'}, |
| 746 |
"enable_auto_configuration": {"type": 'bool'}, |
| 747 |
"static_ip_address": {"type": 'str'}, |
| 748 |
"static_prefix_length": {"type": 'int'}, |
| 749 |
"static_gateway": {"type": 'str'}, |
| 750 |
"use_dhcpv6_to_obtain_dns_server_address": {"type": 'bool'}, |
| 751 |
"static_preferred_dns_server": {"type": 'str'}, |
| 752 |
"static_alternate_dns_server": {"type": 'str'}} |
| 753 |
dns_options = {"register_with_dns": {"type": 'bool'}, |
| 754 |
"use_dhcp_for_dns_domain_name": {"type": 'bool'}, |
| 755 |
"dns_name": {"type": 'str'}, |
| 756 |
"dns_domain_name": {"type": 'str'}, |
| 757 |
"auto_negotiation": {"type": 'bool'}, |
| 758 |
"network_speed": {"type": 'str', |
| 759 |
"choices": ['10_MB', '100_MB']}} |
| 760 |
management_vlan = {"enable_vlan": {"required": True, "type": 'bool'}, |
| 761 |
"vlan_id": {"type": 'int'}} |
| 762 |
dns_server_settings = {"preferred_dns_server": {"type": 'str'}, |
| 763 |
"alternate_dns_server1": {"type": 'str'}, |
| 764 |
"alternate_dns_server2": {"type": 'str'}} |
| 765 |
specs = { |
| 766 |
"enable_nic": {"type": 'bool', "default": True}, |
| 767 |
"device_id": {"type": 'int'}, |
| 768 |
"device_service_tag": {"type": 'str'}, |
| 769 |
"delay": {"type": 'int', "default": 0}, |
| 770 |
"ipv4_configuration": |
| 771 |
{"type": "dict", "options": ipv4_options, |
| 772 |
"required_if": [ |
| 773 |
['enable_ipv4', True, ('enable_dhcp',), True], |
| 774 |
['enable_dhcp', False, |
| 775 |
('static_ip_address', 'static_subnet_mask', "static_gateway"), |
| 776 |
False], |
| 777 |
['use_dhcp_to_obtain_dns_server_address', False, |
| 778 |
('static_preferred_dns_server', |
| 779 |
'static_alternate_dns_server'), True]] |
| 780 |
}, |
| 781 |
"ipv6_configuration": |
| 782 |
{"type": "dict", "options": ipv6_options, |
| 783 |
"required_if": [ |
| 784 |
['enable_ipv6', True, ('enable_auto_configuration',), True], |
| 785 |
['enable_auto_configuration', False, |
| 786 |
('static_ip_address', 'static_prefix_length', |
| 787 |
"static_gateway"), False], |
| 788 |
['use_dhcpv6_to_obtain_dns_server_address', False, |
| 789 |
('static_preferred_dns_server', |
| 790 |
'static_alternate_dns_server'), True]] |
| 791 |
}, |
| 792 |
"dns_configuration": |
| 793 |
{"type": "dict", "options": dns_options, |
| 794 |
"required_if": [ |
| 795 |
['register_with_dns', True, ('dns_name',), False], |
| 796 |
['use_dhcp_for_dns_domain_name', False, ('dns_domain_name',)], |
| 797 |
['auto_negotiation', False, ('network_speed',)]] |
| 798 |
}, |
| 799 |
"management_vlan": |
| 800 |
{"type": "dict", "options": management_vlan, |
| 801 |
"required_if": [ |
| 802 |
['enable_vlan', True, ('vlan_id',), True]] |
| 803 |
}, |
| 804 |
"dns_server_settings": |
| 805 |
{"type": "dict", "options": dns_server_settings, |
| 806 |
"required_one_of": [("preferred_dns_server", |
| 807 |
"alternate_dns_server1", |
| 808 |
"alternate_dns_server2")] |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
module = OmeAnsibleModule( |
| 813 |
argument_spec=specs, |
| 814 |
required_one_of=[('device_id', 'device_service_tag')], |
| 815 |
mutually_exclusive=[('device_id', 'device_service_tag')], |
| 816 |
supports_check_mode=True |
| 817 |
) |
| 818 |
try: |
| 819 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 820 |
validate_input(module) |
| 821 |
dvc = get_device_details(module, rest_obj) |
| 822 |
if dvc.get('Type') in [SERVER, CHASSIS, IO_MODULE]: |
| 823 |
nw_setting = get_network_payload(module, rest_obj, dvc) |
| 824 |
resp = rest_obj.invoke_request( |
| 825 |
'PUT', NETWORK_SETTINGS.format(dvc.get('Id')), |
| 826 |
data=nw_setting, api_timeout=API_TIMEOUT) |
| 827 |
module.exit_json(msg=SUCCESS_MSG, |
| 828 |
network_details=resp.json_data, changed=True) |
| 829 |
else: |
| 830 |
module.exit_json( |
| 831 |
msg=NON_CONFIG_NETWORK.format(dvc.get('Model')), |
| 832 |
failed=True) |
| 833 |
except HTTPError as err: |
| 834 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 835 |
except URLError as err: |
| 836 |
module.exit_json(msg=str(err), unreachable=True) |
| 837 |
except (IOError, ValueError, TypeError, ConnectionError, AttributeError, |
| 838 |
IndexError, KeyError, OSError) as err: |
| 839 |
module.exit_json(msg=str(err), failed=True) |
| 840 |
|
| 841 |
|
| 842 |
if __name__ == '__main__': |
| 843 |
main() |