write sysctl reverted

This commit is contained in:
MubashirUsman 2024-05-19 17:47:12 +02:00
parent 505a4aaa09
commit 7e1b76c46e

View file

@ -386,27 +386,15 @@ class SysctlModule(object):
# Completely rewrite the sysctl file # Completely rewrite the sysctl file
def write_sysctl(self): def write_sysctl(self):
# open a tmp file # open a tmp file
if self.system_Wide: fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(os.path.realpath(self.sysctl_file)))
sysctl_files_dir = '/etc/sysctl.d/' f = open(tmp_path, "w")
fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', sysctl_files_dir)
os.close(fd=fd)
else:
fd, tmp_path = tempfile.mkstemp(dir=os.path.dirname(self.sysctl_file))
os.close(fd)
try: try:
with open(tmp_path, 'w') as write_file: for l in self.fixed_lines:
for line in self.fixed_lines: f.write(l.strip() + "\n")
write_file.write("%s\n" % line)
os.rename(tmp_path, self.sysctl_file)
except IOError as e: except IOError as e:
self.module.fail_json(msg="Failed to write %s: %s" % (to_native(tmp_path), to_native(e))) self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e)))
finally: f.flush()
try: f.close()
os.remove(tmp_path)
except OSError:
pass
# replace the real one # replace the real one
self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file)) self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file))