| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Serge van Ginderachter <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: open_iscsi |
| 11 |
author: |
| 12 |
- Serge van Ginderachter (@srvg) |
| 13 |
short_description: Manage iSCSI targets with Open-iSCSI |
| 14 |
description: |
| 15 |
- Discover targets on given portal, (dis)connect targets, mark targets to manually or auto start, return device nodes of |
| 16 |
connected targets. |
| 17 |
requirements: |
| 18 |
- C(open_iscsi) library and tools C(iscsiadm) |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
portal: |
| 28 |
description: |
| 29 |
- The domain name or IP address of the iSCSI target. |
| 30 |
type: str |
| 31 |
aliases: [ip] |
| 32 |
port: |
| 33 |
description: |
| 34 |
- The port on which the iSCSI target process listens. |
| 35 |
type: str |
| 36 |
default: '3260' |
| 37 |
target: |
| 38 |
description: |
| 39 |
- The iSCSI target name. |
| 40 |
type: str |
| 41 |
aliases: [name, targetname] |
| 42 |
login: |
| 43 |
description: |
| 44 |
- Whether the target node should be connected. |
| 45 |
- When O(target) is omitted, it logins to all available. |
| 46 |
type: bool |
| 47 |
aliases: [state] |
| 48 |
node_auth: |
| 49 |
description: |
| 50 |
- The value for C(node.session.auth.authmethod). |
| 51 |
type: str |
| 52 |
default: CHAP |
| 53 |
node_user: |
| 54 |
description: |
| 55 |
- The value for C(node.session.auth.username). |
| 56 |
type: str |
| 57 |
node_pass: |
| 58 |
description: |
| 59 |
- The value for C(node.session.auth.password). |
| 60 |
type: str |
| 61 |
node_user_in: |
| 62 |
description: |
| 63 |
- The value for C(node.session.auth.username_in). |
| 64 |
type: str |
| 65 |
version_added: 3.8.0 |
| 66 |
node_pass_in: |
| 67 |
description: |
| 68 |
- The value for C(node.session.auth.password_in). |
| 69 |
type: str |
| 70 |
version_added: 3.8.0 |
| 71 |
auto_node_startup: |
| 72 |
description: |
| 73 |
- Whether the target node should be automatically connected at startup. |
| 74 |
type: bool |
| 75 |
aliases: [automatic] |
| 76 |
auto_portal_startup: |
| 77 |
description: |
| 78 |
- Whether the target node portal should be automatically connected at startup. |
| 79 |
type: bool |
| 80 |
version_added: 3.2.0 |
| 81 |
discover: |
| 82 |
description: |
| 83 |
- Whether the list of target nodes on the portal should be (re)discovered and added to the persistent iSCSI database. |
| 84 |
- Keep in mind that C(iscsiadm) discovery resets configuration, like C(node.startup) to manual, hence combined with |
| 85 |
O(auto_node_startup=true) always returns a changed state. |
| 86 |
type: bool |
| 87 |
default: false |
| 88 |
show_nodes: |
| 89 |
description: |
| 90 |
- Whether the list of nodes in the persistent iSCSI database should be returned by the module. |
| 91 |
type: bool |
| 92 |
default: false |
| 93 |
rescan: |
| 94 |
description: |
| 95 |
- Rescan an established session for discovering new targets. |
| 96 |
- When O(target) is omitted, it rescans all sessions. |
| 97 |
type: bool |
| 98 |
default: false |
| 99 |
version_added: 4.1.0 |
| 100 |
""" |
| 101 |
|
| 102 |
EXAMPLES = r""" |
| 103 |
- name: Perform a discovery on sun.com and show available target nodes |
| 104 |
community.general.open_iscsi: |
| 105 |
show_nodes: true |
| 106 |
discover: true |
| 107 |
portal: sun.com |
| 108 |
|
| 109 |
- name: Perform a discovery on 10.1.2.3 and show available target nodes |
| 110 |
community.general.open_iscsi: |
| 111 |
show_nodes: true |
| 112 |
discover: true |
| 113 |
ip: 10.1.2.3 |
| 114 |
|
| 115 |
- name: Discover targets on portal and login to the ones available |
| 116 |
community.general.open_iscsi: |
| 117 |
portal: '{{ iscsi_target }}' |
| 118 |
login: true |
| 119 |
discover: true |
| 120 |
|
| 121 |
- name: Connect to the named target, after updating the local persistent database (cache) |
| 122 |
community.general.open_iscsi: |
| 123 |
login: true |
| 124 |
target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d |
| 125 |
|
| 126 |
- name: Disconnect from the cached named target |
| 127 |
community.general.open_iscsi: |
| 128 |
login: false |
| 129 |
target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d |
| 130 |
|
| 131 |
- name: Override and disable automatic portal login on specific portal |
| 132 |
community.general.open_iscsi: |
| 133 |
login: false |
| 134 |
portal: 10.1.1.250 |
| 135 |
auto_portal_startup: false |
| 136 |
target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d |
| 137 |
|
| 138 |
- name: Rescan one or all established sessions to discover new targets (omit target for all sessions) |
| 139 |
community.general.open_iscsi: |
| 140 |
rescan: true |
| 141 |
target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d |
| 142 |
""" |
| 143 |
|
| 144 |
import glob |
| 145 |
import os |
| 146 |
import re |
| 147 |
import socket |
| 148 |
import time |
| 149 |
|
| 150 |
from ansible.module_utils.basic import AnsibleModule |
| 151 |
|
| 152 |
ISCSIADM = "iscsiadm" |
| 153 |
iscsiadm_cmd = None |
| 154 |
|
| 155 |
|
| 156 |
def format_portal(portal, port): |
| 157 |
"""Format portal address and port for iscsiadm, handling IPv6 bracket notation.""" |
| 158 |
if ":" in portal: |
| 159 |
return f"[{portal}]:{port}" |
| 160 |
return f"{portal}:{port}" |
| 161 |
|
| 162 |
|
| 163 |
def compare_nodelists(l1, l2): |
| 164 |
l1.sort() |
| 165 |
l2.sort() |
| 166 |
return l1 == l2 |
| 167 |
|
| 168 |
|
| 169 |
def iscsi_get_cached_nodes(module, portal=None): |
| 170 |
cmd = [iscsiadm_cmd, "--mode", "node"] |
| 171 |
rc, out, err = module.run_command(cmd) |
| 172 |
|
| 173 |
nodes = [] |
| 174 |
if rc == 0: |
| 175 |
lines = out.splitlines() |
| 176 |
for line in lines: |
| 177 |
# line format is "ip:port,target_portal_group_tag targetname" |
| 178 |
parts = line.split() |
| 179 |
if len(parts) > 2: |
| 180 |
module.fail_json(msg="error parsing output", cmd=cmd) |
| 181 |
target = parts[1] |
| 182 |
parts = parts[0].split(":") |
| 183 |
target_portal = parts[0] |
| 184 |
|
| 185 |
if portal is None or portal == target_portal: |
| 186 |
nodes.append(target) |
| 187 |
|
| 188 |
# older versions of scsiadm don't have nice return codes |
| 189 |
# for newer versions see iscsiadm(8); also usr/iscsiadm.c for details |
| 190 |
# err can contain [N|n]o records... |
| 191 |
elif rc == 21 or (rc == 255 and "o records found" in err): |
| 192 |
pass |
| 193 |
else: |
| 194 |
module.fail_json(cmd=cmd, rc=rc, msg=err) |
| 195 |
|
| 196 |
return nodes |
| 197 |
|
| 198 |
|
| 199 |
def iscsi_discover(module, portal, port): |
| 200 |
cmd = [iscsiadm_cmd, "--mode", "discovery", "--type", "sendtargets", "--portal", format_portal(portal, port)] |
| 201 |
module.run_command(cmd, check_rc=True) |
| 202 |
|
| 203 |
|
| 204 |
def iscsi_rescan(module, target=None): |
| 205 |
if target is None: |
| 206 |
cmd = [iscsiadm_cmd, "--mode", "session", "--rescan"] |
| 207 |
else: |
| 208 |
cmd = [iscsiadm_cmd, "--mode", "node", "--rescan", "-T", target] |
| 209 |
rc, out, err = module.run_command(cmd) |
| 210 |
return out |
| 211 |
|
| 212 |
|
| 213 |
def target_loggedon(module, target, portal=None, port=None): |
| 214 |
cmd = [iscsiadm_cmd, "--mode", "session"] |
| 215 |
rc, out, err = module.run_command(cmd) |
| 216 |
|
| 217 |
if portal is None: |
| 218 |
portal = "" |
| 219 |
if port is None: |
| 220 |
port = "" |
| 221 |
|
| 222 |
if rc == 0: |
| 223 |
search_re = f"{re.escape(portal)}:{port}.*{re.escape(target)}" |
| 224 |
return re.search(search_re, out) is not None |
| 225 |
elif rc == 21: |
| 226 |
return False |
| 227 |
else: |
| 228 |
module.fail_json(cmd=cmd, rc=rc, msg=err) |
| 229 |
|
| 230 |
|
| 231 |
def target_login(module, target, check_rc, portal=None, port=None): |
| 232 |
node_auth = module.params["node_auth"] |
| 233 |
node_user = module.params["node_user"] |
| 234 |
node_pass = module.params["node_pass"] |
| 235 |
node_user_in = module.params["node_user_in"] |
| 236 |
node_pass_in = module.params["node_pass_in"] |
| 237 |
|
| 238 |
if node_user: |
| 239 |
params = [ |
| 240 |
("node.session.auth.authmethod", node_auth), |
| 241 |
("node.session.auth.username", node_user), |
| 242 |
("node.session.auth.password", node_pass), |
| 243 |
] |
| 244 |
for name, value in params: |
| 245 |
cmd = [ |
| 246 |
iscsiadm_cmd, |
| 247 |
"--mode", |
| 248 |
"node", |
| 249 |
"--targetname", |
| 250 |
target, |
| 251 |
"--op=update", |
| 252 |
"--name", |
| 253 |
name, |
| 254 |
"--value", |
| 255 |
value, |
| 256 |
] |
| 257 |
module.run_command(cmd, check_rc=check_rc) |
| 258 |
|
| 259 |
if node_user_in: |
| 260 |
params = [("node.session.auth.username_in", node_user_in), ("node.session.auth.password_in", node_pass_in)] |
| 261 |
for name, value in params: |
| 262 |
cmd = [ |
| 263 |
iscsiadm_cmd, |
| 264 |
"--mode", |
| 265 |
"node", |
| 266 |
"--targetname", |
| 267 |
target, |
| 268 |
"--op=update", |
| 269 |
"--name", |
| 270 |
name, |
| 271 |
"--value", |
| 272 |
value, |
| 273 |
] |
| 274 |
module.run_command(cmd, check_rc=check_rc) |
| 275 |
|
| 276 |
cmd = [iscsiadm_cmd, "--mode", "node", "--targetname", target, "--login"] |
| 277 |
if portal is not None and port is not None: |
| 278 |
cmd.append("--portal") |
| 279 |
cmd.append(format_portal(portal, port)) |
| 280 |
|
| 281 |
rc, out, err = module.run_command(cmd, check_rc=check_rc) |
| 282 |
return rc |
| 283 |
|
| 284 |
|
| 285 |
def target_logout(module, target): |
| 286 |
cmd = [iscsiadm_cmd, "--mode", "node", "--targetname", target, "--logout"] |
| 287 |
module.run_command(cmd, check_rc=True) |
| 288 |
|
| 289 |
|
| 290 |
def target_device_node(target): |
| 291 |
# if anyone know a better way to find out which devicenodes get created for |
| 292 |
# a given target... |
| 293 |
|
| 294 |
devices = glob.glob(f"/dev/disk/by-path/*{target}*") |
| 295 |
devdisks = [] |
| 296 |
for dev in devices: |
| 297 |
# exclude partitions |
| 298 |
if "-part" not in dev: |
| 299 |
devdisk = os.path.realpath(dev) |
| 300 |
# only add once (multi-path?) |
| 301 |
if devdisk not in devdisks: |
| 302 |
devdisks.append(devdisk) |
| 303 |
return devdisks |
| 304 |
|
| 305 |
|
| 306 |
def target_isauto(module, target, portal=None, port=None): |
| 307 |
cmd = [iscsiadm_cmd, "--mode", "node", "--targetname", target] |
| 308 |
|
| 309 |
if portal is not None and port is not None: |
| 310 |
cmd.append("--portal") |
| 311 |
cmd.append(format_portal(portal, port)) |
| 312 |
|
| 313 |
dummy, out, dummy = module.run_command(cmd, check_rc=True) |
| 314 |
|
| 315 |
lines = out.splitlines() |
| 316 |
for line in lines: |
| 317 |
if "node.startup" in line: |
| 318 |
return "automatic" in line |
| 319 |
return False |
| 320 |
|
| 321 |
|
| 322 |
def target_setauto(module, target, portal=None, port=None): |
| 323 |
cmd = [ |
| 324 |
iscsiadm_cmd, |
| 325 |
"--mode", |
| 326 |
"node", |
| 327 |
"--targetname", |
| 328 |
target, |
| 329 |
"--op=update", |
| 330 |
"--name", |
| 331 |
"node.startup", |
| 332 |
"--value", |
| 333 |
"automatic", |
| 334 |
] |
| 335 |
|
| 336 |
if portal is not None and port is not None: |
| 337 |
cmd.append("--portal") |
| 338 |
cmd.append(format_portal(portal, port)) |
| 339 |
|
| 340 |
module.run_command(cmd, check_rc=True) |
| 341 |
|
| 342 |
|
| 343 |
def target_setmanual(module, target, portal=None, port=None): |
| 344 |
cmd = [ |
| 345 |
iscsiadm_cmd, |
| 346 |
"--mode", |
| 347 |
"node", |
| 348 |
"--targetname", |
| 349 |
target, |
| 350 |
"--op=update", |
| 351 |
"--name", |
| 352 |
"node.startup", |
| 353 |
"--value", |
| 354 |
"manual", |
| 355 |
] |
| 356 |
|
| 357 |
if portal is not None and port is not None: |
| 358 |
cmd.append("--portal") |
| 359 |
cmd.append(format_portal(portal, port)) |
| 360 |
|
| 361 |
module.run_command(cmd, check_rc=True) |
| 362 |
|
| 363 |
|
| 364 |
def main(): |
| 365 |
# load ansible module object |
| 366 |
module = AnsibleModule( |
| 367 |
argument_spec=dict( |
| 368 |
# target |
| 369 |
portal=dict(type="str", aliases=["ip"]), |
| 370 |
port=dict(type="str", default="3260"), |
| 371 |
target=dict(type="str", aliases=["name", "targetname"]), |
| 372 |
node_auth=dict(type="str", default="CHAP"), |
| 373 |
node_user=dict(type="str"), |
| 374 |
node_pass=dict(type="str", no_log=True), |
| 375 |
node_user_in=dict(type="str"), |
| 376 |
node_pass_in=dict(type="str", no_log=True), |
| 377 |
# actions |
| 378 |
login=dict(type="bool", aliases=["state"]), |
| 379 |
auto_node_startup=dict(type="bool", aliases=["automatic"]), |
| 380 |
auto_portal_startup=dict(type="bool"), |
| 381 |
discover=dict(type="bool", default=False), |
| 382 |
show_nodes=dict(type="bool", default=False), |
| 383 |
rescan=dict(type="bool", default=False), |
| 384 |
), |
| 385 |
required_together=[["node_user", "node_pass"], ["node_user_in", "node_pass_in"]], |
| 386 |
required_if=[ |
| 387 |
("discover", True, ["portal"]), |
| 388 |
("auto_node_startup", True, ["target"]), |
| 389 |
("auto_portal_startup", True, ["target"]), |
| 390 |
], |
| 391 |
supports_check_mode=True, |
| 392 |
) |
| 393 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 394 |
|
| 395 |
global iscsiadm_cmd |
| 396 |
iscsiadm_cmd = module.get_bin_path("iscsiadm", required=True) |
| 397 |
|
| 398 |
# parameters |
| 399 |
portal = module.params["portal"] |
| 400 |
if portal: |
| 401 |
try: |
| 402 |
portal = socket.getaddrinfo(portal, None)[0][4][0] |
| 403 |
except socket.gaierror: |
| 404 |
module.fail_json(msg="Portal address is incorrect") |
| 405 |
|
| 406 |
target = module.params["target"] |
| 407 |
port = module.params["port"] |
| 408 |
login = module.params["login"] |
| 409 |
automatic = module.params["auto_node_startup"] |
| 410 |
automatic_portal = module.params["auto_portal_startup"] |
| 411 |
discover = module.params["discover"] |
| 412 |
show_nodes = module.params["show_nodes"] |
| 413 |
rescan = module.params["rescan"] |
| 414 |
|
| 415 |
check = module.check_mode |
| 416 |
|
| 417 |
cached = iscsi_get_cached_nodes(module, portal) |
| 418 |
|
| 419 |
# return json dict |
| 420 |
result = {"changed": False} |
| 421 |
login_to_all_nodes = False |
| 422 |
check_rc = True |
| 423 |
|
| 424 |
if discover: |
| 425 |
if check: |
| 426 |
nodes = cached |
| 427 |
else: |
| 428 |
iscsi_discover(module, portal, port) |
| 429 |
nodes = iscsi_get_cached_nodes(module, portal) |
| 430 |
if not compare_nodelists(cached, nodes): |
| 431 |
result["changed"] |= True |
| 432 |
result["cache_updated"] = True |
| 433 |
else: |
| 434 |
nodes = cached |
| 435 |
|
| 436 |
if login is not None or automatic is not None: |
| 437 |
if target is None: |
| 438 |
if len(nodes) > 1: |
| 439 |
# Disable strict return code checking if there are multiple targets |
| 440 |
# That will allow to skip target where we have no rights to login |
| 441 |
login_to_all_nodes = True |
| 442 |
check_rc = False |
| 443 |
else: |
| 444 |
# check given target is in cache |
| 445 |
check_target = False |
| 446 |
for node in nodes: |
| 447 |
if node == target: |
| 448 |
check_target = True |
| 449 |
break |
| 450 |
if not check_target: |
| 451 |
module.fail_json(msg="Specified target not found") |
| 452 |
|
| 453 |
if show_nodes: |
| 454 |
result["nodes"] = nodes |
| 455 |
|
| 456 |
if login is not None: |
| 457 |
if login_to_all_nodes: |
| 458 |
result["devicenodes"] = [] |
| 459 |
for index_target in nodes: |
| 460 |
loggedon = target_loggedon(module, index_target, portal, port) |
| 461 |
if (login and loggedon) or (not login and not loggedon): |
| 462 |
result["changed"] |= False |
| 463 |
if login: |
| 464 |
result["devicenodes"] += target_device_node(index_target) |
| 465 |
elif not check: |
| 466 |
if login: |
| 467 |
login_result = target_login(module, index_target, check_rc, portal, port) |
| 468 |
# give udev some time |
| 469 |
time.sleep(1) |
| 470 |
result["devicenodes"] += target_device_node(index_target) |
| 471 |
else: |
| 472 |
target_logout(module, index_target) |
| 473 |
# Check if there are multiple targets on a single portal and |
| 474 |
# do not mark the task changed if host could not login to one of them |
| 475 |
if len(nodes) > 1 and login_result == 24: |
| 476 |
result["changed"] |= False |
| 477 |
result["connection_changed"] = False |
| 478 |
else: |
| 479 |
result["changed"] |= True |
| 480 |
result["connection_changed"] = True |
| 481 |
else: |
| 482 |
result["changed"] |= True |
| 483 |
result["connection_changed"] = True |
| 484 |
else: |
| 485 |
loggedon = target_loggedon(module, target, portal, port) |
| 486 |
if (login and loggedon) or (not login and not loggedon): |
| 487 |
result["changed"] |= False |
| 488 |
if login: |
| 489 |
result["devicenodes"] = target_device_node(target) |
| 490 |
elif not check: |
| 491 |
if login: |
| 492 |
target_login(module, target, portal, port) |
| 493 |
# give udev some time |
| 494 |
time.sleep(1) |
| 495 |
result["devicenodes"] = target_device_node(target) |
| 496 |
else: |
| 497 |
target_logout(module, target) |
| 498 |
result["changed"] |= True |
| 499 |
result["connection_changed"] = True |
| 500 |
else: |
| 501 |
result["changed"] |= True |
| 502 |
result["connection_changed"] = True |
| 503 |
|
| 504 |
if automatic is not None and not login_to_all_nodes: |
| 505 |
isauto = target_isauto(module, target) |
| 506 |
if (automatic and isauto) or (not automatic and not isauto): |
| 507 |
result["changed"] |= False |
| 508 |
result["automatic_changed"] = False |
| 509 |
elif not check: |
| 510 |
if automatic: |
| 511 |
target_setauto(module, target) |
| 512 |
else: |
| 513 |
target_setmanual(module, target) |
| 514 |
result["changed"] |= True |
| 515 |
result["automatic_changed"] = True |
| 516 |
else: |
| 517 |
result["changed"] |= True |
| 518 |
result["automatic_changed"] = True |
| 519 |
|
| 520 |
if automatic_portal is not None and not login_to_all_nodes: |
| 521 |
isauto = target_isauto(module, target, portal, port) |
| 522 |
if (automatic_portal and isauto) or (not automatic_portal and not isauto): |
| 523 |
result["changed"] |= False |
| 524 |
result["automatic_portal_changed"] = False |
| 525 |
elif not check: |
| 526 |
if automatic_portal: |
| 527 |
target_setauto(module, target, portal, port) |
| 528 |
else: |
| 529 |
target_setmanual(module, target, portal, port) |
| 530 |
result["changed"] |= True |
| 531 |
result["automatic_portal_changed"] = True |
| 532 |
else: |
| 533 |
result["changed"] |= True |
| 534 |
result["automatic_portal_changed"] = True |
| 535 |
|
| 536 |
if rescan is not False: |
| 537 |
result["changed"] = True |
| 538 |
result["sessions"] = iscsi_rescan(module, target) |
| 539 |
|
| 540 |
module.exit_json(**result) |
| 541 |
|
| 542 |
|
| 543 |
if __name__ == "__main__": |
| 544 |
main() |