mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-06 22:24:23 -07:00
change nfs_exports_info
This commit is contained in:
parent
309fe6d4d0
commit
05a326c5c7
1 changed files with 33 additions and 24 deletions
|
@ -59,44 +59,53 @@ def get_exports(module, output_format, file_path="/etc/exports"):
|
||||||
try:
|
try:
|
||||||
exports_file_digest = module.digest_from_file(file_path, 'sha1')
|
exports_file_digest = module.digest_from_file(file_path, 'sha1')
|
||||||
if exports_file_digest is None:
|
if exports_file_digest is None:
|
||||||
module.fail_json(msg=f"{file_path} file not found")
|
module.fail_json(msg="{} file not found".format(file_path))
|
||||||
|
|
||||||
with open(file_path, 'r') as f:
|
try:
|
||||||
|
f = open(file_path, 'r')
|
||||||
output_lines = f.readlines()
|
output_lines = f.readlines()
|
||||||
|
f.close()
|
||||||
|
except IOError:
|
||||||
|
module.fail_json(msg="Could not read {}".format(file_path))
|
||||||
|
|
||||||
exports = {}
|
exports = {}
|
||||||
pattern = r'\s*(\S+)\s+(.+)'
|
pattern = r'\s*(\S+)\s+(.+)'
|
||||||
|
|
||||||
for line in output_lines:
|
for line in output_lines:
|
||||||
if line.strip() and not line.strip().startswith('#'):
|
line = line.strip()
|
||||||
match = re.match(pattern, line)
|
if not line or line.startswith('#'):
|
||||||
if not match:
|
continue
|
||||||
continue
|
|
||||||
|
|
||||||
folder = match.group(1)
|
match = re.match(pattern, line)
|
||||||
rest = match.group(2)
|
if not match:
|
||||||
|
continue
|
||||||
|
|
||||||
entries = re.findall(r'(\d+\.\d+\.\d+\.\d+)\(([^)]+)\)', rest)
|
folder = match.group(1)
|
||||||
for ip, options_str in entries:
|
rest = match.group(2)
|
||||||
options = options_str.split(',')
|
|
||||||
|
|
||||||
if output_format == "ips_per_share":
|
entries = re.findall(r'(\d+\.\d+\.\d+\.\d+)\(([^)]+)\)', rest)
|
||||||
entry = {"ip": ip, "options": options}
|
for ip, options_str in entries:
|
||||||
exports.setdefault(folder, []).append(entry)
|
options = options_str.split(',')
|
||||||
|
|
||||||
elif output_format == "shares_per_ip":
|
if output_format == "ips_per_share":
|
||||||
entry = {"folder": folder, "options": options}
|
entry = {"ip": ip, "options": options}
|
||||||
exports.setdefault(ip, []).append(entry)
|
if folder not in exports:
|
||||||
|
exports[folder] = []
|
||||||
|
exports[folder].append(entry)
|
||||||
|
|
||||||
|
elif output_format == "shares_per_ip":
|
||||||
|
entry = {"folder": folder, "options": options}
|
||||||
|
if ip not in exports:
|
||||||
|
exports[ip] = []
|
||||||
|
exports[ip].append(entry)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'exports_info': exports,
|
'exports_info': exports,
|
||||||
'file_digest': exports_file_digest
|
'file_digest': exports_file_digest
|
||||||
}
|
}
|
||||||
|
|
||||||
except FileNotFoundError:
|
|
||||||
module.fail_json(msg=f"{file_path} file not found")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg="Error while processing exports: {}".format(str(e)))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -108,16 +117,16 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
output_format = module.params['output_format']
|
output_format = module.params['output_format']
|
||||||
exports_info = get_exports(module, output_format)
|
result = get_exports(module, output_format)
|
||||||
|
|
||||||
module.exit_json(
|
module.exit_json(
|
||||||
changed=False,
|
changed=False,
|
||||||
exports_info=exports_info['exports_info'],
|
exports_info=result['exports_info'],
|
||||||
file_digest=exports_info['file_digest']
|
file_digest=result['file_digest']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
__all__ = ['get_exports']
|
__all__ = ['get_exports']
|
Loading…
Add table
Add a link
Reference in a new issue