mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 20:01:25 -07:00
Add check for correct parsing of sysctl (#24540)
* PEP8 fixes * Refactoring of code * Check to skip non-comment lines which doesn't contain = character Fixes #24453 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
2f732a621d
commit
be86d77a70
2 changed files with 24 additions and 25 deletions
|
@ -111,6 +111,7 @@ import tempfile
|
||||||
from ansible.module_utils.basic import get_platform, AnsibleModule
|
from ansible.module_utils.basic import get_platform, AnsibleModule
|
||||||
from ansible.module_utils.six import string_types
|
from ansible.module_utils.six import string_types
|
||||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE, BOOLEANS_TRUE
|
from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE, BOOLEANS_TRUE
|
||||||
|
from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
|
|
||||||
class SysctlModule(object):
|
class SysctlModule(object):
|
||||||
|
@ -297,18 +298,17 @@ class SysctlModule(object):
|
||||||
lines = []
|
lines = []
|
||||||
if os.path.isfile(self.sysctl_file):
|
if os.path.isfile(self.sysctl_file):
|
||||||
try:
|
try:
|
||||||
f = open(self.sysctl_file, "r")
|
with open(self.sysctl_file, "r") as read_file:
|
||||||
lines = f.readlines()
|
lines = read_file.readlines()
|
||||||
f.close()
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
self.module.fail_json(msg="Failed to open %s: %s" % (self.sysctl_file, str(e)))
|
self.module.fail_json(msg="Failed to open %s: %s" % (self.sysctl_file, to_native(e)))
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
self.file_lines.append(line)
|
self.file_lines.append(line)
|
||||||
|
|
||||||
# don't split empty lines or comments
|
# don't split empty lines or comments or line without equal sign
|
||||||
if not line or line.startswith(("#", ";")):
|
if not line or line.startswith(("#", ";")) or "=" not in line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
k, v = line.split('=', 1)
|
k, v = line.split('=', 1)
|
||||||
|
@ -321,7 +321,7 @@ class SysctlModule(object):
|
||||||
checked = []
|
checked = []
|
||||||
self.fixed_lines = []
|
self.fixed_lines = []
|
||||||
for line in self.file_lines:
|
for line in self.file_lines:
|
||||||
if not line.strip() or line.strip().startswith(("#", ";")):
|
if not line.strip() or line.strip().startswith(("#", ";")) or "=" not in line:
|
||||||
self.fixed_lines.append(line)
|
self.fixed_lines.append(line)
|
||||||
continue
|
continue
|
||||||
tmpline = line.strip()
|
tmpline = line.strip()
|
||||||
|
@ -351,7 +351,7 @@ class SysctlModule(object):
|
||||||
for l in self.fixed_lines:
|
for l in self.fixed_lines:
|
||||||
f.write(l.strip() + "\n")
|
f.write(l.strip() + "\n")
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e)))
|
||||||
f.flush()
|
f.flush()
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
|
@ -460,7 +460,6 @@ lib/ansible/modules/system/seport.py
|
||||||
lib/ansible/modules/system/service.py
|
lib/ansible/modules/system/service.py
|
||||||
lib/ansible/modules/system/solaris_zone.py
|
lib/ansible/modules/system/solaris_zone.py
|
||||||
lib/ansible/modules/system/svc.py
|
lib/ansible/modules/system/svc.py
|
||||||
lib/ansible/modules/system/sysctl.py
|
|
||||||
lib/ansible/modules/system/systemd.py
|
lib/ansible/modules/system/systemd.py
|
||||||
lib/ansible/modules/system/timezone.py
|
lib/ansible/modules/system/timezone.py
|
||||||
lib/ansible/modules/system/ufw.py
|
lib/ansible/modules/system/ufw.py
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue