| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
ANSIBLE_METADATA = { |
| 13 |
"metadata_version": "1.1", |
| 14 |
"status": ["preview"], |
| 15 |
"supported_by": "community", |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
DOCUMENTATION = """ |
| 20 |
--- |
| 21 |
module: purefa_network |
| 22 |
short_description: Manage network interfaces in a Pure Storage FlashArray |
| 23 |
version_added: '1.0.0' |
| 24 |
description: |
| 25 |
- This module manages the physical and virtual network interfaces on a Pure Storage FlashArray. |
| 26 |
- To manage VLAN interfaces use the I(purestorage.flasharray.purefa_vlan) module. |
| 27 |
- To manage network subnets use the I(purestorage.flasharray.purefa_subnet) module. |
| 28 |
- To remove an IP address from a non-management port use 0.0.0.0/0 |
| 29 |
author: Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 30 |
options: |
| 31 |
name: |
| 32 |
description: |
| 33 |
- Interface name (physical or virtual). |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
state: |
| 37 |
description: |
| 38 |
- State of existing interface (on/off). |
| 39 |
required: false |
| 40 |
default: present |
| 41 |
choices: [ "present", "absent" ] |
| 42 |
type: str |
| 43 |
address: |
| 44 |
description: |
| 45 |
- IPv4 or IPv6 address of interface in CIDR notation. |
| 46 |
- To remove an IP address from a non-management port use 0.0.0.0/0 |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
gateway: |
| 50 |
description: |
| 51 |
- IPv4 or IPv6 address of interface gateway. |
| 52 |
required: false |
| 53 |
type: str |
| 54 |
mtu: |
| 55 |
description: |
| 56 |
- MTU size of the interface. Range is 1280 to 9216. |
| 57 |
required: false |
| 58 |
type: int |
| 59 |
servicelist: |
| 60 |
description: |
| 61 |
- Assigns the specified (comma-separated) service list to one or more specified interfaces. |
| 62 |
- Replaces the previous service list. |
| 63 |
- Supported service lists depend on whether the network interface is Ethernet or Fibre Channel. |
| 64 |
- Note that I(system) is only valid for Cloud Block Store. |
| 65 |
elements: str |
| 66 |
type: list |
| 67 |
choices: [ "replication", "management", "ds", "file", "iscsi", "scsi-fc", "nvme-fc", "nvme-tcp", "nvme-roce", "system"] |
| 68 |
version_added: '1.15.0' |
| 69 |
interface: |
| 70 |
description: |
| 71 |
- Type of interface to create if subinterfaces is supplied |
| 72 |
type: str |
| 73 |
choices: [ "vif", "lacp" ] |
| 74 |
version_added: '1.22.0' |
| 75 |
subordinates: |
| 76 |
description: |
| 77 |
- List of one or more child devices to be added to a LACP interface |
| 78 |
- Subordinates must be on the same controller, therefore the full device needs |
| 79 |
to be provided. |
| 80 |
type: list |
| 81 |
elements: str |
| 82 |
version_added: '1.22.0' |
| 83 |
subinterfaces: |
| 84 |
description: |
| 85 |
- List of one or more child devices to be added to a VIF interface |
| 86 |
- Only the 'eth' name needs to be provided, such as 'eth6'. This interface on |
| 87 |
all controllers will be assigned to the interface. |
| 88 |
type: list |
| 89 |
elements: str |
| 90 |
version_added: '1.22.0' |
| 91 |
subnet: |
| 92 |
description: |
| 93 |
- Name of the subnet which interface is to be attached |
| 94 |
type: str |
| 95 |
version_added: '1.22.0' |
| 96 |
enabled: |
| 97 |
description: |
| 98 |
- State of the network interface |
| 99 |
type: bool |
| 100 |
default: true |
| 101 |
version_added: '1.22.0' |
| 102 |
extends_documentation_fragment: |
| 103 |
- purestorage.flasharray.purestorage.fa |
| 104 |
""" |
| 105 |
|
| 106 |
EXAMPLES = """ |
| 107 |
- name: Configure and enable network interface ct0.eth8 |
| 108 |
purestorage.flasharray.purefa_network: |
| 109 |
name: ct0.eth8 |
| 110 |
gateway: 10.21.200.1 |
| 111 |
address: "10.21.200.18/24" |
| 112 |
mtu: 9000 |
| 113 |
state: present |
| 114 |
fa_url: 10.10.10.2 |
| 115 |
api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40 |
| 116 |
|
| 117 |
- name: Disable physical interface ct1.eth2 |
| 118 |
purestorage.flasharray.purefa_network: |
| 119 |
name: ct1.eth2 |
| 120 |
state: absent |
| 121 |
fa_url: 10.10.10.2 |
| 122 |
api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40 |
| 123 |
|
| 124 |
- name: Enable virtual network interface vir0 |
| 125 |
purestorage.flasharray.purefa_network: |
| 126 |
name: vir0 |
| 127 |
state: present |
| 128 |
fa_url: 10.10.10.2 |
| 129 |
api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40 |
| 130 |
|
| 131 |
- name: Remove an IP address from iSCSI interface ct0.eth4 |
| 132 |
purestorage.flasharray.purefa_network: |
| 133 |
name: ct0.eth4 |
| 134 |
address: 0.0.0.0/0 |
| 135 |
gateway: 0.0.0.0 |
| 136 |
fa_url: 10.10.10.2 |
| 137 |
api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40 |
| 138 |
|
| 139 |
- name: Change service list for FC interface ct0.fc1 |
| 140 |
purestorage.flasharray.purefa_network: |
| 141 |
name: ct0.fc1 |
| 142 |
servicelist: |
| 143 |
- replication |
| 144 |
fa_url: 10.10.10.2 |
| 145 |
api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40 |
| 146 |
""" |
| 147 |
|
| 148 |
RETURN = """ |
| 149 |
""" |
| 150 |
|
| 151 |
try: |
| 152 |
from netaddr import IPAddress, IPNetwork, valid_ipv4 |
| 153 |
|
| 154 |
HAS_NETADDR = True |
| 155 |
except ImportError: |
| 156 |
HAS_NETADDR = False |
| 157 |
|
| 158 |
try: |
| 159 |
from pypureclient.flasharray import ( |
| 160 |
NetworkInterfacePatch, |
| 161 |
NetworkInterfacePost, |
| 162 |
NetworkinterfacepostEth, |
| 163 |
NetworkinterfacepatchEth, |
| 164 |
FixedReferenceNoId, |
| 165 |
ReferenceNoId, |
| 166 |
) |
| 167 |
|
| 168 |
HAS_PYPURECLIENT = True |
| 169 |
except ImportError: |
| 170 |
HAS_PYPURECLIENT = False |
| 171 |
|
| 172 |
from ansible.module_utils.basic import AnsibleModule |
| 173 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 174 |
get_array, |
| 175 |
purefa_argument_spec, |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
def update_fc_interface(module, array, interface): |
| 180 |
"""Modify FC Interface settings""" |
| 181 |
changed = False |
| 182 |
if not interface.enabled and module.params["state"] == "present": |
| 183 |
changed = True |
| 184 |
if not module.check_mode: |
| 185 |
network = NetworkInterfacePatch(enabled=True, override_npiv_check=True) |
| 186 |
res = array.patch_network_interfaces( |
| 187 |
names=[module.params["name"]], network=network |
| 188 |
) |
| 189 |
if res.status_code != 200: |
| 190 |
module.fail_json( |
| 191 |
msg="Failed to enable interface {0}.".format(module.params["name"]) |
| 192 |
) |
| 193 |
if interface.enabled and module.params["state"] == "absent": |
| 194 |
changed = True |
| 195 |
if not module.check_mode: |
| 196 |
network = NetworkInterfacePatch(enabled=False, override_npiv_check=True) |
| 197 |
res = array.patch_network_interfaces( |
| 198 |
names=[module.params["name"]], network=network |
| 199 |
) |
| 200 |
if res.status_code != 200: |
| 201 |
module.fail_json( |
| 202 |
msg="Failed to disable interface {0}.".format(module.params["name"]) |
| 203 |
) |
| 204 |
if module.params["servicelist"] and sorted(module.params["servicelist"]) != sorted( |
| 205 |
interface.services |
| 206 |
): |
| 207 |
changed = True |
| 208 |
if not module.check_mode: |
| 209 |
network = NetworkInterfacePatch(services=module.params["servicelist"]) |
| 210 |
res = array.patch_network_interfaces( |
| 211 |
names=[module.params["name"]], network=network |
| 212 |
) |
| 213 |
if res.status_code != 200: |
| 214 |
module.fail_json( |
| 215 |
msg="Failed to update interface service list {0}. Error: {1}".format( |
| 216 |
module.params["name"], res.errors[0].message |
| 217 |
) |
| 218 |
) |
| 219 |
|
| 220 |
module.exit_json(changed=changed) |
| 221 |
|
| 222 |
|
| 223 |
def _check_subinterfaces(module, array): |
| 224 |
subordinates = [] |
| 225 |
subinterfaces = list( |
| 226 |
array.get_network_interfaces(names=[module.params["name"]]).items |
| 227 |
)[0].eth.subinterfaces |
| 228 |
for subinterface in subinterfaces: |
| 229 |
subordinates.append(subinterface) |
| 230 |
return subordinates |
| 231 |
|
| 232 |
|
| 233 |
def _create_subordinates(module, array): |
| 234 |
subordinates_v1 = [] |
| 235 |
subordinates_v2 = [] |
| 236 |
all_children = True |
| 237 |
if module.params["subordinates"]: |
| 238 |
for inter in sorted(module.params["subordinates"]): |
| 239 |
if array.get_network_interfaces(names=[inter]).status_code != 200: |
| 240 |
all_children = False |
| 241 |
if not all_children: |
| 242 |
module.fail_json( |
| 243 |
msg="Subordinate {0} does not exist. Ensure you have specified the controller.".format( |
| 244 |
inter |
| 245 |
) |
| 246 |
) |
| 247 |
subordinates_v2.append(FixedReferenceNoId(name=inter)) |
| 248 |
subordinates_v1.append(inter) |
| 249 |
return subordinates_v1, subordinates_v2 |
| 250 |
|
| 251 |
|
| 252 |
def _create_subinterfaces(module, array): |
| 253 |
subinterfaces_v1 = [] |
| 254 |
subinterfaces_v2 = [] |
| 255 |
all_children = True |
| 256 |
purity_vm = bool(len(array.get_controllers().items) == 1) |
| 257 |
if module.params["subinterfaces"]: |
| 258 |
if any("lacp" in sub for sub in module.params["subinterfaces"]): |
| 259 |
for inter in sorted(module.params["subinterfaces"]): |
| 260 |
if array.get_network_interfaces(names=[inter]).status_code != 200: |
| 261 |
all_children = False |
| 262 |
if not all_children: |
| 263 |
module.fail_json( |
| 264 |
msg="Child subinterface {0} does not exist".format(inter) |
| 265 |
) |
| 266 |
subinterfaces_v2.append(FixedReferenceNoId(name=inter)) |
| 267 |
subinterfaces_v1.append(inter) |
| 268 |
else: |
| 269 |
for inter in sorted(module.params["subinterfaces"]): |
| 270 |
|
| 271 |
if ( |
| 272 |
array.get_network_interfaces(names=["ct0." + inter]).status_code |
| 273 |
!= 200 |
| 274 |
): |
| 275 |
all_children = False |
| 276 |
if not all_children: |
| 277 |
module.fail_json( |
| 278 |
msg="Child subinterface {0} does not exist".format(inter) |
| 279 |
) |
| 280 |
subinterfaces_v2.append(FixedReferenceNoId(name="ct0." + inter)) |
| 281 |
subinterfaces_v1.append("ct0." + inter) |
| 282 |
if not purity_vm: |
| 283 |
subinterfaces_v2.append(FixedReferenceNoId(name="ct1." + inter)) |
| 284 |
subinterfaces_v1.append("ct1." + inter) |
| 285 |
return subinterfaces_v1, subinterfaces_v2 |
| 286 |
|
| 287 |
|
| 288 |
def update_interface(module, array): |
| 289 |
"""Modify Interface settings""" |
| 290 |
changed = False |
| 291 |
interfaces = list(array.get_network_interfaces(names=[module.params["name"]]).items) |
| 292 |
if not interfaces: |
| 293 |
module.fail_json("Interface {0} not found".format(module.params["name"])) |
| 294 |
|
| 295 |
interface = interfaces[0] |
| 296 |
|
| 297 |
def is_fc_interface(ifname): |
| 298 |
parts = ifname.split(".") |
| 299 |
return len(parts) > 1 and parts[1].lower().startswith("f") |
| 300 |
|
| 301 |
|
| 302 |
if is_fc_interface(module.params["name"]): |
| 303 |
if not interface.enabled and module.params["state"] == "present": |
| 304 |
changed = True |
| 305 |
if not module.check_mode: |
| 306 |
network = NetworkInterfacePatch(enabled=True, override_npiv_check=True) |
| 307 |
res = array.patch_network_interfaces( |
| 308 |
names=[module.params["name"]], network=network |
| 309 |
) |
| 310 |
if res.status_code != 200: |
| 311 |
module.fail_json( |
| 312 |
msg="Failed to enable interface {0}.".format( |
| 313 |
module.params["name"] |
| 314 |
) |
| 315 |
) |
| 316 |
if interface.enabled and module.params["state"] == "absent": |
| 317 |
changed = True |
| 318 |
if not module.check_mode: |
| 319 |
network = NetworkInterfacePatch(enabled=False, override_npiv_check=True) |
| 320 |
res = array.patch_network_interfaces( |
| 321 |
names=[module.params["name"]], network=network |
| 322 |
) |
| 323 |
if res.status_code != 200: |
| 324 |
module.fail_json( |
| 325 |
msg="Failed to disable interface {0}.".format( |
| 326 |
module.params["name"] |
| 327 |
) |
| 328 |
) |
| 329 |
if module.params["servicelist"] and sorted( |
| 330 |
module.params["servicelist"] |
| 331 |
) != sorted(interface.services): |
| 332 |
changed = True |
| 333 |
if not module.check_mode: |
| 334 |
network = NetworkInterfacePatch(services=module.params["servicelist"]) |
| 335 |
res = array.patch_network_interfaces( |
| 336 |
names=[module.params["name"]], network=network |
| 337 |
) |
| 338 |
if res.status_code != 200: |
| 339 |
module.fail_json( |
| 340 |
msg="Failed to update interface service list {0}. Error: {1}".format( |
| 341 |
module.params["name"], res.errors[0].message |
| 342 |
) |
| 343 |
) |
| 344 |
module.exit_json(changed=changed) |
| 345 |
|
| 346 |
current_state = { |
| 347 |
"enabled": interface.enabled, |
| 348 |
"mtu": interface.eth.mtu, |
| 349 |
"gateway": getattr(interface, "gateway", None), |
| 350 |
"address": getattr(interface, "address", None), |
| 351 |
"netmask": getattr(interface, "netmask", None), |
| 352 |
"services": sorted(interface["services"]), |
| 353 |
"subinterfaces": sorted(interface.eth.subinterfaces), |
| 354 |
} |
| 355 |
new_state = current_state.copy() |
| 356 |
|
| 357 |
if module.params["subinterfaces"]: |
| 358 |
new_subinterfaces = _check_subinterfaces(module, array) |
| 359 |
if new_subinterfaces != current_state["subinterfaces"]: |
| 360 |
new_state["subinterfaces"] = new_subinterfaces |
| 361 |
|
| 362 |
if module.params["subordinates"]: |
| 363 |
subordinates = _check_subinterfaces(module, array) |
| 364 |
if sorted(subordinates) != sorted(module.params["subordinates"]): |
| 365 |
new_state["subinterfaces"] = module.params["subordinates"] |
| 366 |
|
| 367 |
if module.params["enabled"] != current_state["enabled"]: |
| 368 |
new_state["enabled"] = module.params["enabled"] |
| 369 |
|
| 370 |
if ( |
| 371 |
module.params["servicelist"] |
| 372 |
and sorted(module.params["servicelist"]) != current_state["services"] |
| 373 |
): |
| 374 |
new_state["services"] = sorted(module.params["servicelist"]) |
| 375 |
|
| 376 |
if ( |
| 377 |
module.params["address"] |
| 378 |
and module.params["address"] != current_state["address"] |
| 379 |
): |
| 380 |
new_state["netmask"] = current_state["netmask"] |
| 381 |
if module.params["gateway"] and module.params["gateway"] not in [ |
| 382 |
"0.0.0.0", |
| 383 |
"::", |
| 384 |
]: |
| 385 |
if module.params["gateway"] not in IPNetwork(module.params["address"]): |
| 386 |
module.fail_json(msg="Gateway and subnet are not compatible.") |
| 387 |
if not module.params["gateway"] and interface["gateway"] not in [ |
| 388 |
None, |
| 389 |
IPNetwork(module.params["address"]), |
| 390 |
]: |
| 391 |
module.fail_json(msg="Gateway and subnet are not compatible.") |
| 392 |
new_state["address"] = str(module.params["address"].split("/", 1)[0]) |
| 393 |
if new_state["address"] in ["0.0.0.0", "::"]: |
| 394 |
new_state["netmask"] = "" |
| 395 |
|
| 396 |
if module.params["mtu"] and module.params["mtu"] != current_state["mtu"]: |
| 397 |
if not 1280 <= module.params["mtu"] <= 9216: |
| 398 |
module.fail_json( |
| 399 |
msg="MTU {0} is out of range (1280 to 9216)".format( |
| 400 |
module.params["mtu"] |
| 401 |
) |
| 402 |
) |
| 403 |
else: |
| 404 |
new_state["mtu"] = module.params["mtu"] |
| 405 |
|
| 406 |
if module.params["address"]: |
| 407 |
if new_state["address"]: |
| 408 |
if valid_ipv4(new_state["address"]): |
| 409 |
new_state["netmask"] = str(IPNetwork(module.params["address"]).netmask) |
| 410 |
else: |
| 411 |
new_state["netmask"] = str(module.params["address"].split("/", 1)[1]) |
| 412 |
if new_state["netmask"] in ["0.0.0.0", "0"]: |
| 413 |
new_state["netmask"] = "" |
| 414 |
|
| 415 |
if module.params["gateway"] and module.params["gateway"] in ["0.0.0.0", "::"]: |
| 416 |
new_state["gateway"] = "" |
| 417 |
elif new_state["address"] and valid_ipv4(new_state["address"]): |
| 418 |
cidr = str(IPAddress(new_state["netmask"]).netmask_bits()) |
| 419 |
full_addr = new_state["address"] + "/" + cidr |
| 420 |
if module.params["gateway"] not in IPNetwork(full_addr): |
| 421 |
module.fail_json(msg="Gateway and subnet are not compatible.") |
| 422 |
new_state["gateway"] = module.params["gateway"] |
| 423 |
else: |
| 424 |
new_state["gateway"] = module.params["gateway"] |
| 425 |
|
| 426 |
if new_state["address"]: |
| 427 |
if ( |
| 428 |
current_state["address"] |
| 429 |
and IPAddress(new_state["address"]).version |
| 430 |
!= IPAddress(current_state["address"]).version |
| 431 |
): |
| 432 |
if new_state["gateway"]: |
| 433 |
if ( |
| 434 |
IPAddress(new_state["gateway"]).version |
| 435 |
!= IPAddress(new_state["address"]).version |
| 436 |
): |
| 437 |
module.fail_json( |
| 438 |
msg="Changing IP protocol requires gateway to change as well." |
| 439 |
) |
| 440 |
if new_state != current_state: |
| 441 |
changed = True |
| 442 |
if ( |
| 443 |
module.params["servicelist"] |
| 444 |
and sorted(module.params["servicelist"]) != interface["services"] |
| 445 |
): |
| 446 |
if not module.check_mode: |
| 447 |
network = NetworkInterfacePatch(services=module.params["servicelist"]) |
| 448 |
res = array.patch_network_interfaces( |
| 449 |
names=[module.params["name"]], network=network |
| 450 |
) |
| 451 |
if res.status_code != 200: |
| 452 |
module.fail_json( |
| 453 |
msg="Failed to update interface service list {0}. Error: {1}".format( |
| 454 |
module.params["name"], res.errors[0].message |
| 455 |
) |
| 456 |
) |
| 457 |
if ( |
| 458 |
"management" in interface["services"] or "app" in interface["services"] |
| 459 |
) and new_state["address"] in ["0.0.0.0/0", "::/0"]: |
| 460 |
module.fail_json( |
| 461 |
msg="Removing IP address from a management or app port is not supported" |
| 462 |
) |
| 463 |
if not module.check_mode: |
| 464 |
res = array.patch_network_interfaces( |
| 465 |
names=[interface.name], |
| 466 |
network=NetworkInterfacePatch(enabled=new_state["enabled"]), |
| 467 |
) |
| 468 |
if res.status_code != 200: |
| 469 |
module.fail_json( |
| 470 |
msg="Failed to enable or disable interface {0}. Error: {1}".format( |
| 471 |
module.params["name"], res.errors[0].message |
| 472 |
) |
| 473 |
) |
| 474 |
if new_state["gateway"] is not None: |
| 475 |
res = array.patch_network_interfaces( |
| 476 |
names=[interface.name], |
| 477 |
network=NetworkInterfacePatch( |
| 478 |
eth=NetworkinterfacepatchEth( |
| 479 |
address=new_state["address"], |
| 480 |
mtu=new_state["mtu"], |
| 481 |
netmask=new_state["netmask"], |
| 482 |
gateway=new_state["gateway"], |
| 483 |
) |
| 484 |
), |
| 485 |
) |
| 486 |
if res.status_code != 200: |
| 487 |
module.fail_json( |
| 488 |
msg="Failed to update IP settings for {0}. Error: {1}".format( |
| 489 |
interface.name, res.errors[0].message |
| 490 |
) |
| 491 |
) |
| 492 |
if ( |
| 493 |
current_state["subinterfaces"] != new_state["subinterfaces"] |
| 494 |
and new_state["subinterfaces"] != [] |
| 495 |
): |
| 496 |
new_subs = [] |
| 497 |
for sub in new_state["subinterfaces"]: |
| 498 |
new_subs.append(ReferenceNoId(sub)) |
| 499 |
res = array.patch_network_interfaces( |
| 500 |
names=[interface.name], |
| 501 |
network=NetworkInterfacePatch( |
| 502 |
eth=NetworkinterfacepatchEth( |
| 503 |
subinterfacelist=new_subs, |
| 504 |
) |
| 505 |
), |
| 506 |
) |
| 507 |
if res.status_code != 200: |
| 508 |
module.fail_json( |
| 509 |
msg="Failed to update subinterfaces for {0}. Error: {1}".format( |
| 510 |
interface.name, res.errors[0].message |
| 511 |
) |
| 512 |
) |
| 513 |
else: |
| 514 |
try: |
| 515 |
if valid_ipv4(new_state["address"]): |
| 516 |
empty_gateway = "0.0.0.0" |
| 517 |
else: |
| 518 |
empty_gateway = "::" |
| 519 |
except Exception: |
| 520 |
empty_gateway = "::" |
| 521 |
res = array.patch_network_interfaces( |
| 522 |
names=[interface.name], |
| 523 |
network=NetworkInterfacePatch( |
| 524 |
eth=NetworkinterfacepatchEth( |
| 525 |
address=new_state["address"], |
| 526 |
mtu=new_state["mtu"], |
| 527 |
netmask=new_state["netmask"], |
| 528 |
gateway=empty_gateway, |
| 529 |
) |
| 530 |
), |
| 531 |
) |
| 532 |
if res.status_code != 200: |
| 533 |
module.fail_json( |
| 534 |
msg="Failed to update IP settings for {0}. Error: {1}".format( |
| 535 |
interface.name, res.errors[0].message |
| 536 |
) |
| 537 |
) |
| 538 |
if ( |
| 539 |
current_state["subinterfaces"] != new_state["subinterfaces"] |
| 540 |
and new_state["subinterfaces"] != [] |
| 541 |
): |
| 542 |
res = array.delete_network_interfacess( |
| 543 |
names=[module.params["name"]] |
| 544 |
) |
| 545 |
if res.status_code != 200: |
| 546 |
module.fail_json( |
| 547 |
msg="Failed to delete network interface {0}. Error: {1}".format( |
| 548 |
module.params["name"], res.errors[0].message |
| 549 |
) |
| 550 |
) |
| 551 |
create_interface(module, array) |
| 552 |
module.exit_json(changed=changed) |
| 553 |
|
| 554 |
|
| 555 |
def create_interface(module, array): |
| 556 |
changed = True |
| 557 |
subnet_exists = bool( |
| 558 |
array.get_subnets(names=[module.params["subnet"]]).status_code == 200 |
| 559 |
) |
| 560 |
if module.params["subnet"] and not subnet_exists: |
| 561 |
module.fail_json( |
| 562 |
msg="Subnet {0} does not exist".format(module.params["subnet"]) |
| 563 |
) |
| 564 |
|
| 565 |
if module.params["interface"] == "vif": |
| 566 |
dummy, subinterfaces = _create_subinterfaces(module, array) |
| 567 |
else: |
| 568 |
dummy, subinterfaces = _create_subordinates(module, array) |
| 569 |
|
| 570 |
if not module.check_mode: |
| 571 |
if module.params["address"]: |
| 572 |
address = str(module.params["address"].strip("[]").split("/", 1)[0]) |
| 573 |
if valid_ipv4(address): |
| 574 |
netmask = str(IPNetwork(module.params["address"]).netmask) |
| 575 |
else: |
| 576 |
netmask = str(module.params["address"].strip("[]").split("/", 1)[1]) |
| 577 |
else: |
| 578 |
netmask = None |
| 579 |
address = None |
| 580 |
if module.params["gateway"]: |
| 581 |
gateway = str(module.params["gateway"].strip("[]")) |
| 582 |
if gateway not in ["0.0.0.0", "::"]: |
| 583 |
if address and gateway not in IPNetwork(module.params["address"]): |
| 584 |
module.fail_json(msg="Gateway and subnet are not compatible.") |
| 585 |
else: |
| 586 |
gateway = None |
| 587 |
if module.params["interface"] == "vif": |
| 588 |
res = array.post_network_interfaces( |
| 589 |
names=[module.params["name"]], |
| 590 |
network=NetworkInterfacePost( |
| 591 |
eth=NetworkinterfacepostEth(subtype="vif") |
| 592 |
), |
| 593 |
) |
| 594 |
else: |
| 595 |
res = array.post_network_interfaces( |
| 596 |
names=[module.params["name"]], |
| 597 |
network=NetworkInterfacePost( |
| 598 |
eth=NetworkinterfacepostEth( |
| 599 |
subtype="lacpbond", subinterfaces=subinterfaces |
| 600 |
), |
| 601 |
), |
| 602 |
) |
| 603 |
|
| 604 |
if res.status_code != 200: |
| 605 |
module.fail_json( |
| 606 |
msg="Failed to create interface {0}. Error: {1}".format( |
| 607 |
module.params["name"], res.errors[0].message |
| 608 |
) |
| 609 |
) |
| 610 |
|
| 611 |
if module.params["subinterfaces"] and module.params["subnet"]: |
| 612 |
res = array.patch_network_interfaces( |
| 613 |
names=[module.params["name"]], |
| 614 |
network=NetworkInterfacePatch( |
| 615 |
enabled=module.params["enabled"], |
| 616 |
eth=NetworkinterfacepatchEth( |
| 617 |
subinterfaces=subinterfaces, |
| 618 |
address=address, |
| 619 |
gateway=gateway, |
| 620 |
mtu=module.params["mtu"], |
| 621 |
netmask=netmask, |
| 622 |
subnet=ReferenceNoId(name=module.params["subnet"]), |
| 623 |
), |
| 624 |
), |
| 625 |
) |
| 626 |
if res.status_code != 200: |
| 627 |
array.delete_network_interfaces(names=[module.params["name"]]) |
| 628 |
module.fail_json( |
| 629 |
msg="Failed to create interface {0}. Error: {1}".format( |
| 630 |
module.params["name"], res.errors[0].message |
| 631 |
) |
| 632 |
) |
| 633 |
elif module.params["subinterfaces"] and not module.params["subnet"]: |
| 634 |
res = array.patch_network_interfaces( |
| 635 |
names=[module.params["name"]], |
| 636 |
network=NetworkInterfacePatch( |
| 637 |
enabled=module.params["enabled"], |
| 638 |
eth=NetworkinterfacepatchEth( |
| 639 |
subinterfaces=subinterfaces, |
| 640 |
address=address, |
| 641 |
gateway=gateway, |
| 642 |
mtu=module.params["mtu"], |
| 643 |
netmask=netmask, |
| 644 |
), |
| 645 |
), |
| 646 |
) |
| 647 |
if res.status_code != 200: |
| 648 |
array.delete_network_interfaces(names=[module.params["name"]]) |
| 649 |
module.fail_json( |
| 650 |
msg="Failed to create interface {0}. Error: {1}".format( |
| 651 |
module.params["name"], res.errors[0].message |
| 652 |
) |
| 653 |
) |
| 654 |
elif not module.params["subinterfaces"] and module.params["subnet"]: |
| 655 |
res = array.patch_network_interfaces( |
| 656 |
names=[module.params["name"]], |
| 657 |
network=NetworkInterfacePatch( |
| 658 |
enabled=module.params["enabled"], |
| 659 |
eth=NetworkinterfacepatchEth( |
| 660 |
address=address, |
| 661 |
gateway=gateway, |
| 662 |
mtu=module.params["mtu"], |
| 663 |
netmask=netmask, |
| 664 |
subnet=ReferenceNoId(name=module.params["subnet"]), |
| 665 |
), |
| 666 |
), |
| 667 |
) |
| 668 |
if res.status_code != 200: |
| 669 |
array.delete_network_interfaces(names=[module.params["name"]]) |
| 670 |
module.fail_json( |
| 671 |
msg="Failed to create interface {0}. Error: {1}".format( |
| 672 |
module.params["name"], res.errors[0].message |
| 673 |
) |
| 674 |
) |
| 675 |
else: |
| 676 |
res = array.patch_network_interfaces( |
| 677 |
names=[module.params["name"]], |
| 678 |
network=NetworkInterfacePatch( |
| 679 |
enabled=module.params["enabled"], |
| 680 |
eth=NetworkinterfacepatchEth( |
| 681 |
address=address, |
| 682 |
gateway=gateway, |
| 683 |
mtu=module.params["mtu"], |
| 684 |
netmask=netmask, |
| 685 |
), |
| 686 |
), |
| 687 |
) |
| 688 |
if res.status_code != 200: |
| 689 |
array.delete_network_interfaces(names=[module.params["name"]]) |
| 690 |
module.fail_json( |
| 691 |
msg="Failed to create interface {0}. Error: {1}".format( |
| 692 |
module.params["name"], res.errors[0].message |
| 693 |
) |
| 694 |
) |
| 695 |
|
| 696 |
module.exit_json(changed=changed) |
| 697 |
|
| 698 |
|
| 699 |
def delete_interface(module, array): |
| 700 |
changed = True |
| 701 |
if not module.check_mode: |
| 702 |
res = array.delete_network_interfaces(names=[module.params["name"]]) |
| 703 |
if res.status_code != 200: |
| 704 |
module.fail_json( |
| 705 |
msg="Failed to delete network interface {0}. Error: {1}".format( |
| 706 |
module.params["name"], res.errors[0].message |
| 707 |
) |
| 708 |
) |
| 709 |
module.exit_json(changed=changed) |
| 710 |
|
| 711 |
|
| 712 |
def main(): |
| 713 |
argument_spec = purefa_argument_spec() |
| 714 |
argument_spec.update( |
| 715 |
dict( |
| 716 |
name=dict(type="str", required=True), |
| 717 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 718 |
address=dict(type="str"), |
| 719 |
gateway=dict(type="str"), |
| 720 |
mtu=dict(type="int"), |
| 721 |
servicelist=dict( |
| 722 |
type="list", |
| 723 |
elements="str", |
| 724 |
choices=[ |
| 725 |
"replication", |
| 726 |
"management", |
| 727 |
"ds", |
| 728 |
"file", |
| 729 |
"iscsi", |
| 730 |
"scsi-fc", |
| 731 |
"nvme-fc", |
| 732 |
"nvme-tcp", |
| 733 |
"nvme-roce", |
| 734 |
"system", |
| 735 |
], |
| 736 |
), |
| 737 |
interface=dict(type="str", choices=["vif", "lacp"]), |
| 738 |
subinterfaces=dict(type="list", elements="str"), |
| 739 |
subordinates=dict(type="list", elements="str"), |
| 740 |
subnet=dict(type="str"), |
| 741 |
enabled=dict(type="bool", default=True), |
| 742 |
) |
| 743 |
) |
| 744 |
|
| 745 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 746 |
|
| 747 |
if module.params["state"] == "present": |
| 748 |
if module.params["interface"] == "lacp" and not module.params["subordinates"]: |
| 749 |
module.fail_json( |
| 750 |
msg="interface is lacp but all of the following are missing: subordinates" |
| 751 |
) |
| 752 |
|
| 753 |
creating_new_if = bool(module.params["interface"]) |
| 754 |
|
| 755 |
if not HAS_NETADDR: |
| 756 |
module.fail_json(msg="netaddr module is required") |
| 757 |
|
| 758 |
if not HAS_PYPURECLIENT: |
| 759 |
module.fail_json(msg="pypureclient module is required") |
| 760 |
array = get_array(module) |
| 761 |
if module.params["address"]: |
| 762 |
module.params["address"] = module.params["address"].strip("[]") |
| 763 |
if "/" not in module.params["address"]: |
| 764 |
module.fail_json(msg="address must include valid netmask bits") |
| 765 |
if module.params["gateway"]: |
| 766 |
module.params["gateway"] = module.params["gateway"].strip("[]") |
| 767 |
if bool("CBS" in list(array.get_controllers().items)[0].model): |
| 768 |
if module.params["servicelist"] and "system" in module.params["servicelist"]: |
| 769 |
module.fail_json( |
| 770 |
msg="Only Cloud Block Store supports the 'system' service type" |
| 771 |
) |
| 772 |
if "." in module.params["name"]: |
| 773 |
interface = bool( |
| 774 |
array.get_network_interfaces(names=[module.params["name"]]).status_code |
| 775 |
== 200 |
| 776 |
) |
| 777 |
if not interface: |
| 778 |
module.fail_json(msg="Invalid network interface specified.") |
| 779 |
else: |
| 780 |
update_interface(module, array) |
| 781 |
else: |
| 782 |
if (module.params["interface"] == "vif" and module.params["subordinates"]) or ( |
| 783 |
module.params["interface"] == "lacp" and module.params["subinterfaces"] |
| 784 |
): |
| 785 |
module.fail_json( |
| 786 |
msg="interface type not compatible with provided subinterfaces | subordinates" |
| 787 |
) |
| 788 |
interface = bool( |
| 789 |
array.get_network_interfaces(names=[module.params["name"]]).status_code |
| 790 |
== 200 |
| 791 |
) |
| 792 |
if not creating_new_if: |
| 793 |
if not interface: |
| 794 |
module.fail_json(msg="Invalid network interface specified.") |
| 795 |
elif module.params["state"] == "present": |
| 796 |
update_interface(module, array) |
| 797 |
else: |
| 798 |
delete_interface(module, array) |
| 799 |
elif not interface and module.params["state"] == "present": |
| 800 |
create_interface(module, array) |
| 801 |
elif interface and module.params["state"] == "absent": |
| 802 |
delete_interface(module, array) |
| 803 |
elif interface and module.params["state"] == "present": |
| 804 |
update_interface(module, array) |
| 805 |
else: |
| 806 |
module.exit_json(changed=False) |
| 807 |
|
| 808 |
module.exit_json(changed=False) |
| 809 |
|
| 810 |
|
| 811 |
if __name__ == "__main__": |
| 812 |
main() |