From 42c4c192d4a42d161f3f0313439a74555dded4f6 Mon Sep 17 00:00:00 2001 From: satken2 Date: Thu, 10 Jun 2021 23:42:18 +0900 Subject: [PATCH 1/5] sysctl: Fixed sysctl to work on symlinks(#111) --- plugins/modules/sysctl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/sysctl.py b/plugins/modules/sysctl.py index b82b2e4..c5d0ce5 100644 --- a/plugins/modules/sysctl.py +++ b/plugins/modules/sysctl.py @@ -366,7 +366,7 @@ 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(self.sysctl_file)) + fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(os.path.realpath(self.sysctl_file))) f = open(tmp_path, "w") try: for l in self.fixed_lines: @@ -377,7 +377,7 @@ class SysctlModule(object): f.close() # replace the real one - self.module.atomic_move(tmp_path, self.sysctl_file) + self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file)) # ============================================================== From 3d7334dcfc6e5ff1fab86a8816cae3244a44cc75 Mon Sep 17 00:00:00 2001 From: satken2 Date: Fri, 11 Jun 2021 23:57:09 +0900 Subject: [PATCH 2/5] sysctl: Fixed sysctl to work on symlinks(#111) --- .../206_fix_sysctl_to_work_on_symlinks.yml | 3 ++ .../integration/targets/sysctl/tasks/main.yml | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 changelogs/fragments/206_fix_sysctl_to_work_on_symlinks.yml diff --git a/changelogs/fragments/206_fix_sysctl_to_work_on_symlinks.yml b/changelogs/fragments/206_fix_sysctl_to_work_on_symlinks.yml new file mode 100644 index 0000000..703a6a2 --- /dev/null +++ b/changelogs/fragments/206_fix_sysctl_to_work_on_symlinks.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- sysctl - fix sysctl to work properly on symlinks (https://github.com/ansible-collections/ansible.posix/issues/111). diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index 6372128..f378e26 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -310,3 +310,36 @@ that: - sysctl_invalid_set1 is failed - "'vm.mmap_rnd_bits' not in sysctl_invalid_conf_content.stdout" + + # Test sysctl: sysctl_file is symlink + - name: Create link source + copy: + content: | + # Testing Ansible Sysctl module on symlink. + dest: /tmp/ansible_sysctl_test.conf + + - name: Create symlink to the conf file + file: + src: /tmp/ansible_sysctl_test.conf + dest: /tmp/ansible_sysctl_test_symlink.conf + state: link + + - name: Use sysctl module with symlink sysctl file + sysctl: + name: 'kernel.randomize_va_space' + value: '1' + sysctl_file: /tmp/ansible_sysctl_test_symlink.conf + state: present + sysctl_set: false + reload: false + + - Stat sysctl file + stat: + path: /tmp/ansible_sysctl_test_symlink.conf + register: stat_result + + - Ensure the sysctl file remains a symlink + assert: + that: + - stat_result.stat.islnk is defined and stat_result.stat.islnk + - stat_result.stat.lnk_source == /tmp/ansible_sysctl_test.conf From c9747fd2434edeb43bb35690ab944374fd946ae2 Mon Sep 17 00:00:00 2001 From: satken2 Date: Sat, 12 Jun 2021 00:33:20 +0900 Subject: [PATCH 3/5] sysctl: Fixed sysctl to work on symlinks(#111) --- tests/integration/targets/sysctl/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index f378e26..e5944a1 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -333,7 +333,7 @@ sysctl_set: false reload: false - - Stat sysctl file + - name: Stat sysctl file stat: path: /tmp/ansible_sysctl_test_symlink.conf register: stat_result From a488709313c7a3cb24bc952a07e653bcf2d58bfc Mon Sep 17 00:00:00 2001 From: satken2 Date: Sat, 12 Jun 2021 00:33:39 +0900 Subject: [PATCH 4/5] sysctl: Fixed sysctl to work on symlinks(#111) --- tests/integration/targets/sysctl/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index e5944a1..5f4335b 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -338,7 +338,7 @@ path: /tmp/ansible_sysctl_test_symlink.conf register: stat_result - - Ensure the sysctl file remains a symlink + - name: Ensure the sysctl file remains a symlink assert: that: - stat_result.stat.islnk is defined and stat_result.stat.islnk From 34b467719e0dab10d062813cdef11b49f3925ed6 Mon Sep 17 00:00:00 2001 From: satken2 Date: Sat, 12 Jun 2021 01:03:28 +0900 Subject: [PATCH 5/5] sysctl: Fixed sysctl to work on symlinks(#111) --- .../integration/targets/sysctl/tasks/main.yml | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/tests/integration/targets/sysctl/tasks/main.yml b/tests/integration/targets/sysctl/tasks/main.yml index 5f4335b..6372128 100644 --- a/tests/integration/targets/sysctl/tasks/main.yml +++ b/tests/integration/targets/sysctl/tasks/main.yml @@ -310,36 +310,3 @@ that: - sysctl_invalid_set1 is failed - "'vm.mmap_rnd_bits' not in sysctl_invalid_conf_content.stdout" - - # Test sysctl: sysctl_file is symlink - - name: Create link source - copy: - content: | - # Testing Ansible Sysctl module on symlink. - dest: /tmp/ansible_sysctl_test.conf - - - name: Create symlink to the conf file - file: - src: /tmp/ansible_sysctl_test.conf - dest: /tmp/ansible_sysctl_test_symlink.conf - state: link - - - name: Use sysctl module with symlink sysctl file - sysctl: - name: 'kernel.randomize_va_space' - value: '1' - sysctl_file: /tmp/ansible_sysctl_test_symlink.conf - state: present - sysctl_set: false - reload: false - - - name: Stat sysctl file - stat: - path: /tmp/ansible_sysctl_test_symlink.conf - register: stat_result - - - name: Ensure the sysctl file remains a symlink - assert: - that: - - stat_result.stat.islnk is defined and stat_result.stat.islnk - - stat_result.stat.lnk_source == /tmp/ansible_sysctl_test.conf