mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Reformat everything.
This commit is contained in:
@@ -177,7 +177,7 @@ def _fs_exists(module, filesystem):
|
||||
:param community.general.filesystem: filesystem name.
|
||||
:return: True or False.
|
||||
"""
|
||||
lsfs_cmd = module.get_bin_path('lsfs', True)
|
||||
lsfs_cmd = module.get_bin_path("lsfs", True)
|
||||
rc, lsfs_out, err = module.run_command([lsfs_cmd, "-l", filesystem])
|
||||
if rc == 1:
|
||||
if re.findall("No record matching", err):
|
||||
@@ -187,7 +187,6 @@ def _fs_exists(module, filesystem):
|
||||
module.fail_json(msg=f"Failed to run lsfs. Error message: {err}")
|
||||
|
||||
else:
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -200,14 +199,14 @@ def _check_nfs_device(module, nfs_host, device):
|
||||
:param device: device parameter, remote export.
|
||||
:return: True or False.
|
||||
"""
|
||||
showmount_cmd = module.get_bin_path('showmount', True)
|
||||
showmount_cmd = module.get_bin_path("showmount", True)
|
||||
rc, showmount_out, err = module.run_command([showmount_cmd, "-a", nfs_host])
|
||||
if rc != 0:
|
||||
module.fail_json(msg=f"Failed to run showmount. Error message: {err}")
|
||||
else:
|
||||
showmount_data = showmount_out.splitlines()
|
||||
for line in showmount_data:
|
||||
if line.split(':')[1] == device:
|
||||
if line.split(":")[1] == device:
|
||||
return True
|
||||
|
||||
return False
|
||||
@@ -222,7 +221,7 @@ def _validate_vg(module, vg):
|
||||
:return: True (VG in varyon state) or False (VG in varyoff state) or
|
||||
None (VG does not exist), message.
|
||||
"""
|
||||
lsvg_cmd = module.get_bin_path('lsvg', True)
|
||||
lsvg_cmd = module.get_bin_path("lsvg", True)
|
||||
rc, current_active_vgs, err = module.run_command([lsvg_cmd, "-o"])
|
||||
if rc != 0:
|
||||
module.fail_json(msg=f"Failed executing {lsvg_cmd} command.")
|
||||
@@ -243,9 +242,9 @@ def _validate_vg(module, vg):
|
||||
|
||||
|
||||
def resize_fs(module, filesystem, size):
|
||||
""" Resize LVM file system. """
|
||||
"""Resize LVM file system."""
|
||||
|
||||
chfs_cmd = module.get_bin_path('chfs', True)
|
||||
chfs_cmd = module.get_bin_path("chfs", True)
|
||||
if not module.check_mode:
|
||||
rc, chfs_out, err = module.run_command([chfs_cmd, "-a", f"size={size}", filesystem])
|
||||
|
||||
@@ -253,14 +252,14 @@ def resize_fs(module, filesystem, size):
|
||||
changed = False
|
||||
return changed, chfs_out
|
||||
elif rc != 0:
|
||||
if re.findall('Maximum allocation for logical', err):
|
||||
if re.findall("Maximum allocation for logical", err):
|
||||
changed = False
|
||||
return changed, err
|
||||
else:
|
||||
module.fail_json(msg=f"Failed to run chfs. Error message: {err}")
|
||||
|
||||
else:
|
||||
if re.findall('The filesystem size is already', chfs_out):
|
||||
if re.findall("The filesystem size is already", chfs_out):
|
||||
changed = False
|
||||
else:
|
||||
changed = True
|
||||
@@ -268,48 +267,50 @@ def resize_fs(module, filesystem, size):
|
||||
return changed, chfs_out
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
|
||||
def create_fs(
|
||||
module, fs_type, filesystem, vg, device, size, mount_group, auto_mount,
|
||||
account_subsystem, permissions, nfs_server, attributes):
|
||||
""" Create LVM file system or NFS remote mount point. """
|
||||
module,
|
||||
fs_type,
|
||||
filesystem,
|
||||
vg,
|
||||
device,
|
||||
size,
|
||||
mount_group,
|
||||
auto_mount,
|
||||
account_subsystem,
|
||||
permissions,
|
||||
nfs_server,
|
||||
attributes,
|
||||
):
|
||||
"""Create LVM file system or NFS remote mount point."""
|
||||
|
||||
attributes = ' -a '.join(attributes)
|
||||
attributes = " -a ".join(attributes)
|
||||
|
||||
# Parameters definition.
|
||||
account_subsys_opt = {
|
||||
True: '-t yes',
|
||||
False: '-t no'
|
||||
}
|
||||
account_subsys_opt = {True: "-t yes", False: "-t no"}
|
||||
|
||||
if nfs_server is not None:
|
||||
auto_mount_opt = {
|
||||
True: '-A',
|
||||
False: '-a'
|
||||
}
|
||||
auto_mount_opt = {True: "-A", False: "-a"}
|
||||
|
||||
else:
|
||||
auto_mount_opt = {
|
||||
True: '-A yes',
|
||||
False: '-A no'
|
||||
}
|
||||
auto_mount_opt = {True: "-A yes", False: "-A no"}
|
||||
|
||||
if size is None:
|
||||
size = ''
|
||||
size = ""
|
||||
else:
|
||||
size = f"-a size={size}"
|
||||
|
||||
if device is None:
|
||||
device = ''
|
||||
device = ""
|
||||
else:
|
||||
device = f"-d {device}"
|
||||
|
||||
if vg is None:
|
||||
vg = ''
|
||||
vg = ""
|
||||
else:
|
||||
vg_state, msg = _validate_vg(module, vg)
|
||||
if vg_state:
|
||||
@@ -320,7 +321,7 @@ def create_fs(
|
||||
return changed, msg
|
||||
|
||||
if mount_group is None:
|
||||
mount_group = ''
|
||||
mount_group = ""
|
||||
|
||||
else:
|
||||
mount_group = f"-u {mount_group}"
|
||||
@@ -330,9 +331,11 @@ def create_fs(
|
||||
|
||||
if nfs_server is not None:
|
||||
# Creates a NFS file system.
|
||||
mknfsmnt_cmd = module.get_bin_path('mknfsmnt', True)
|
||||
mknfsmnt_cmd = module.get_bin_path("mknfsmnt", True)
|
||||
if not module.check_mode:
|
||||
rc, mknfsmnt_out, err = module.run_command([mknfsmnt_cmd, "-f", filesystem, device, "-h", nfs_server, "-t", permissions, auto_mount, "-w", "bg"])
|
||||
rc, mknfsmnt_out, err = module.run_command(
|
||||
[mknfsmnt_cmd, "-f", filesystem, device, "-h", nfs_server, "-t", permissions, auto_mount, "-w", "bg"]
|
||||
)
|
||||
if rc != 0:
|
||||
module.fail_json(msg=f"Failed to run mknfsmnt. Error message: {err}")
|
||||
else:
|
||||
@@ -342,13 +345,13 @@ def create_fs(
|
||||
return changed, msg
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
else:
|
||||
# Creates a LVM file system.
|
||||
crfs_cmd = module.get_bin_path('crfs', True)
|
||||
crfs_cmd = module.get_bin_path("crfs", True)
|
||||
if not module.check_mode:
|
||||
cmd = [crfs_cmd]
|
||||
|
||||
@@ -401,7 +404,8 @@ def create_fs(
|
||||
|
||||
if rc == 10:
|
||||
module.exit_json(
|
||||
msg=f"Using a existent previously defined logical volume, volume group needs to be empty. {err}")
|
||||
msg=f"Using a existent previously defined logical volume, volume group needs to be empty. {err}"
|
||||
)
|
||||
|
||||
elif rc != 0:
|
||||
module.fail_json(msg=f"Failed to run {cmd}. Error message: {err}")
|
||||
@@ -411,23 +415,20 @@ def create_fs(
|
||||
return changed, crfs_out
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
|
||||
def remove_fs(module, filesystem, rm_mount_point):
|
||||
""" Remove an LVM file system or NFS entry. """
|
||||
"""Remove an LVM file system or NFS entry."""
|
||||
|
||||
# Command parameters.
|
||||
rm_mount_point_opt = {
|
||||
True: '-r',
|
||||
False: ''
|
||||
}
|
||||
rm_mount_point_opt = {True: "-r", False: ""}
|
||||
|
||||
rm_mount_point = rm_mount_point_opt[rm_mount_point]
|
||||
|
||||
rmfs_cmd = module.get_bin_path('rmfs', True)
|
||||
rmfs_cmd = module.get_bin_path("rmfs", True)
|
||||
if not module.check_mode:
|
||||
cmd = [rmfs_cmd, "-r", rm_mount_point, filesystem]
|
||||
rc, rmfs_out, err = module.run_command(cmd)
|
||||
@@ -442,14 +443,14 @@ def remove_fs(module, filesystem, rm_mount_point):
|
||||
return changed, msg
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
|
||||
def mount_fs(module, filesystem):
|
||||
""" Mount a file system. """
|
||||
mount_cmd = module.get_bin_path('mount', True)
|
||||
"""Mount a file system."""
|
||||
mount_cmd = module.get_bin_path("mount", True)
|
||||
|
||||
if not module.check_mode:
|
||||
rc, mount_out, err = module.run_command([mount_cmd, filesystem])
|
||||
@@ -462,14 +463,14 @@ def mount_fs(module, filesystem):
|
||||
return changed, msg
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
|
||||
def unmount_fs(module, filesystem):
|
||||
""" Unmount a file system."""
|
||||
unmount_cmd = module.get_bin_path('unmount', True)
|
||||
"""Unmount a file system."""
|
||||
unmount_cmd = module.get_bin_path("unmount", True)
|
||||
|
||||
if not module.check_mode:
|
||||
rc, unmount_out, err = module.run_command([unmount_cmd, filesystem])
|
||||
@@ -482,7 +483,7 @@ def unmount_fs(module, filesystem):
|
||||
return changed, msg
|
||||
else:
|
||||
changed = True
|
||||
msg = ''
|
||||
msg = ""
|
||||
|
||||
return changed, msg
|
||||
|
||||
@@ -490,114 +491,150 @@ def unmount_fs(module, filesystem):
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
account_subsystem=dict(type='bool', default=False),
|
||||
attributes=dict(type='list', elements='str', default=["agblksize=4096", "isnapshot=no"]),
|
||||
auto_mount=dict(type='bool', default=True),
|
||||
device=dict(type='str'),
|
||||
filesystem=dict(type='str', required=True),
|
||||
fs_type=dict(type='str', default='jfs2'),
|
||||
permissions=dict(type='str', default='rw', choices=['rw', 'ro']),
|
||||
mount_group=dict(type='str'),
|
||||
nfs_server=dict(type='str'),
|
||||
rm_mount_point=dict(type='bool', default=False),
|
||||
size=dict(type='str'),
|
||||
state=dict(type='str', default='present', choices=['absent', 'mounted', 'present', 'unmounted']),
|
||||
vg=dict(type='str'),
|
||||
account_subsystem=dict(type="bool", default=False),
|
||||
attributes=dict(type="list", elements="str", default=["agblksize=4096", "isnapshot=no"]),
|
||||
auto_mount=dict(type="bool", default=True),
|
||||
device=dict(type="str"),
|
||||
filesystem=dict(type="str", required=True),
|
||||
fs_type=dict(type="str", default="jfs2"),
|
||||
permissions=dict(type="str", default="rw", choices=["rw", "ro"]),
|
||||
mount_group=dict(type="str"),
|
||||
nfs_server=dict(type="str"),
|
||||
rm_mount_point=dict(type="bool", default=False),
|
||||
size=dict(type="str"),
|
||||
state=dict(type="str", default="present", choices=["absent", "mounted", "present", "unmounted"]),
|
||||
vg=dict(type="str"),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
account_subsystem = module.params['account_subsystem']
|
||||
attributes = module.params['attributes']
|
||||
auto_mount = module.params['auto_mount']
|
||||
device = module.params['device']
|
||||
fs_type = module.params['fs_type']
|
||||
permissions = module.params['permissions']
|
||||
mount_group = module.params['mount_group']
|
||||
filesystem = module.params['filesystem']
|
||||
nfs_server = module.params['nfs_server']
|
||||
rm_mount_point = module.params['rm_mount_point']
|
||||
size = module.params['size']
|
||||
state = module.params['state']
|
||||
vg = module.params['vg']
|
||||
account_subsystem = module.params["account_subsystem"]
|
||||
attributes = module.params["attributes"]
|
||||
auto_mount = module.params["auto_mount"]
|
||||
device = module.params["device"]
|
||||
fs_type = module.params["fs_type"]
|
||||
permissions = module.params["permissions"]
|
||||
mount_group = module.params["mount_group"]
|
||||
filesystem = module.params["filesystem"]
|
||||
nfs_server = module.params["nfs_server"]
|
||||
rm_mount_point = module.params["rm_mount_point"]
|
||||
size = module.params["size"]
|
||||
state = module.params["state"]
|
||||
vg = module.params["vg"]
|
||||
|
||||
result = dict(
|
||||
changed=False,
|
||||
msg='',
|
||||
msg="",
|
||||
)
|
||||
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
fs_mounted = ismount(filesystem)
|
||||
fs_exists = _fs_exists(module, filesystem)
|
||||
|
||||
# Check if fs is mounted or exists.
|
||||
if fs_mounted or fs_exists:
|
||||
result['msg'] = f"File system {filesystem} already exists."
|
||||
result['changed'] = False
|
||||
result["msg"] = f"File system {filesystem} already exists."
|
||||
result["changed"] = False
|
||||
|
||||
# If parameter size was passed, resize fs.
|
||||
if size is not None:
|
||||
result['changed'], result['msg'] = resize_fs(module, filesystem, size)
|
||||
result["changed"], result["msg"] = resize_fs(module, filesystem, size)
|
||||
|
||||
# If fs doesn't exist, create it.
|
||||
else:
|
||||
# Check if fs will be a NFS device.
|
||||
if nfs_server is not None:
|
||||
if device is None:
|
||||
result['msg'] = 'Parameter "device" is required when "nfs_server" is defined.'
|
||||
result["msg"] = 'Parameter "device" is required when "nfs_server" is defined.'
|
||||
module.fail_json(**result)
|
||||
else:
|
||||
# Create a fs from NFS export.
|
||||
if _check_nfs_device(module, nfs_server, device):
|
||||
result['changed'], result['msg'] = create_fs(
|
||||
module, fs_type, filesystem, vg, device, size, mount_group, auto_mount, account_subsystem, permissions, nfs_server, attributes)
|
||||
result["changed"], result["msg"] = create_fs(
|
||||
module,
|
||||
fs_type,
|
||||
filesystem,
|
||||
vg,
|
||||
device,
|
||||
size,
|
||||
mount_group,
|
||||
auto_mount,
|
||||
account_subsystem,
|
||||
permissions,
|
||||
nfs_server,
|
||||
attributes,
|
||||
)
|
||||
|
||||
if device is None:
|
||||
if vg is None:
|
||||
result['msg'] = 'Required parameter "device" and/or "vg" is missing for filesystem creation.'
|
||||
result["msg"] = 'Required parameter "device" and/or "vg" is missing for filesystem creation.'
|
||||
module.fail_json(**result)
|
||||
else:
|
||||
# Create a fs from
|
||||
result['changed'], result['msg'] = create_fs(
|
||||
module, fs_type, filesystem, vg, device, size, mount_group, auto_mount, account_subsystem, permissions, nfs_server, attributes)
|
||||
result["changed"], result["msg"] = create_fs(
|
||||
module,
|
||||
fs_type,
|
||||
filesystem,
|
||||
vg,
|
||||
device,
|
||||
size,
|
||||
mount_group,
|
||||
auto_mount,
|
||||
account_subsystem,
|
||||
permissions,
|
||||
nfs_server,
|
||||
attributes,
|
||||
)
|
||||
|
||||
if device is not None and nfs_server is None:
|
||||
# Create a fs from a previously lv device.
|
||||
result['changed'], result['msg'] = create_fs(
|
||||
module, fs_type, filesystem, vg, device, size, mount_group, auto_mount, account_subsystem, permissions, nfs_server, attributes)
|
||||
result["changed"], result["msg"] = create_fs(
|
||||
module,
|
||||
fs_type,
|
||||
filesystem,
|
||||
vg,
|
||||
device,
|
||||
size,
|
||||
mount_group,
|
||||
auto_mount,
|
||||
account_subsystem,
|
||||
permissions,
|
||||
nfs_server,
|
||||
attributes,
|
||||
)
|
||||
|
||||
elif state == 'absent':
|
||||
elif state == "absent":
|
||||
if ismount(filesystem):
|
||||
result['msg'] = f"File system {filesystem} mounted."
|
||||
result["msg"] = f"File system {filesystem} mounted."
|
||||
|
||||
else:
|
||||
fs_status = _fs_exists(module, filesystem)
|
||||
if not fs_status:
|
||||
result['msg'] = f"File system {filesystem} does not exist."
|
||||
result["msg"] = f"File system {filesystem} does not exist."
|
||||
else:
|
||||
result['changed'], result['msg'] = remove_fs(module, filesystem, rm_mount_point)
|
||||
result["changed"], result["msg"] = remove_fs(module, filesystem, rm_mount_point)
|
||||
|
||||
elif state == 'mounted':
|
||||
elif state == "mounted":
|
||||
if ismount(filesystem):
|
||||
result['changed'] = False
|
||||
result['msg'] = f"File system {filesystem} already mounted."
|
||||
result["changed"] = False
|
||||
result["msg"] = f"File system {filesystem} already mounted."
|
||||
else:
|
||||
result['changed'], result['msg'] = mount_fs(module, filesystem)
|
||||
result["changed"], result["msg"] = mount_fs(module, filesystem)
|
||||
|
||||
elif state == 'unmounted':
|
||||
elif state == "unmounted":
|
||||
if not ismount(filesystem):
|
||||
result['changed'] = False
|
||||
result['msg'] = f"File system {filesystem} already unmounted."
|
||||
result["changed"] = False
|
||||
result["msg"] = f"File system {filesystem} already unmounted."
|
||||
else:
|
||||
result['changed'], result['msg'] = unmount_fs(module, filesystem)
|
||||
result["changed"], result["msg"] = unmount_fs(module, filesystem)
|
||||
|
||||
else:
|
||||
# Unreachable codeblock
|
||||
result['msg'] = f"Unexpected state {state}."
|
||||
result["msg"] = f"Unexpected state {state}."
|
||||
module.fail_json(**result)
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user