1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

open_iscsi - minor refactoring (#3286)

* open_iscsi - minor refactoring

* added changelog fragment
This commit is contained in:
Alexei Znamensky 2021-08-31 23:09:29 +12:00 committed by GitHub
parent edd7b84285
commit 1f5345881d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 66 deletions

View file

@ -0,0 +1,4 @@
minor_changes:
- open_iscsi - minor refactoring (https://github.com/ansible-collections/community.general/pull/3286).
bugfixes:
- open_iscsi - calling ``run_command`` with arguments as ``list`` instead of ``str`` (https://github.com/ansible-collections/community.general/pull/3286).

View file

@ -125,6 +125,7 @@ import time
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
ISCSIADM = 'iscsiadm' ISCSIADM = 'iscsiadm'
iscsiadm_cmd = None
def compare_nodelists(l1, l2): def compare_nodelists(l1, l2):
@ -134,12 +135,12 @@ def compare_nodelists(l1, l2):
def iscsi_get_cached_nodes(module, portal=None): def iscsi_get_cached_nodes(module, portal=None):
cmd = '%s --mode node' % iscsiadm_cmd cmd = [iscsiadm_cmd, '--mode', 'node']
(rc, out, err) = module.run_command(cmd) rc, out, err = module.run_command(cmd)
nodes = []
if rc == 0: if rc == 0:
lines = out.splitlines() lines = out.splitlines()
nodes = []
for line in lines: for line in lines:
# line format is "ip:port,target_portal_group_tag targetname" # line format is "ip:port,target_portal_group_tag targetname"
parts = line.split() parts = line.split()
@ -156,7 +157,7 @@ def iscsi_get_cached_nodes(module, portal=None):
# for newer versions see iscsiadm(8); also usr/iscsiadm.c for details # for newer versions see iscsiadm(8); also usr/iscsiadm.c for details
# err can contain [N|n]o records... # err can contain [N|n]o records...
elif rc == 21 or (rc == 255 and "o records found" in err): elif rc == 21 or (rc == 255 and "o records found" in err):
nodes = [] pass
else: else:
module.fail_json(cmd=cmd, rc=rc, msg=err) module.fail_json(cmd=cmd, rc=rc, msg=err)
@ -164,16 +165,13 @@ def iscsi_get_cached_nodes(module, portal=None):
def iscsi_discover(module, portal, port): def iscsi_discover(module, portal, port):
cmd = '%s --mode discovery --type sendtargets --portal %s:%s' % (iscsiadm_cmd, portal, port) cmd = [iscsiadm_cmd, '--mode', 'discovery', '--type', 'sendtargets', '--portal', '%s:%s' % (portal, port)]
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def target_loggedon(module, target, portal=None, port=None): def target_loggedon(module, target, portal=None, port=None):
cmd = '%s --mode session' % iscsiadm_cmd cmd = [iscsiadm_cmd, '--mode', 'session']
(rc, out, err) = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if portal is None: if portal is None:
portal = "" portal = ""
@ -199,30 +197,23 @@ def target_login(module, target, portal=None, port=None):
('node.session.auth.username', node_user), ('node.session.auth.username', node_user),
('node.session.auth.password', node_pass)] ('node.session.auth.password', node_pass)]
for (name, value) in params: for (name, value) in params:
cmd = '%s --mode node --targetname %s --op=update --name %s --value %s' % (iscsiadm_cmd, target, name, value) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target, '--op=update', '--name', name, '--value', value]
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
cmd = '%s --mode node --targetname %s --login' % (iscsiadm_cmd, target) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target, '--login']
if portal is not None and port is not None: if portal is not None and port is not None:
cmd += ' --portal %s:%s' % (portal, port) cmd.append('--portal')
cmd.append('%s:%s' % (portal, port))
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def target_logout(module, target): def target_logout(module, target):
cmd = '%s --mode node --targetname %s --logout' % (iscsiadm_cmd, target) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target, '--logout']
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def target_device_node(module, target): def target_device_node(target):
# if anyone know a better way to find out which devicenodes get created for # if anyone know a better way to find out which devicenodes get created for
# a given target... # a given target...
@ -239,51 +230,39 @@ def target_device_node(module, target):
def target_isauto(module, target, portal=None, port=None): def target_isauto(module, target, portal=None, port=None):
cmd = '%s --mode node --targetname %s' % (iscsiadm_cmd, target) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target]
if portal is not None: if portal is not None and port is not None:
if port is not None: cmd.append('--portal')
portal = '%s:%s' % (portal, port) cmd.append('%s:%s' % (portal, port))
cmd = '%s --portal %s' % (cmd, portal)
(rc, out, err) = module.run_command(cmd) dummy, out, dummy = module.run_command(cmd, check_rc=True)
if rc == 0:
lines = out.splitlines() lines = out.splitlines()
for line in lines: for line in lines:
if 'node.startup' in line: if 'node.startup' in line:
return 'automatic' in line return 'automatic' in line
return False return False
else:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def target_setauto(module, target, portal=None, port=None): def target_setauto(module, target, portal=None, port=None):
cmd = '%s --mode node --targetname %s --op=update --name node.startup --value automatic' % (iscsiadm_cmd, target) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target, '--op=update', '--name', 'node.startup', '--value', 'automatic']
if portal is not None: if portal is not None and port is not None:
if port is not None: cmd.append('--portal')
portal = '%s:%s' % (portal, port) cmd.append('%s:%s' % (portal, port))
cmd = '%s --portal %s' % (cmd, portal)
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def target_setmanual(module, target, portal=None, port=None): def target_setmanual(module, target, portal=None, port=None):
cmd = '%s --mode node --targetname %s --op=update --name node.startup --value manual' % (iscsiadm_cmd, target) cmd = [iscsiadm_cmd, '--mode', 'node', '--targetname', target, '--op=update', '--name', 'node.startup', '--value', 'manual']
if portal is not None: if portal is not None and port is not None:
if port is not None: cmd.append('--portal')
portal = '%s:%s' % (portal, port) cmd.append('%s:%s' % (portal, port))
cmd = '%s --portal %s' % (cmd, portal)
(rc, out, err) = module.run_command(cmd) module.run_command(cmd, check_rc=True)
if rc > 0:
module.fail_json(cmd=cmd, rc=rc, msg=err)
def main(): def main():
@ -308,6 +287,7 @@ def main():
), ),
required_together=[['node_user', 'node_pass']], required_together=[['node_user', 'node_pass']],
required_if=[('discover', True, ['portal'])],
supports_check_mode=True, supports_check_mode=True,
) )
@ -335,13 +315,10 @@ def main():
cached = iscsi_get_cached_nodes(module, portal) cached = iscsi_get_cached_nodes(module, portal)
# return json dict # return json dict
result = {} result = {'changed': False}
result['changed'] = False
if discover: if discover:
if portal is None: if check:
module.fail_json(msg="Need to specify at least the portal (ip) to discover")
elif check:
nodes = cached nodes = cached
else: else:
iscsi_discover(module, portal, port) iscsi_discover(module, portal, port)
@ -376,13 +353,13 @@ def main():
if (login and loggedon) or (not login and not loggedon): if (login and loggedon) or (not login and not loggedon):
result['changed'] |= False result['changed'] |= False
if login: if login:
result['devicenodes'] = target_device_node(module, target) result['devicenodes'] = target_device_node(target)
elif not check: elif not check:
if login: if login:
target_login(module, target, portal, port) target_login(module, target, portal, port)
# give udev some time # give udev some time
time.sleep(1) time.sleep(1)
result['devicenodes'] = target_device_node(module, target) result['devicenodes'] = target_device_node(target)
else: else:
target_logout(module, target) target_logout(module, target)
result['changed'] |= True result['changed'] |= True