From 806ff5c1a37813d008a29c7de88746e2082e4e8a Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 19 May 2024 13:54:43 +0200 Subject: [PATCH 1/6] 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 7914e9d..de75291 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/6] 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 de75291..8054319 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -101,6 +101,7 @@ import os 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 @@ class SysctlModule(object): # 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/6] 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 8054319..723d315 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -386,15 +386,27 @@ class SysctlModule(object): # 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/6] 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 723d315..c016d09 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -386,27 +386,15 @@ class SysctlModule(object): # 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)) From 6280bb8ec8fec0e2868232783482ff5b6a77494e Mon Sep 17 00:00:00 2001 From: MubashirUsman Date: Sun, 3 Nov 2024 23:20:40 +0100 Subject: [PATCH 5/6] Add integration test for --system option --- .../integration/targets/sysctl/tasks/main.yml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index ef5b86f..d5f6899 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -229,6 +229,40 @@ ansible.builtin.assert: that: - sysctl_test4 is failed + + ## + ## sysctl --system + ## + + - name: Set vm.swappiness to 10 with --system option + ansible.posix.sysctl: + name: vm.swappiness + value: 10 + state: present + reload: false + sysctl_set: true + system: true + register: sysctl_system_test1 + + - name: Check with sysctl command + ansible.builtin.command: sysctl vm.swappiness + changed_when: false + register: sysctl_check_system1 + + - name: Debug sysctl_system_test1 sysctl_check_system1 + ansible.builtin.debug: + var: item + verbosity: 1 + with_items: + - "{{ sysctl_system_test1 }}" + - "{{ sysctl_check_system1 }}" + + - name: Validate results for --system option + ansible.builtin.assert: + that: + - sysctl_system_test1 is changed + - sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"] + - name: Test on RHEL VMs when: @@ -366,3 +400,33 @@ that: - stat_result.stat.islnk is defined and stat_result.stat.islnk - stat_result.stat.lnk_source == '/tmp/ansible_sysctl_test.conf' + + # Test sysctl: --system + - name: Set vm.swappiness to 10 with --system option + ansible.posix.sysctl: + name: vm.swappiness + value: 10 + state: present + reload: false + sysctl_set: true + system: true + register: sysctl_system_test1 + + - name: Check with sysctl command + ansible.builtin.command: sysctl vm.swappiness + changed_when: false + register: sysctl_check_system1 + + - name: Debug sysctl_system_test1 sysctl_check_system1 + ansible.builtin.debug: + var: item + verbosity: 1 + with_items: + - "{{ sysctl_system_test1 }}" + - "{{ sysctl_check_system1 }}" + + - name: Validate results for --system option + ansible.builtin.assert: + that: + - sysctl_system_test1 is changed + - sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"] From 2f5210f362ecd7db9765248e8ec5b4ada57390ea Mon Sep 17 00:00:00 2001 From: "mubashir.Ijaz" Date: Thu, 7 Aug 2025 17:47:24 +0200 Subject: [PATCH 6/6] Add integration system_wide tests --- plugins/modules/sysctl.py | 22 ++- .../integration/targets/sysctl/tasks/main.yml | 58 +++++- .../sysctl/tasks/system_wide_tests.yml | 170 ++++++++++++++++++ 3 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 tests/integration/targets/sysctl/tasks/system_wide_tests.yml diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index 8b53620..dbca675 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -56,6 +56,16 @@ options: - Verify token value with the sysctl command and set with C(-w) if necessary. type: bool default: false + system_wide: + description: + - If V(true), uses C(sysctl --system) behavior to reload all sysctl configuration files. + - This will reload configuration from C(/etc/sysctl.d/*.conf), C(/run/sysctl.d/*.conf), + C(/usr/local/lib/sysctl.d/*.conf), C(/usr/lib/sysctl.d/*.conf), C(/lib/sysctl.d/*.conf), + and C(/etc/sysctl.conf) in that order. + - If V(false), only reloads the specific sysctl file defined by O(sysctl_file). + - Only applies when O(reload) is V(true). + type: bool + default: false author: - David CHANIAL (@davixx) ''' @@ -100,6 +110,14 @@ EXAMPLES = r''' sysctl_set: true state: present reload: true + +# Set vm.swappiness and reload all system sysctl configuration files (equivalent to sysctl --system) +- ansible.posix.sysctl: + name: vm.swappiness + value: '10' + state: present + reload: true + system_wide: true ''' # ============================================================== @@ -139,7 +157,7 @@ class SysctlModule(object): 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.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 @@ -319,7 +337,7 @@ class SysctlModule(object): # https://github.com/ansible/ansible/issues/58158 return else: - if self.system_Wide: + 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) diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index d5f6899..4355071 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -241,7 +241,7 @@ state: present reload: false sysctl_set: true - system: true + system_wide: true register: sysctl_system_test1 - name: Check with sysctl command @@ -263,6 +263,57 @@ - sysctl_system_test1 is changed - sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"] + # Test system_wide with reload=true + - name: Set vm.dirty_ratio to 20 with system_wide and reload=true + ansible.posix.sysctl: + name: vm.dirty_ratio + value: 20 + state: present + reload: true + system_wide: true + register: sysctl_system_reload_test + + - name: Check vm.dirty_ratio value + ansible.builtin.command: sysctl -n vm.dirty_ratio + changed_when: false + register: sysctl_check_dirty_ratio + + - name: Validate system_wide with reload + ansible.builtin.assert: + that: + - sysctl_system_reload_test is changed + - sysctl_check_dirty_ratio.stdout == "20" + + # Test system_wide=false behavior (default) + - name: Create custom sysctl file for testing + ansible.builtin.copy: + content: | + # Custom sysctl test file + vm.dirty_background_ratio=5 + dest: "{{ output_dir_test }}/custom_sysctl.conf" + mode: "0644" + + - name: Set vm.dirty_background_ratio with system_wide=false + ansible.posix.sysctl: + name: vm.dirty_background_ratio + value: 10 + state: present + reload: true + system_wide: false + sysctl_file: "{{ output_dir_test }}/custom_sysctl.conf" + register: sysctl_system_false_test + + - name: Check custom sysctl file content + ansible.builtin.command: cat {{ output_dir_test }}/custom_sysctl.conf + changed_when: false + register: custom_sysctl_content + + - name: Validate system_wide=false behavior + ansible.builtin.assert: + that: + - sysctl_system_false_test is changed + - "'vm.dirty_background_ratio=10' in custom_sysctl_content.stdout" + - name: Test on RHEL VMs when: @@ -409,7 +460,7 @@ state: present reload: false sysctl_set: true - system: true + system_wide: true register: sysctl_system_test1 - name: Check with sysctl command @@ -430,3 +481,6 @@ that: - sysctl_system_test1 is changed - sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"] + +- name: Include system_wide specific tests + ansible.builtin.include_tasks: system_wide_tests.yml diff --git a/tests/integration/targets/sysctl/tasks/system_wide_tests.yml b/tests/integration/targets/sysctl/tasks/system_wide_tests.yml new file mode 100644 index 0000000..fed7395 --- /dev/null +++ b/tests/integration/targets/sysctl/tasks/system_wide_tests.yml @@ -0,0 +1,170 @@ +--- +# Additional tests specifically for system_wide parameter functionality + +- name: Test system_wide parameter comprehensive functionality + block: + # Test system_wide with reload=true + - name: Set vm.dirty_expire_centisecs with system_wide=true + ansible.posix.sysctl: + name: vm.dirty_expire_centisecs + value: 3000 + state: present + reload: true + system_wide: true + register: sysctl_system_wide_reload_test + + - name: Check vm.dirty_expire_centisecs value + ansible.builtin.command: sysctl -n vm.dirty_expire_centisecs + changed_when: false + register: sysctl_check_dirty_expire + + - name: Validate system_wide with reload=true + ansible.builtin.assert: + that: + - sysctl_system_wide_reload_test is changed + - sysctl_check_dirty_expire.stdout == "3000" + + # Test system_wide=false behavior (default) + - name: Create custom sysctl file for testing system_wide=false + ansible.builtin.copy: + content: | + # Custom sysctl test file + vm.dirty_background_ratio=5 + dest: "{{ output_dir_test }}/custom_sysctl.conf" + mode: "0644" + + - name: Set vm.dirty_background_ratio with system_wide=false + ansible.posix.sysctl: + name: vm.dirty_background_ratio + value: 10 + state: present + reload: true + system_wide: false + sysctl_file: "{{ output_dir_test }}/custom_sysctl.conf" + register: sysctl_system_false_test + + - name: Check custom sysctl file content + ansible.builtin.command: cat {{ output_dir_test }}/custom_sysctl.conf + changed_when: false + register: custom_sysctl_content + + - name: Validate system_wide=false behavior + ansible.builtin.assert: + that: + - sysctl_system_false_test is changed + - "'vm.dirty_background_ratio=10' in custom_sysctl_content.stdout" + + # Test system_wide with check mode + - name: Test system_wide in check mode + ansible.posix.sysctl: + name: vm.swappiness + value: 25 + state: present + reload: true + system_wide: true + check_mode: true + register: sysctl_system_wide_check_mode + + - name: Validate check mode works with system_wide + ansible.builtin.assert: + that: + - sysctl_system_wide_check_mode is changed + + # Test system_wide with missing directories (should not fail) + - name: Test system_wide with potentially missing directories + ansible.posix.sysctl: + name: vm.overcommit_memory + value: 1 + state: present + reload: true + system_wide: true + ignoreerrors: true + register: sysctl_system_wide_missing_dirs + + - name: Validate system_wide handles missing directories + ansible.builtin.assert: + that: + - sysctl_system_wide_missing_dirs is not failed + +- name: Test system_wide with multiple configuration files (RHEL/CentOS only) + when: + - ansible_facts.os_family == 'RedHat' + - ansible_facts.virtualization_type != 'docker' + block: + # Test that system_wide processes multiple configuration files + - name: Create test sysctl.d file + ansible.builtin.copy: + content: | + # Test system-wide sysctl reload + vm.dirty_writeback_centisecs=500 + dest: /etc/sysctl.d/99-ansible-test.conf + mode: "0644" + backup: true + register: test_sysctl_file + + - name: Apply setting with system_wide to test multiple file processing + ansible.posix.sysctl: + name: vm.overcommit_memory + value: 1 + state: present + reload: true + system_wide: true + register: sysctl_multifile_test + + - name: Verify both settings are applied + ansible.builtin.shell: | + sysctl -n vm.dirty_writeback_centisecs + sysctl -n vm.overcommit_memory + changed_when: false + register: sysctl_multifile_check + + - name: Validate multiple file processing + ansible.builtin.assert: + that: + - sysctl_multifile_test is changed + - "'500' in sysctl_multifile_check.stdout" + - "'1' in sysctl_multifile_check.stdout" + + - name: Cleanup test sysctl.d file + ansible.builtin.file: + path: /etc/sysctl.d/99-ansible-test.conf + state: absent + +- name: Test system_wide parameter combinations + block: + # Test system_wide with sysctl_set + - name: Test system_wide with sysctl_set=true + ansible.posix.sysctl: + name: vm.swappiness + value: 15 + state: present + reload: true + system_wide: true + sysctl_set: true + register: sysctl_system_wide_set_test + + - name: Check vm.swappiness value after system_wide + sysctl_set + ansible.builtin.command: sysctl -n vm.swappiness + changed_when: false + register: sysctl_check_swappiness_set + + - name: Validate system_wide with sysctl_set + ansible.builtin.assert: + that: + - sysctl_system_wide_set_test is changed + - sysctl_check_swappiness_set.stdout == "15" + + # Test system_wide with reload=false (should not trigger system reload) + - name: Test system_wide with reload=false + ansible.posix.sysctl: + name: vm.dirty_ratio + value: 25 + state: present + reload: false + system_wide: true + register: sysctl_system_wide_no_reload + + - name: Validate system_wide with reload=false + ansible.builtin.assert: + that: + - sysctl_system_wide_no_reload is changed