Merge pull request #206 from satken2/sakten2_issue111

Fixed sysctl to work on symlinks

SUMMARY
Fixes #111.
This issue reports a bug of sysctl that the module does not work properly when sysctl_file is a symlink.
I Fixed the bug by inserting os.path.realpath to get real path.
When sysctl_file is a real file, os.path.realpath return the original path as is.
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
sysctl
ADDITIONAL INFORMATION


I have executed the script described in #111 and confirmed that it works properly.
But I need to add some tests.

satken@dockerhost1:~/ansible$ sudo docker run --rm -v ${PWD}:/work -w /work -e ANSIBLE_LIBRARY=/work/ansible.posix -e ANSIBLE_HOST_KEY_CHECKING=False satken2/ansible:3.3.0 ansible-playbook -i hosts main.yml

PLAY [test] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.91.76]

TASK [test] ********************************************************************
ok: [192.168.91.76] => {
    "msg": "This is test"
}

TASK [command] *****************************************************************
changed: [192.168.91.76]

TASK [command] *****************************************************************
ok: [192.168.91.76]

TASK [debug] *******************************************************************
ok: [192.168.91.76] => {
    "sysctl_current_value.stdout": "kernel.randomize_va_space = 2"
}

TASK [copy] ********************************************************************
changed: [192.168.91.76]

TASK [file] ********************************************************************
changed: [192.168.91.76]

TASK [stat] ********************************************************************
ok: [192.168.91.76]

TASK [assert] ******************************************************************
ok: [192.168.91.76] => {
    "changed": false,
    "msg": "/tmp/ansible_sysctl_test_symlink.conf is correct symlink"
}

TASK [sysctl | enable randomized layout of virtual address space] **************
changed: [192.168.91.76]

TASK [stat] ********************************************************************
ok: [192.168.91.76]

TASK [assert] ******************************************************************
ok: [192.168.91.76] => {
    "changed": false,
    "msg": "/tmp/ansible_sysctl_test_symlink.conf is correct symlink"
}

PLAY RECAP *********************************************************************
192.168.91.76              : ok=12   changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Reviewed-by: quidame
Reviewed-by: Jill R
This commit is contained in:
softwarefactory-project-zuul[bot] 2024-05-09 19:41:13 +00:00 committed by GitHub
commit 34f140c22f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 2 deletions

View File

@ -0,0 +1,3 @@
---
bugfixes:
- sysctl - fix sysctl to work properly on symlinks (https://github.com/ansible-collections/ansible.posix/issues/111).

View File

@ -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))
# ==============================================================

View File

@ -332,3 +332,37 @@
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
ansible.builtin.copy:
content: |
# Testing Ansible Sysctl module on symlink.
dest: /tmp/ansible_sysctl_test.conf
mode: "0644"
- name: Create symlink to the conf file
ansible.builtin.file:
src: /tmp/ansible_sysctl_test.conf
dest: /tmp/ansible_sysctl_test_symlink.conf
state: link
- name: Use sysctl module with symlink sysctl file
ansible.posix.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
ansible.builtin.stat:
path: /tmp/ansible_sysctl_test_symlink.conf
register: stat_result
- name: Ensure the sysctl file remains a symlink
ansible.builtin.assert:
that:
- stat_result.stat.islnk is defined and stat_result.stat.islnk
- stat_result.stat.lnk_source == '/tmp/ansible_sysctl_test.conf'