| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
|
| 15 |
__metaclass__ = type |
| 16 |
|
| 17 |
DOCUMENTATION = r''' |
| 18 |
--- |
| 19 |
module: ome_application_network_address |
| 20 |
short_description: Updates the network configuration on OpenManage Enterprise |
| 21 |
version_added: "2.1.0" |
| 22 |
description: |
| 23 |
- This module allows the configuration of a DNS and an IPV4 or IPV6 network on OpenManage Enterprise. |
| 24 |
notes: |
| 25 |
- The configuration changes can only be applied to one interface at a time. |
| 26 |
- The system management consoles might be unreachable for some time after the configuration changes are applied. |
| 27 |
- This module supports C(check_mode). |
| 28 |
extends_documentation_fragment: |
| 29 |
- dellemc.openmanage.ome_auth_options |
| 30 |
options: |
| 31 |
enable_nic: |
| 32 |
description: Enable or disable Network Interface Card (NIC) configuration. |
| 33 |
type: bool |
| 34 |
default: true |
| 35 |
interface_name: |
| 36 |
description: |
| 37 |
- "If there are multiple interfaces, network configuration changes can be applied to a single interface using the |
| 38 |
interface name of the NIC." |
| 39 |
- If this option is not specified, Primary interface is chosen by default. |
| 40 |
type: str |
| 41 |
ipv4_configuration: |
| 42 |
description: |
| 43 |
- IPv4 network configuration. |
| 44 |
- "I(Warning) Ensure that you have an alternate interface to access OpenManage Enterprise as these options can |
| 45 |
change the current IPv4 address for I(hostname)." |
| 46 |
type: dict |
| 47 |
suboptions: |
| 48 |
enable: |
| 49 |
description: |
| 50 |
- Enable or disable access to the network using IPv4. |
| 51 |
type: bool |
| 52 |
required: true |
| 53 |
enable_dhcp: |
| 54 |
description: |
| 55 |
- "Enable or disable the automatic request to get an IPv4 address from the IPv4 Dynamic Host Configuration |
| 56 |
Protocol (DHCP) server" |
| 57 |
- "If I(enable_dhcp) option is true, OpenManage Enterprise retrieves the IP configuration—IPv4 address, |
| 58 |
subnet mask, and gateway from a DHCP server on the existing network." |
| 59 |
type: bool |
| 60 |
static_ip_address: |
| 61 |
description: |
| 62 |
- Static IPv4 address |
| 63 |
- This option is applicable when I(enable_dhcp) is false. |
| 64 |
type: str |
| 65 |
static_subnet_mask: |
| 66 |
description: |
| 67 |
- Static IPv4 subnet mask address |
| 68 |
- This option is applicable when I(enable_dhcp) is false. |
| 69 |
type: str |
| 70 |
static_gateway: |
| 71 |
description: |
| 72 |
- Static IPv4 gateway address |
| 73 |
- This option is applicable when I(enable_dhcp) is false. |
| 74 |
type: str |
| 75 |
use_dhcp_for_dns_server_names: |
| 76 |
description: |
| 77 |
- This option allows to automatically request and obtain a DNS server IPv4 address from the DHCP server. |
| 78 |
- This option is applicable when I(enable_dhcp) is true. |
| 79 |
type: bool |
| 80 |
static_preferred_dns_server: |
| 81 |
description: |
| 82 |
- Static IPv4 DNS preferred server |
| 83 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is false. |
| 84 |
type: str |
| 85 |
static_alternate_dns_server: |
| 86 |
description: |
| 87 |
- Static IPv4 DNS alternate server |
| 88 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is false. |
| 89 |
type: str |
| 90 |
ipv6_configuration: |
| 91 |
description: |
| 92 |
- IPv6 network configuration. |
| 93 |
- "I(Warning) Ensure that you have an alternate interface to access OpenManage Enterprise as these options can |
| 94 |
change the current IPv6 address for I(hostname)." |
| 95 |
type: dict |
| 96 |
suboptions: |
| 97 |
enable: |
| 98 |
description: Enable or disable access to the network using the IPv6. |
| 99 |
type: bool |
| 100 |
required: true |
| 101 |
enable_auto_configuration: |
| 102 |
description: |
| 103 |
- "Enable or disable the automatic request to get an IPv6 address from the IPv6 DHCP server or router |
| 104 |
advertisements(RA)" |
| 105 |
- "If I(enable_auto_configuration) is true, OME retrieves IP configuration-IPv6 address, prefix, and gateway, |
| 106 |
from a DHCPv6 server on the existing network" |
| 107 |
type: bool |
| 108 |
static_ip_address: |
| 109 |
description: |
| 110 |
- Static IPv6 address |
| 111 |
- This option is applicable when I(enable_auto_configuration) is false. |
| 112 |
type: str |
| 113 |
static_prefix_length: |
| 114 |
description: |
| 115 |
- Static IPv6 prefix length |
| 116 |
- This option is applicable when I(enable_auto_configuration) is false. |
| 117 |
type: int |
| 118 |
static_gateway: |
| 119 |
description: |
| 120 |
- Static IPv6 gateway address |
| 121 |
- This option is applicable when I(enable_auto_configuration) is false. |
| 122 |
type: str |
| 123 |
use_dhcp_for_dns_server_names: |
| 124 |
description: |
| 125 |
- This option allows to automatically request and obtain a DNS server IPv6 address from the DHCP server. |
| 126 |
- This option is applicable when I(enable_auto_configuration) is true |
| 127 |
type: bool |
| 128 |
static_preferred_dns_server: |
| 129 |
description: |
| 130 |
- Static IPv6 DNS preferred server |
| 131 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is false. |
| 132 |
type: str |
| 133 |
static_alternate_dns_server: |
| 134 |
description: |
| 135 |
- Static IPv6 DNS alternate server |
| 136 |
- This option is applicable when I(use_dhcp_for_dns_server_names) is false. |
| 137 |
type: str |
| 138 |
management_vlan: |
| 139 |
description: |
| 140 |
- vLAN configuration. |
| 141 |
- These settings are applicable for OpenManage Enterprise Modular. |
| 142 |
type: dict |
| 143 |
suboptions: |
| 144 |
enable_vlan: |
| 145 |
description: |
| 146 |
- Enable or disable vLAN for management. |
| 147 |
- The vLAN configuration cannot be updated if the I(register_with_dns) field under I(dns_configuration) is true. |
| 148 |
- "I(WARNING) Ensure that the network cable is plugged to the correct port after the vLAN configuration |
| 149 |
changes have been made. If not, the configuration change may not be effective." |
| 150 |
required: true |
| 151 |
type: bool |
| 152 |
vlan_id: |
| 153 |
description: |
| 154 |
- vLAN ID. |
| 155 |
- This option is applicable when I(enable_vlan) is true. |
| 156 |
type: int |
| 157 |
dns_configuration: |
| 158 |
description: Domain Name System(DNS) settings. |
| 159 |
type: dict |
| 160 |
suboptions: |
| 161 |
register_with_dns: |
| 162 |
description: |
| 163 |
- Register/Unregister I(dns_name) on the DNS Server. |
| 164 |
- This option cannot be updated if vLAN configuration changes. |
| 165 |
type: bool |
| 166 |
use_dhcp_for_dns_domain_name: |
| 167 |
description: Get the I(dns_domain_name) using a DHCP server. |
| 168 |
type: bool |
| 169 |
dns_name: |
| 170 |
description: |
| 171 |
- DNS name for I(hostname) |
| 172 |
- This is applicable when I(register_with_dns) is true. |
| 173 |
type: str |
| 174 |
dns_domain_name: |
| 175 |
description: |
| 176 |
- Static DNS domain name |
| 177 |
- This is applicable when I(use_dhcp_for_dns_domain_name) is false. |
| 178 |
type: str |
| 179 |
reboot_delay: |
| 180 |
description: |
| 181 |
- The time in seconds, after which settings are applied. |
| 182 |
- This option is not mandatory. |
| 183 |
type: int |
| 184 |
requirements: |
| 185 |
- "python >= 3.9.6" |
| 186 |
author: |
| 187 |
- "Jagadeesh N V(@jagadeeshnv)" |
| 188 |
- "Sapana Gupta(@sapana05)" |
| 189 |
''' |
| 190 |
|
| 191 |
EXAMPLES = r''' |
| 192 |
--- |
| 193 |
- name: IPv4 network configuration for primary interface |
| 194 |
dellemc.openmanage.ome_application_network_address: |
| 195 |
hostname: "192.168.0.1" |
| 196 |
username: "username" |
| 197 |
password: "password" |
| 198 |
ca_path: "/path/to/ca_cert.pem" |
| 199 |
enable_nic: true |
| 200 |
ipv4_configuration: |
| 201 |
enable: true |
| 202 |
enable_dhcp: false |
| 203 |
static_ip_address: 192.168.0.2 |
| 204 |
static_subnet_mask: 255.255.254.0 |
| 205 |
static_gateway: 192.168.0.3 |
| 206 |
use_dhcp_for_dns_server_names: false |
| 207 |
static_preferred_dns_server: 192.168.0.4 |
| 208 |
static_alternate_dns_server: 192.168.0.5 |
| 209 |
reboot_delay: 5 |
| 210 |
|
| 211 |
- name: IPv6 network configuration for primary interface |
| 212 |
dellemc.openmanage.ome_application_network_address: |
| 213 |
hostname: "192.168.0.1" |
| 214 |
username: "username" |
| 215 |
password: "password" |
| 216 |
ca_path: "/path/to/ca_cert.pem" |
| 217 |
ipv6_configuration: |
| 218 |
enable: true |
| 219 |
enable_auto_configuration: true |
| 220 |
static_ip_address: 2626:f2f2:f081:9:1c1c:f1f1:4747:1 |
| 221 |
static_prefix_length: 10 |
| 222 |
static_gateway: 2626:f2f2:f081:9:1c1c:f1f1:4747:2 |
| 223 |
use_dhcp_for_dns_server_names: true |
| 224 |
static_preferred_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:3 |
| 225 |
static_alternate_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:4 |
| 226 |
|
| 227 |
- name: Management vLAN configuration for primary interface |
| 228 |
dellemc.openmanage.ome_application_network_address: |
| 229 |
hostname: "192.168.0.1" |
| 230 |
username: "username" |
| 231 |
password: "password" |
| 232 |
ca_path: "/path/to/ca_cert.pem" |
| 233 |
management_vlan: |
| 234 |
enable_vlan: true |
| 235 |
vlan_id: 3344 |
| 236 |
dns_configuration: |
| 237 |
register_with_dns: false |
| 238 |
reboot_delay: 1 |
| 239 |
|
| 240 |
- name: DNS settings |
| 241 |
dellemc.openmanage.ome_application_network_address: |
| 242 |
hostname: "192.168.0.1" |
| 243 |
username: "username" |
| 244 |
password: "password" |
| 245 |
ca_path: "/path/to/ca_cert.pem" |
| 246 |
ipv4_configuration: |
| 247 |
enable: true |
| 248 |
use_dhcp_for_dns_server_names: false |
| 249 |
static_preferred_dns_server: 192.168.0.4 |
| 250 |
static_alternate_dns_server: 192.168.0.5 |
| 251 |
dns_configuration: |
| 252 |
register_with_dns: true |
| 253 |
use_dhcp_for_dns_domain_name: false |
| 254 |
dns_name: "MX-SVCTAG" |
| 255 |
dns_domain_name: "dnslocaldomain" |
| 256 |
|
| 257 |
- name: Disbale nic interface eth1 |
| 258 |
dellemc.openmanage.ome_application_network_address: |
| 259 |
hostname: "192.168.0.1" |
| 260 |
username: "username" |
| 261 |
password: "password" |
| 262 |
ca_path: "/path/to/ca_cert.pem" |
| 263 |
enable_nic: false |
| 264 |
interface_name: eth1 |
| 265 |
|
| 266 |
- name: Complete network settings for interface eth1 |
| 267 |
dellemc.openmanage.ome_application_network_address: |
| 268 |
hostname: "192.168.0.1" |
| 269 |
username: "username" |
| 270 |
password: "password" |
| 271 |
ca_path: "/path/to/ca_cert.pem" |
| 272 |
enable_nic: true |
| 273 |
interface_name: eth1 |
| 274 |
ipv4_configuration: |
| 275 |
enable: true |
| 276 |
enable_dhcp: false |
| 277 |
static_ip_address: 192.168.0.2 |
| 278 |
static_subnet_mask: 255.255.254.0 |
| 279 |
static_gateway: 192.168.0.3 |
| 280 |
use_dhcp_for_dns_server_names: false |
| 281 |
static_preferred_dns_server: 192.168.0.4 |
| 282 |
static_alternate_dns_server: 192.168.0.5 |
| 283 |
ipv6_configuration: |
| 284 |
enable: true |
| 285 |
enable_auto_configuration: true |
| 286 |
static_ip_address: 2626:f2f2:f081:9:1c1c:f1f1:4747:1 |
| 287 |
static_prefix_length: 10 |
| 288 |
static_gateway: ffff::2607:f2b1:f081:9 |
| 289 |
use_dhcp_for_dns_server_names: true |
| 290 |
static_preferred_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:3 |
| 291 |
static_alternate_dns_server: 2626:f2f2:f081:9:1c1c:f1f1:4747:4 |
| 292 |
dns_configuration: |
| 293 |
register_with_dns: true |
| 294 |
use_dhcp_for_dns_domain_name: false |
| 295 |
dns_name: "MX-SVCTAG" |
| 296 |
dns_domain_name: "dnslocaldomain" |
| 297 |
reboot_delay: 5 |
| 298 |
''' |
| 299 |
|
| 300 |
RETURN = r''' |
| 301 |
--- |
| 302 |
msg: |
| 303 |
type: str |
| 304 |
description: Overall status of the network address configuration change. |
| 305 |
returned: always |
| 306 |
sample: Successfully updated network address configuration |
| 307 |
network_configuration: |
| 308 |
type: dict |
| 309 |
description: Updated application network address configuration. |
| 310 |
returned: on success |
| 311 |
sample: { |
| 312 |
"Delay": 0, |
| 313 |
"DnsConfiguration": { |
| 314 |
"DnsDomainName": "", |
| 315 |
"DnsName": "MX-SVCTAG", |
| 316 |
"RegisterWithDNS": false, |
| 317 |
"UseDHCPForDNSDomainName": true |
| 318 |
}, |
| 319 |
"EnableNIC": true, |
| 320 |
"InterfaceName": "eth0", |
| 321 |
"PrimaryInterface": true, |
| 322 |
"Ipv4Configuration": { |
| 323 |
"Enable": true, |
| 324 |
"EnableDHCP": false, |
| 325 |
"StaticAlternateDNSServer": "", |
| 326 |
"StaticGateway": "192.168.0.2", |
| 327 |
"StaticIPAddress": "192.168.0.3", |
| 328 |
"StaticPreferredDNSServer": "192.168.0.4", |
| 329 |
"StaticSubnetMask": "255.255.254.0", |
| 330 |
"UseDHCPForDNSServerNames": false |
| 331 |
}, |
| 332 |
"Ipv6Configuration": { |
| 333 |
"Enable": true, |
| 334 |
"EnableAutoConfiguration": true, |
| 335 |
"StaticAlternateDNSServer": "", |
| 336 |
"StaticGateway": "", |
| 337 |
"StaticIPAddress": "", |
| 338 |
"StaticPreferredDNSServer": "", |
| 339 |
"StaticPrefixLength": 0, |
| 340 |
"UseDHCPForDNSServerNames": true |
| 341 |
}, |
| 342 |
"ManagementVLAN": { |
| 343 |
"EnableVLAN": false, |
| 344 |
"Id": 1 |
| 345 |
} |
| 346 |
} |
| 347 |
job_info: |
| 348 |
description: Details of the job to update in case OME version is >= 3.3. |
| 349 |
returned: on success |
| 350 |
type: dict |
| 351 |
sample: { |
| 352 |
"Builtin": false, |
| 353 |
"CreatedBy": "system", |
| 354 |
"Editable": true, |
| 355 |
"EndTime": null, |
| 356 |
"Id": 14902, |
| 357 |
"JobDescription": "Generic OME runtime task", |
| 358 |
"JobName": "OMERealtime_Task", |
| 359 |
"JobStatus": { |
| 360 |
"Id": 2080, |
| 361 |
"Name": "New" |
| 362 |
}, |
| 363 |
"JobType": { |
| 364 |
"Id": 207, |
| 365 |
"Internal": true, |
| 366 |
"Name": "OMERealtime_Task" |
| 367 |
}, |
| 368 |
"LastRun": null, |
| 369 |
"LastRunStatus": { |
| 370 |
"Id": 2080, |
| 371 |
"Name": "New" |
| 372 |
}, |
| 373 |
"NextRun": null, |
| 374 |
"Params": [ |
| 375 |
{ |
| 376 |
"JobId": 14902, |
| 377 |
"Key": "Nmcli_Update", |
| 378 |
"Value": "{\"interfaceName\":\"eth0\",\"profileName\":\"eth0\",\"enableNIC\":true, |
| 379 |
\"ipv4Configuration\":{\"enable\":true,\"enableDHCP\":true,\"staticIPAddress\":\"\", |
| 380 |
\"staticSubnetMask\":\"\",\"staticGateway\":\"\",\"useDHCPForDNSServerNames\":true, |
| 381 |
\"staticPreferredDNSServer\":\"\",\"staticAlternateDNSServer\":\"\"}, |
| 382 |
\"ipv6Configuration\":{\"enable\":false,\"enableAutoConfiguration\":true,\"staticIPAddress\":\"\", |
| 383 |
\"staticPrefixLength\":0,\"staticGateway\":\"\",\"useDHCPForDNSServerNames\":false, |
| 384 |
\"staticPreferredDNSServer\":\"\",\"staticAlternateDNSServer\":\"\"}, |
| 385 |
\"managementVLAN\":{\"enableVLAN\":false,\"id\":0},\"dnsConfiguration\":{\"registerWithDNS\":false, |
| 386 |
\"dnsName\":\"\",\"useDHCPForDNSDomainName\":false,\"dnsDomainName\":\"\",\"fqdndomainName\":\"\", |
| 387 |
\"ipv4CurrentPreferredDNSServer\":\"\",\"ipv4CurrentAlternateDNSServer\":\"\", |
| 388 |
\"ipv6CurrentPreferredDNSServer\":\"\",\"ipv6CurrentAlternateDNSServer\":\"\"}, |
| 389 |
\"currentSettings\":{\"ipv4Address\":[],\"ipv4Gateway\":\"\",\"ipv4Dns\":[],\"ipv4Domain\":\"\", |
| 390 |
\"ipv6Address\":[],\"ipv6LinkLocalAddress\":\"\",\"ipv6Gateway\":\"\",\"ipv6Dns\":[], |
| 391 |
\"ipv6Domain\":\"\"},\"delay\":0,\"primaryInterface\":true,\"modifiedConfigs\":{}}" |
| 392 |
} |
| 393 |
], |
| 394 |
"Schedule": "startnow", |
| 395 |
"StartTime": null, |
| 396 |
"State": "Enabled", |
| 397 |
"Targets": [], |
| 398 |
"UpdatedBy": null, |
| 399 |
"Visible": true |
| 400 |
} |
| 401 |
error_info: |
| 402 |
description: Details of the HTTP error. |
| 403 |
returned: on HTTP error |
| 404 |
type: dict |
| 405 |
sample: { |
| 406 |
"error": { |
| 407 |
"@Message.ExtendedInfo": [ |
| 408 |
{ |
| 409 |
"Message": "Unable to update the address configuration because a dependent field is missing for Use DHCP |
| 410 |
for DNS Domain Name, Enable DHCP for ipv4 or Enable Autoconfig for ipv6 settings for valid configuration .", |
| 411 |
"MessageArgs": [ |
| 412 |
"Use DHCP for DNS Domain Name, Enable DHCP for ipv4 or Enable Autoconfig for ipv6 settings for valid |
| 413 |
configuration" |
| 414 |
], |
| 415 |
"MessageId": "CAPP1304", |
| 416 |
"RelatedProperties": [], |
| 417 |
"Resolution": "Make sure that all dependent fields contain valid content and retry the operation.", |
| 418 |
"Severity": "Critical" |
| 419 |
} |
| 420 |
], |
| 421 |
"code": "Base.1.0.GeneralError", |
| 422 |
"message": "A general error has occurred. See ExtendedInfo for more information." |
| 423 |
} |
| 424 |
} |
| 425 |
''' |
| 426 |
|
| 427 |
import json |
| 428 |
import socket |
| 429 |
from ssl import SSLError |
| 430 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 431 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 432 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 433 |
|
| 434 |
IP_CONFIG = "ApplicationService/Network/AddressConfiguration" |
| 435 |
JOB_IP_CONFIG = "ApplicationService/Network/AdapterConfigurations" |
| 436 |
POST_IP_CONFIG = "ApplicationService/Actions/Network.ConfigureNetworkAdapter" |
| 437 |
CHANGES_FOUND = "Changes found to be applied." |
| 438 |
NO_CHANGES_FOUND = "No changes found to be applied." |
| 439 |
|
| 440 |
|
| 441 |
def validate_ip_address(address): |
| 442 |
try: |
| 443 |
socket.inet_aton(address) |
| 444 |
except socket.error: |
| 445 |
return False |
| 446 |
return address.count('.') == 3 |
| 447 |
|
| 448 |
|
| 449 |
def validate_ip_v6_address(address): |
| 450 |
try: |
| 451 |
socket.inet_pton(socket.AF_INET6, address) |
| 452 |
except socket.error: |
| 453 |
return False |
| 454 |
return True |
| 455 |
|
| 456 |
|
| 457 |
def remove_unwanted_keys(key_list, payload): |
| 458 |
for key in key_list: |
| 459 |
if key in payload: |
| 460 |
payload.pop(key) |
| 461 |
|
| 462 |
|
| 463 |
def format_payload(src_dict): |
| 464 |
address_payload_map = {"enable_nic": "EnableNIC", |
| 465 |
"interface_name": "InterfaceName", |
| 466 |
"enable": "Enable", |
| 467 |
"enable_dhcp": "EnableDHCP", |
| 468 |
"static_ip_address": "StaticIPAddress", |
| 469 |
"static_subnet_mask": "StaticSubnetMask", |
| 470 |
"static_gateway": "StaticGateway", |
| 471 |
"use_dhcp_for_dns_server_names": "UseDHCPForDNSServerNames", |
| 472 |
"static_preferred_dns_server": "StaticPreferredDNSServer", |
| 473 |
"static_alternate_dns_server": "StaticAlternateDNSServer", |
| 474 |
"enable_auto_configuration": "EnableAutoConfiguration", |
| 475 |
"static_prefix_length": "StaticPrefixLength", |
| 476 |
"enable_vlan": "EnableVLAN", |
| 477 |
"vlan_id": "Id", |
| 478 |
"register_with_dns": "RegisterWithDNS", |
| 479 |
"use_dhcp_for_dns_domain_name": "UseDHCPForDNSDomainName", |
| 480 |
"dns_name": "DnsName", |
| 481 |
"dns_domain_name": "DnsDomainName", |
| 482 |
"reboot_delay": "Delay"} |
| 483 |
if src_dict: |
| 484 |
return dict([(address_payload_map[key], val) for key, val in src_dict.items() if val is not None]) |
| 485 |
|
| 486 |
|
| 487 |
def get_payload(module): |
| 488 |
params = module.params |
| 489 |
backup_params = params.copy() |
| 490 |
remove_keys = ["hostname", "username", "password", "port"] |
| 491 |
remove_unwanted_keys(remove_keys, backup_params) |
| 492 |
ipv4_payload = format_payload(backup_params.get("ipv4_configuration", {})) |
| 493 |
ipv6_payload = format_payload(backup_params.get("ipv6_configuration", {})) |
| 494 |
dns_payload = format_payload(backup_params.get("dns_configuration", {})) |
| 495 |
vlan_payload = format_payload(backup_params.get("management_vlan", {})) |
| 496 |
return ipv4_payload, ipv6_payload, dns_payload, vlan_payload |
| 497 |
|
| 498 |
|
| 499 |
def _compare_dict_merge(src_dict, new_dict, param_list): |
| 500 |
diff = 0 |
| 501 |
for parm in param_list: |
| 502 |
val = new_dict.get(parm) |
| 503 |
if val is not None: |
| 504 |
if val != src_dict.get(parm): |
| 505 |
src_dict[parm] = val |
| 506 |
diff += 1 |
| 507 |
return diff |
| 508 |
|
| 509 |
|
| 510 |
def update_ipv4_payload(src_dict, new_dict): |
| 511 |
diff = 0 |
| 512 |
if new_dict: |
| 513 |
if new_dict.get("Enable") != src_dict.get("Enable"): |
| 514 |
src_dict["Enable"] = new_dict.get("Enable") |
| 515 |
diff += 1 |
| 516 |
if new_dict.get("Enable"): |
| 517 |
tmp_dict = {"EnableDHCP": ["StaticIPAddress", "StaticSubnetMask", "StaticGateway"], |
| 518 |
"UseDHCPForDNSServerNames": ["StaticPreferredDNSServer", "StaticAlternateDNSServer"]} |
| 519 |
for key, val in tmp_dict.items(): |
| 520 |
if new_dict.get(key) is not None: |
| 521 |
if new_dict.get(key) != src_dict.get(key): |
| 522 |
src_dict[key] = new_dict.get(key) |
| 523 |
diff += 1 |
| 524 |
if not new_dict.get(key): |
| 525 |
diff = diff + _compare_dict_merge(src_dict, new_dict, val) |
| 526 |
return diff |
| 527 |
|
| 528 |
|
| 529 |
def update_ipv6_payload(src_dict, new_dict): |
| 530 |
diff = 0 |
| 531 |
if new_dict: |
| 532 |
if new_dict.get("Enable") != src_dict.get("Enable"): |
| 533 |
src_dict["Enable"] = new_dict.get("Enable") |
| 534 |
diff += 1 |
| 535 |
if new_dict.get("Enable"): |
| 536 |
tmp_dict = {"EnableAutoConfiguration": ["StaticIPAddress", "StaticPrefixLength", "StaticGateway"], |
| 537 |
"UseDHCPForDNSServerNames": ["StaticPreferredDNSServer", "StaticAlternateDNSServer"]} |
| 538 |
for key, val in tmp_dict.items(): |
| 539 |
if new_dict.get(key) is not None: |
| 540 |
if new_dict.get(key) != src_dict.get(key): |
| 541 |
src_dict[key] = new_dict.get(key) |
| 542 |
diff += 1 |
| 543 |
if not new_dict.get(key): |
| 544 |
diff = diff + _compare_dict_merge(src_dict, new_dict, val) |
| 545 |
return diff |
| 546 |
|
| 547 |
|
| 548 |
def update_dns_payload(src_dict, new_dict): |
| 549 |
diff = 0 |
| 550 |
if new_dict: |
| 551 |
mkey = "RegisterWithDNS" |
| 552 |
if new_dict.get(mkey) is not None: |
| 553 |
if new_dict.get(mkey) != src_dict.get(mkey): |
| 554 |
src_dict[mkey] = new_dict.get(mkey) |
| 555 |
diff += 1 |
| 556 |
if new_dict.get(mkey) is True: |
| 557 |
diff = diff + _compare_dict_merge(src_dict, new_dict, ["DnsName"]) |
| 558 |
mkey = "UseDHCPForDNSDomainName" |
| 559 |
if new_dict.get(mkey) is not None: |
| 560 |
if new_dict.get(mkey) != src_dict.get(mkey): |
| 561 |
src_dict[mkey] = new_dict.get(mkey) |
| 562 |
diff += 1 |
| 563 |
if not new_dict.get(mkey): |
| 564 |
diff = diff + _compare_dict_merge(src_dict, new_dict, ["DnsDomainName"]) |
| 565 |
return diff |
| 566 |
|
| 567 |
|
| 568 |
def update_vlan_payload(src_dict, new_dict): |
| 569 |
diff = 0 |
| 570 |
if new_dict: |
| 571 |
mkey = "EnableVLAN" |
| 572 |
if new_dict.get(mkey) is not None: |
| 573 |
if new_dict.get(mkey) != src_dict.get(mkey): |
| 574 |
src_dict[mkey] = new_dict.get(mkey) |
| 575 |
diff += 1 |
| 576 |
if new_dict.get(mkey) is True: |
| 577 |
diff = diff + _compare_dict_merge(src_dict, new_dict, ["Id"]) |
| 578 |
return diff |
| 579 |
|
| 580 |
|
| 581 |
def get_network_config_data(rest_obj, module): |
| 582 |
try: |
| 583 |
interface = module.params.get("interface_name") |
| 584 |
resp = rest_obj.invoke_request("GET", JOB_IP_CONFIG) |
| 585 |
adapter_list = resp.json_data.get("value") |
| 586 |
int_adp = None |
| 587 |
pri_adp = None |
| 588 |
if adapter_list: |
| 589 |
for adp in adapter_list: |
| 590 |
if interface and adp.get("InterfaceName") == interface: |
| 591 |
int_adp = adp |
| 592 |
break |
| 593 |
if adp.get("PrimaryInterface"): |
| 594 |
pri_adp = adp |
| 595 |
if interface and int_adp is None: |
| 596 |
module.exit_json(msg="The 'interface_name' value provided {0} is invalid".format(interface), failed=True) |
| 597 |
elif int_adp: |
| 598 |
return int_adp, "POST", POST_IP_CONFIG |
| 599 |
else: |
| 600 |
return pri_adp, "POST", POST_IP_CONFIG |
| 601 |
except HTTPError: |
| 602 |
pass |
| 603 |
except Exception as err: |
| 604 |
raise err |
| 605 |
resp = rest_obj.invoke_request("GET", IP_CONFIG) |
| 606 |
return resp.json_data, "PUT", IP_CONFIG |
| 607 |
|
| 608 |
|
| 609 |
def get_updated_payload(rest_obj, module, ipv4_payload, ipv6_payload, dns_payload, vlan_payload): |
| 610 |
current_setting = {} |
| 611 |
remove_keys = ["@odata.context", "@odata.type", "@odata.id", "CurrentSettings"] |
| 612 |
current_setting, rest_method, uri = get_network_config_data(rest_obj, module) |
| 613 |
remove_unwanted_keys(remove_keys, current_setting) |
| 614 |
payload_dict = {"Ipv4Configuration": [ipv4_payload, update_ipv4_payload], |
| 615 |
"Ipv6Configuration": [ipv6_payload, update_ipv6_payload], |
| 616 |
"DnsConfiguration": [dns_payload, update_dns_payload], |
| 617 |
"ManagementVLAN": [vlan_payload, update_vlan_payload]} |
| 618 |
diff = 0 |
| 619 |
enable_nic = module.params.get("enable_nic") |
| 620 |
if current_setting.get("EnableNIC") != enable_nic: |
| 621 |
current_setting["EnableNIC"] = enable_nic |
| 622 |
diff += 1 |
| 623 |
if enable_nic: |
| 624 |
for config, pload in payload_dict.items(): |
| 625 |
if pload[0]: |
| 626 |
diff = diff + pload[1](current_setting.get(config), pload[0]) |
| 627 |
delay = module.params.get("reboot_delay") |
| 628 |
if delay is not None: |
| 629 |
if current_setting["Delay"] != delay: |
| 630 |
current_setting["Delay"] = delay |
| 631 |
if diff == 0: |
| 632 |
module.exit_json( |
| 633 |
msg=NO_CHANGES_FOUND, network_configuration=current_setting) |
| 634 |
if module.check_mode: |
| 635 |
module.exit_json(changed=True, msg=CHANGES_FOUND) |
| 636 |
return current_setting, rest_method, uri |
| 637 |
|
| 638 |
|
| 639 |
def validate_ipaddress(module, ip_type, config, var_list, ip_func): |
| 640 |
ipv_input = module.params.get(config) |
| 641 |
if ipv_input: |
| 642 |
for ipname in var_list: |
| 643 |
val = ipv_input.get(ipname) |
| 644 |
if val and not ip_func(val): |
| 645 |
module.exit_json(msg="Invalid {0} address provided for the {1}".format(ip_type, ipname), failed=True) |
| 646 |
|
| 647 |
|
| 648 |
def validate_input(module): |
| 649 |
ip_addr = ["static_ip_address", "static_gateway", "static_preferred_dns_server", "static_alternate_dns_server"] |
| 650 |
validate_ipaddress(module, "IPv6", "ipv6_configuration", ip_addr, validate_ip_v6_address) |
| 651 |
ip_addr.append("static_subnet_mask") |
| 652 |
validate_ipaddress(module, "IPv4", "ipv4_configuration", ip_addr, validate_ip_address) |
| 653 |
delay = module.params.get("reboot_delay") |
| 654 |
if delay and delay < 0: |
| 655 |
module.exit_json(msg="Invalid value provided for 'reboot_delay'", failed=True) |
| 656 |
|
| 657 |
|
| 658 |
def main(): |
| 659 |
ipv4_options = {"enable": {"required": True, "type": "bool"}, |
| 660 |
"enable_dhcp": {"required": False, "type": "bool"}, |
| 661 |
"static_ip_address": {"required": False, "type": "str"}, |
| 662 |
"static_subnet_mask": {"required": False, "type": "str"}, |
| 663 |
"static_gateway": {"required": False, "type": "str"}, |
| 664 |
"use_dhcp_for_dns_server_names": {"required": False, "type": "bool"}, |
| 665 |
"static_preferred_dns_server": {"required": False, "type": "str"}, |
| 666 |
"static_alternate_dns_server": {"required": False, "type": "str"}} |
| 667 |
ipv6_options = {"enable": {"required": True, "type": "bool"}, |
| 668 |
"enable_auto_configuration": {"required": False, "type": "bool"}, |
| 669 |
"static_ip_address": {"required": False, "type": "str"}, |
| 670 |
"static_prefix_length": {"required": False, "type": "int"}, |
| 671 |
"static_gateway": {"required": False, "type": "str"}, |
| 672 |
"use_dhcp_for_dns_server_names": {"required": False, "type": "bool"}, |
| 673 |
"static_preferred_dns_server": {"required": False, "type": "str"}, |
| 674 |
"static_alternate_dns_server": {"required": False, "type": "str"}} |
| 675 |
dns_options = {"register_with_dns": {"required": False, "type": "bool"}, |
| 676 |
"use_dhcp_for_dns_domain_name": {"required": False, "type": "bool"}, |
| 677 |
"dns_name": {"required": False, "type": "str"}, |
| 678 |
"dns_domain_name": {"required": False, "type": "str"}} |
| 679 |
management_vlan = {"enable_vlan": {"required": True, "type": "bool"}, |
| 680 |
"vlan_id": {"required": False, "type": "int"}} |
| 681 |
|
| 682 |
specs = { |
| 683 |
"enable_nic": {"required": False, "type": "bool", "default": True}, |
| 684 |
"interface_name": {"required": False, "type": "str"}, |
| 685 |
"ipv4_configuration": |
| 686 |
{"required": False, "type": "dict", "options": ipv4_options, |
| 687 |
"required_if": [ |
| 688 |
['enable', True, ('enable_dhcp',), True], |
| 689 |
['enable_dhcp', False, ('static_ip_address', 'static_subnet_mask', "static_gateway"), False], |
| 690 |
['use_dhcp_for_dns_server_names', False, |
| 691 |
('static_preferred_dns_server', 'static_alternate_dns_server'), True] |
| 692 |
] |
| 693 |
}, |
| 694 |
"ipv6_configuration": |
| 695 |
{"required": False, "type": "dict", "options": ipv6_options, |
| 696 |
"required_if": [ |
| 697 |
['enable', True, ('enable_auto_configuration',), True], |
| 698 |
['enable_auto_configuration', False, ('static_ip_address', 'static_prefix_length', "static_gateway"), |
| 699 |
False], |
| 700 |
['use_dhcp_for_dns_server_names', False, |
| 701 |
('static_preferred_dns_server', 'static_alternate_dns_server'), True] |
| 702 |
] |
| 703 |
}, |
| 704 |
"dns_configuration": |
| 705 |
{"required": False, "type": "dict", "options": dns_options, |
| 706 |
"required_if": [ |
| 707 |
['register_with_dns', True, ('dns_name',), False], |
| 708 |
['use_dhcp_for_dns_domain_name', False, ('dns_domain_name',)] |
| 709 |
] |
| 710 |
}, |
| 711 |
"management_vlan": |
| 712 |
{"required": False, "type": "dict", "options": management_vlan, |
| 713 |
"required_if": [ |
| 714 |
['enable_vlan', True, ('vlan_id',), True] |
| 715 |
] |
| 716 |
}, |
| 717 |
"reboot_delay": {"required": False, "type": "int"} |
| 718 |
} |
| 719 |
|
| 720 |
module = OmeAnsibleModule( |
| 721 |
argument_spec=specs, |
| 722 |
required_if=[ |
| 723 |
["enable_nic", True, |
| 724 |
("ipv4_configuration", "ipv6_configuration", "dns_configuration", "management_vlan"), True] |
| 725 |
], |
| 726 |
supports_check_mode=True |
| 727 |
) |
| 728 |
try: |
| 729 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 730 |
validate_input(module) |
| 731 |
ipv4_payload, ipv6_payload, dns_payload, vlan_payload = get_payload(module) |
| 732 |
updated_payload, rest_method, uri = get_updated_payload( |
| 733 |
rest_obj, module, ipv4_payload, ipv6_payload, dns_payload, vlan_payload) |
| 734 |
resp = rest_obj.invoke_request(rest_method, uri, data=updated_payload, api_timeout=150) |
| 735 |
if rest_method == "POST": |
| 736 |
module.exit_json(msg="Successfully triggered job to update network address configuration.", |
| 737 |
network_configuration=updated_payload, job_info=resp.json_data, changed=True) |
| 738 |
module.exit_json(msg="Successfully triggered task to update network address configuration.", |
| 739 |
network_configuration=resp.json_data, changed=True) |
| 740 |
except HTTPError as err: |
| 741 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 742 |
except URLError as err: |
| 743 |
module.exit_json(msg=str(err), unreachable=True) |
| 744 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError, OSError) as err: |
| 745 |
module.exit_json(msg=str(err), failed=True) |
| 746 |
|
| 747 |
|
| 748 |
if __name__ == "__main__": |
| 749 |
main() |