| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefb_lag |
| 20 |
version_added: '1.7.0' |
| 21 |
short_description: Manage FlashBlade Link Aggregation Groups |
| 22 |
description: |
| 23 |
- Maintain FlashBlade Link Aggregation Groups |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the Link Aggregation Group |
| 30 |
type: str |
| 31 |
default: uplink |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the LAG should be added or deleted |
| 35 |
default: present |
| 36 |
choices: [ absent, present ] |
| 37 |
type: str |
| 38 |
ports: |
| 39 |
description: |
| 40 |
- Name of network ports assigned to the LAG |
| 41 |
- Format should be CHx.ETHy, where CHx is the chassis number and |
| 42 |
ETHy is the ethernet port number. |
| 43 |
- Matched port pairs from each Fabric Module in the Chassis will |
| 44 |
be used. |
| 45 |
- To modify required ports for a LAG specify only the ports required |
| 46 |
by the LAG. Any ports currently used by the LAG not specified will be |
| 47 |
disconnected from the LAG. |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
extends_documentation_fragment: |
| 51 |
- purestorage.flashblade.purestorage.fb |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: Add LAG |
| 56 |
purestorage.flashblade.purefb_lag: |
| 57 |
name: lag2 |
| 58 |
ports: |
| 59 |
- ch1.eth2 |
| 60 |
fb_url: 10.10.10.2 |
| 61 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 62 |
|
| 63 |
- name: Upate LAG |
| 64 |
purestorage.flashblade.purefb_lag: |
| 65 |
name: lag2 |
| 66 |
ports: |
| 67 |
- ch1.eth2 |
| 68 |
- ch1.eth4 |
| 69 |
fb_url: 10.10.10.2 |
| 70 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 71 |
|
| 72 |
- name: Delete LAG |
| 73 |
purestorage.flashblade.purefb_lag: |
| 74 |
name: lag2 |
| 75 |
state: absent |
| 76 |
fb_url: 10.10.10.2 |
| 77 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = r""" |
| 81 |
lag: |
| 82 |
description: A dictionary describing the LAG. |
| 83 |
type: dict |
| 84 |
returned: success |
| 85 |
contains: |
| 86 |
lag_speed: |
| 87 |
description: Combined speed of all ports in the LAG in Gb/s |
| 88 |
type: str |
| 89 |
port_speed: |
| 90 |
description: Configured speed of each port in the LAG in Gb/s |
| 91 |
type: str |
| 92 |
mac_address: |
| 93 |
description: Unique MAC address assigned to the LAG |
| 94 |
type: str |
| 95 |
status: |
| 96 |
description: Health status of the LAG. |
| 97 |
type: str |
| 98 |
""" |
| 99 |
|
| 100 |
HAS_PURESTORAGE = True |
| 101 |
try: |
| 102 |
from pypureclient import flashblade |
| 103 |
except ImportError: |
| 104 |
HAS_PURESTORAGE = False |
| 105 |
|
| 106 |
from ansible.module_utils.basic import AnsibleModule |
| 107 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 108 |
get_system, |
| 109 |
purefb_argument_spec, |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
def delete_lag(module, blade): |
| 114 |
"""Delete Link Aggregation Group""" |
| 115 |
changed = True |
| 116 |
if not module.check_mode: |
| 117 |
res = blade.delete_link_aggregation_groups(names=[module.params["name"]]) |
| 118 |
if res.status_code != 200: |
| 119 |
module.fail_json( |
| 120 |
msg="Failed to delete LAG {0}. Error: {1}".format( |
| 121 |
module.params["name"], res.errors[0].message |
| 122 |
) |
| 123 |
) |
| 124 |
module.exit_json(changed=changed) |
| 125 |
|
| 126 |
|
| 127 |
def update_lag(module, blade): |
| 128 |
"""Update Link Aggregation Group""" |
| 129 |
changed = False |
| 130 |
used_ports = [] |
| 131 |
current_ports = [] |
| 132 |
lagfact = [] |
| 133 |
current_lag = list( |
| 134 |
blade.get_link_aggregation_groups(names=[module.params["name"]]).items |
| 135 |
)[0] |
| 136 |
for port in range(len(current_lag.ports)): |
| 137 |
used_ports.append(current_lag.ports[port].name) |
| 138 |
for lag_port in range(len(module.params["ports"]), 2): |
| 139 |
if ( |
| 140 |
not ( |
| 141 |
module.params["ports"][lag_port].split(".")[0].upper() |
| 142 |
+ ".FM1." |
| 143 |
+ module.params["ports"][lag_port].split(".")[1].upper() |
| 144 |
) |
| 145 |
in used_ports |
| 146 |
): |
| 147 |
current_lags = list(blade.get_link_aggregation_groups().items) |
| 148 |
for lag in range(len(current_lags)): |
| 149 |
for port in range(len(current_lags[lag].ports)): |
| 150 |
current_ports.append(current_lags[lag].ports[port].name) |
| 151 |
for current_lag_port in range(len(current_ports)): |
| 152 |
if ( |
| 153 |
module.params["ports"][current_lag_port].split(".")[0].upper() |
| 154 |
+ ".FM1." |
| 155 |
+ module.params["ports"][current_lag_port].split(".")[1].upper() |
| 156 |
) in current_ports: |
| 157 |
module.fail_json( |
| 158 |
msg="Selected port {0} is currently in use by another LAG.".format( |
| 159 |
module.params["ports"][lag_port].upper() |
| 160 |
) |
| 161 |
) |
| 162 |
new_ports = [] |
| 163 |
for port in range(len(module.params["ports"])): |
| 164 |
if module.params["ports"][port].split(".")[0].upper()[0] != "X": |
| 165 |
new_ports.append( |
| 166 |
module.params["ports"][port].split(".")[0].upper() |
| 167 |
+ ".FM1." |
| 168 |
+ module.params["ports"][port].split(".")[1].upper() |
| 169 |
) |
| 170 |
new_ports.append( |
| 171 |
module.params["ports"][port].split(".")[0].upper() |
| 172 |
+ ".FM2." |
| 173 |
+ module.params["ports"][port].split(".")[1].upper() |
| 174 |
) |
| 175 |
else: |
| 176 |
new_ports.append(module.params["ports"][port].upper()) |
| 177 |
ports = [] |
| 178 |
for final_port in range(len(new_ports)): |
| 179 |
ports.append(flashblade.Reference(name=new_ports[final_port])) |
| 180 |
link_aggregation_group = flashblade.LinkAggregationGroup(ports=[]) |
| 181 |
link_aggregation_group["ports"] = ports |
| 182 |
if sorted(used_ports) != sorted(new_ports): |
| 183 |
changed = True |
| 184 |
if not module.check_mode: |
| 185 |
res = blade.patch_link_aggregation_groups( |
| 186 |
names=[module.params["name"]], |
| 187 |
link_aggregation_group=link_aggregation_group, |
| 188 |
) |
| 189 |
if res.status_code != 200: |
| 190 |
module.fail_json( |
| 191 |
msg="Failed to update LAG {0}. Error: {1}".format( |
| 192 |
module.params["name"], |
| 193 |
res.errors[0].message, |
| 194 |
) |
| 195 |
) |
| 196 |
else: |
| 197 |
response = list(res.items)[0] |
| 198 |
lagfact = { |
| 199 |
"mac_address": response.mac_address, |
| 200 |
"port_speed": str(response.port_speed / 1000000000) + "Gb/s", |
| 201 |
"lag_speed": str(response.lag_speed / 1000000000) + "Gb/s", |
| 202 |
"status": response.status, |
| 203 |
} |
| 204 |
module.exit_json(changed=changed, lag=lagfact) |
| 205 |
|
| 206 |
|
| 207 |
def create_lag(module, blade): |
| 208 |
"""Create Link Aggregation Group""" |
| 209 |
changed = True |
| 210 |
used_ports = [] |
| 211 |
lagfact = [] |
| 212 |
current_lags = list(blade.get_link_aggregation_groups().items) |
| 213 |
for lag in range(len(current_lags)): |
| 214 |
for port in range(len(current_lags[lag].ports)): |
| 215 |
used_ports.append(current_lags[lag].ports[port].name) |
| 216 |
for lag_port in range(len(module.params["ports"])): |
| 217 |
if ( |
| 218 |
module.params["ports"][lag_port].split(".")[0].upper() |
| 219 |
+ ".FM1." |
| 220 |
+ module.params["ports"][0].split(".")[1].upper() |
| 221 |
) in used_ports: |
| 222 |
module.fail_json( |
| 223 |
msg="Selected port {0} is currently in use by another LAG.".format( |
| 224 |
module.params["ports"][lag_port].upper() |
| 225 |
) |
| 226 |
) |
| 227 |
new_ports = [] |
| 228 |
for new_port in range(len(module.params["ports"])): |
| 229 |
new_ports.append( |
| 230 |
module.params["ports"][new_port].split(".")[0].upper() |
| 231 |
+ ".FM1." |
| 232 |
+ module.params["ports"][new_port].split(".")[1].upper() |
| 233 |
) |
| 234 |
new_ports.append( |
| 235 |
module.params["ports"][new_port].split(".")[0].upper() |
| 236 |
+ ".FM2." |
| 237 |
+ module.params["ports"][new_port].split(".")[1].upper() |
| 238 |
) |
| 239 |
ports = [] |
| 240 |
module.warn("new_ports: {0}".format(new_ports)) |
| 241 |
for final_port in range(len(new_ports)): |
| 242 |
ports.append(flashblade.FixedReference(name=new_ports[final_port])) |
| 243 |
link_aggregation_group = flashblade.LinkAggregationGroup(ports=ports) |
| 244 |
module.warn("LAG: {0}".format(link_aggregation_group)) |
| 245 |
if not module.check_mode: |
| 246 |
res = blade.post_link_aggregation_groups( |
| 247 |
names=[module.params["name"]], link_aggregation_group=link_aggregation_group |
| 248 |
) |
| 249 |
if res.status_code != 200: |
| 250 |
module.fail_json( |
| 251 |
msg="Failed to create LAG {0}. Error: {1}".format( |
| 252 |
module.params["name"], |
| 253 |
res.errors[0].message, |
| 254 |
) |
| 255 |
) |
| 256 |
else: |
| 257 |
response = list(res.items)[0] |
| 258 |
lagfact = { |
| 259 |
"mac_address": response.mac_address, |
| 260 |
"port_speed": str(response.port_speed / 1000000000) + "Gb/s", |
| 261 |
"lag_speed": str(response.lag_speed / 1000000000) + "Gb/s", |
| 262 |
"status": response.status, |
| 263 |
} |
| 264 |
module.exit_json(changed=changed, lag=lagfact) |
| 265 |
|
| 266 |
|
| 267 |
def main(): |
| 268 |
argument_spec = purefb_argument_spec() |
| 269 |
argument_spec.update( |
| 270 |
dict( |
| 271 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 272 |
name=dict(type="str", default="uplink"), |
| 273 |
ports=dict(type="list", elements="str"), |
| 274 |
) |
| 275 |
) |
| 276 |
|
| 277 |
required_if = [["state", "present", ["ports"]]] |
| 278 |
|
| 279 |
module = AnsibleModule( |
| 280 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 281 |
) |
| 282 |
|
| 283 |
if not HAS_PURESTORAGE: |
| 284 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 285 |
|
| 286 |
blade = get_system(module) |
| 287 |
state = module.params["state"] |
| 288 |
|
| 289 |
exists = bool( |
| 290 |
blade.get_link_aggregation_groups(names=[module.params["name"]]).status_code |
| 291 |
== 200 |
| 292 |
) |
| 293 |
if module.params["ports"]: |
| 294 |
|
| 295 |
module.params["ports"] = list(dict.fromkeys(module.params["ports"])) |
| 296 |
if not exists and state == "present": |
| 297 |
create_lag(module, blade) |
| 298 |
elif exists and state == "present": |
| 299 |
update_lag(module, blade) |
| 300 |
elif exists and state == "absent": |
| 301 |
if module.params["name"].lower() == "uplink": |
| 302 |
module.fail_json( |
| 303 |
msg="Preconfigured Link Aggregation Group cannot be deleted" |
| 304 |
) |
| 305 |
else: |
| 306 |
delete_lag(module, blade) |
| 307 |
|
| 308 |
module.exit_json(changed=False) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == "__main__": |
| 312 |
main() |