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>
pull/25737/merge
Abhijeet Kasurde 7 years ago committed by ansibot
parent 2f732a621d
commit be86d77a70

@ -111,6 +111,7 @@ import tempfile
from ansible.module_utils.basic import get_platform, AnsibleModule
from ansible.module_utils.six import string_types
from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE, BOOLEANS_TRUE
from ansible.module_utils._text import to_native
class SysctlModule(object):
@ -297,18 +298,17 @@ class SysctlModule(object):
lines = []
if os.path.isfile(self.sysctl_file):
try:
f = open(self.sysctl_file, "r")
lines = f.readlines()
f.close()
with open(self.sysctl_file, "r") as read_file:
lines = read_file.readlines()
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:
line = line.strip()
self.file_lines.append(line)
# don't split empty lines or comments
if not line or line.startswith(("#", ";")):
# don't split empty lines or comments or line without equal sign
if not line or line.startswith(("#", ";")) or "=" not in line:
continue
k, v = line.split('=', 1)
@ -321,7 +321,7 @@ class SysctlModule(object):
checked = []
self.fixed_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)
continue
tmpline = line.strip()
@ -351,7 +351,7 @@ class SysctlModule(object):
for l in self.fixed_lines:
f.write(l.strip() + "\n")
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.close()

@ -460,7 +460,6 @@ lib/ansible/modules/system/seport.py
lib/ansible/modules/system/service.py
lib/ansible/modules/system/solaris_zone.py
lib/ansible/modules/system/svc.py
lib/ansible/modules/system/sysctl.py
lib/ansible/modules/system/systemd.py
lib/ansible/modules/system/timezone.py
lib/ansible/modules/system/ufw.py

Loading…
Cancel
Save