From 806ff5c1a37813d008a29c7de88746e2082e4e8a Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 19 May 2024 13:54:43 +0200 Subject: [PATCH 1/4] added sysctl_dirs variable and system_wide var --- plugins/modules/sysctl.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index 7914e9d168..de75291a08 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -114,12 +114,24 @@ class SysctlModule(object): # success or failure. LANG_ENV = {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'} + # We define a variable to keep all the directories to be read, equivalent to + # (/sbin/sysctl --system) option + SYSCTL_DIRS = [ + '/etc/sysctl.d/*.conf', + '/run/sysctl.d/*.conf', + '/usr/local/lib/sysctl.d/*.conf', + '/usr/lib/sysctl.d/*.conf', + '/lib/sysctl.d/*.conf', + '/etc/sysctl.conf' + ] + def __init__(self, module): self.module = module self.args = self.module.params self.sysctl_cmd = self.module.get_bin_path('sysctl', required=True) self.sysctl_file = self.args['sysctl_file'] + self.system_Wide = self.args['system_Wide'] self.proc_value = None # current token value in proc fs self.file_value = None # current token value in file From d70d2aaaa7349c2bc312ab86a0e358800f568caa Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 19 May 2024 16:29:36 +0200 Subject: [PATCH 2/4] read sysctl_dir files --- plugins/modules/sysctl.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index de75291a08..8054319d09 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -101,6 +101,7 @@ import platform import re import tempfile +import glob from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import string_types @@ -311,15 +312,22 @@ def reload_sysctl(self): # https://github.com/ansible/ansible/issues/58158 return else: - # system supports reloading via the -p flag to sysctl, so we'll use that - sysctl_args = [self.sysctl_cmd, '-p', self.sysctl_file] - if self.args['ignoreerrors']: - sysctl_args.insert(1, '-e') + if self.system_Wide: + for sysctl_file in self.SYSCTL_DIRS: + for conf_file in glob.glob(sysctl_file): + rc, out, err = self.module.run_command([self.sysctl_cmd, '-p', conf_file], environ_update=self.LANG_ENV) + if rc != 0 or self._stderr_failed(err): + self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err)) + else: + # system supports reloading via the -p flag to sysctl, so we'll use that + sysctl_args = [self.sysctl_cmd, '-p', self.sysctl_file] + if self.args['ignoreerrors']: + sysctl_args.insert(1, '-e') - rc, out, err = self.module.run_command(sysctl_args, environ_update=self.LANG_ENV) + rc, out, err = self.module.run_command(sysctl_args, environ_update=self.LANG_ENV) - if rc != 0 or self._stderr_failed(err): - self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err)) + if rc != 0 or self._stderr_failed(err): + self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err)) # ============================================================== # SYSCTL FILE MANAGEMENT From 505a4aaa09fe33e05706aa5947c6be2f7168172b Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 19 May 2024 17:29:02 +0200 Subject: [PATCH 3/4] system_wide in defining module --- plugins/modules/sysctl.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index 8054319d09..723d315ce8 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -386,15 +386,27 @@ def fix_lines(self): # Completely rewrite the sysctl file def write_sysctl(self): # open a tmp file - fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(os.path.realpath(self.sysctl_file))) - f = open(tmp_path, "w") + if self.system_Wide: + sysctl_files_dir = '/etc/sysctl.d/' + 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: - for l in self.fixed_lines: - f.write(l.strip() + "\n") + with open(tmp_path, 'w') as write_file: + for line in self.fixed_lines: + write_file.write("%s\n" % line) + os.rename(tmp_path, self.sysctl_file) except IOError as e: - self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e))) - f.flush() - f.close() + self.module.fail_json(msg="Failed to write %s: %s" % (to_native(tmp_path), to_native(e))) + finally: + try: + os.remove(tmp_path) + except OSError: + pass + # replace the real one self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file)) @@ -414,7 +426,8 @@ def main(): reload=dict(default=True, type='bool'), sysctl_set=dict(default=False, type='bool'), ignoreerrors=dict(default=False, type='bool'), - sysctl_file=dict(default='/etc/sysctl.conf', type='path') + sysctl_file=dict(default='/etc/sysctl.conf', type='path'), + system_wide=dict(default=False, type='bool'), # system_wide parameter ), supports_check_mode=True, required_if=[('state', 'present', ['value'])], From 7e1b76c46e073a82798f6504d98b6d05fc4d7ba6 Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 19 May 2024 17:47:12 +0200 Subject: [PATCH 4/4] write sysctl reverted --- plugins/modules/sysctl.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index 723d315ce8..c016d09228 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -386,27 +386,15 @@ def fix_lines(self): # Completely rewrite the sysctl file def write_sysctl(self): # open a tmp file - if self.system_Wide: - sysctl_files_dir = '/etc/sysctl.d/' - 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) - + fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(os.path.realpath(self.sysctl_file))) + f = open(tmp_path, "w") try: - with open(tmp_path, 'w') as write_file: - for line in self.fixed_lines: - write_file.write("%s\n" % line) - os.rename(tmp_path, self.sysctl_file) + for l in self.fixed_lines: + f.write(l.strip() + "\n") except IOError as e: - self.module.fail_json(msg="Failed to write %s: %s" % (to_native(tmp_path), to_native(e))) - finally: - try: - os.remove(tmp_path) - except OSError: - pass - + self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e))) + f.flush() + f.close() # replace the real one self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file))