diff --git a/changelogs/fragments/2989-pamd-single-line.yaml b/changelogs/fragments/2989-pamd-single-line.yaml new file mode 100644 index 0000000000..359e160785 --- /dev/null +++ b/changelogs/fragments/2989-pamd-single-line.yaml @@ -0,0 +1,2 @@ +bugfixes: + - pamd - fixed problem with files containing only one or two lines (https://github.com/ansible-collections/community.general/issues/2925). diff --git a/plugins/modules/system/pamd.py b/plugins/modules/system/pamd.py index 39b3f32e44..738a23ee43 100644 --- a/plugins/modules/system/pamd.py +++ b/plugins/modules/system/pamd.py @@ -733,14 +733,19 @@ class PamdService(object): lines = [] current_line = self._head + mark = "# Updated by Ansible - %s" % datetime.now().isoformat() while current_line is not None: lines.append(str(current_line)) current_line = current_line.next - if lines[1].startswith("# Updated by Ansible"): - lines.pop(1) - - lines.insert(1, "# Updated by Ansible - " + datetime.now().isoformat()) + if len(lines) <= 1: + lines.insert(0, "") + lines.insert(1, mark) + else: + if lines[1].startswith("# Updated by Ansible"): + lines[1] = mark + else: + lines.insert(1, mark) return '\n'.join(lines) + '\n' diff --git a/tests/integration/targets/pamd/aliases b/tests/integration/targets/pamd/aliases new file mode 100644 index 0000000000..abe0a21e22 --- /dev/null +++ b/tests/integration/targets/pamd/aliases @@ -0,0 +1,5 @@ +shippable/posix/group1 +skip/aix +skip/freebsd +skip/osx +skip/macos diff --git a/tests/integration/targets/pamd/tasks/main.yml b/tests/integration/targets/pamd/tasks/main.yml new file mode 100644 index 0000000000..3e0fb4ee32 --- /dev/null +++ b/tests/integration/targets/pamd/tasks/main.yml @@ -0,0 +1,56 @@ +# (c) 2021, Alexei Znamensky +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +- name: Set value for temp limit configuration + set_fact: + test_pamd_file: "/tmp/pamd_file" + +- name: Copy temporary pam.d file + copy: + content: "session required pam_lastlog.so silent showfailed" + dest: "{{ test_pamd_file }}" + +- name: Test working on a single-line file works (2925) + community.general.pamd: + path: /tmp + name: pamd_file + type: session + control: required + module_path: pam_lastlog.so + module_arguments: silent + state: args_absent + register: pamd_file_output + +- name: Check if changes made + assert: + that: + - pamd_file_output is changed + +- name: Copy temporary pam.d file + copy: + content: "" + dest: "{{ test_pamd_file }}" + +# This test merely demonstrates that, as-is, module will not perform any changes on an empty file +# All the existing values for "state" will first search for a rule matching type, control, module_path +# and will not perform any change whatsoever if no existing rules match. +- name: Test working on a empty file works (2925) + community.general.pamd: + path: /tmp + name: pamd_file + type: session + control: required + module_path: pam_lastlog.so + module_arguments: silent + register: pamd_file_output_empty + +- name: Read back the file + slurp: + src: "{{ test_pamd_file }}" + register: pamd_file_slurp + +- name: Check if changes made + assert: + that: + - pamd_file_output_empty is not changed + - pamd_file_slurp.content|b64decode == ''