From 6940ec84652909ad3ea33ed6fa35efa2b710f3da Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 19 Nov 2026 15:26:01 +0100 Subject: [PATCH] test: verify unmanaged sudoers files survive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guard against regressions that remove the login user’s sudo access. Verify that only a drop-in marked state: absent is removed. Co-authored-by: Copilot --- defaults/main.yml | 12 ++++++------ molecule/default/prepare.yml | 10 ++++++---- molecule/default/verify.yml | 10 ++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 6cc89fa..c8f84a7 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,9 +3,9 @@ sudo_users_sudoers: [] # - commands: # - ALL -# filename: "" # Optional: Default to user or group -# group: "" # Group or User, not booth! -# nopassword: true -# runas: "" # Optional -# state: present # Optional: present or absent. Default to present -# user: "markus": # Group or User, not booth! +# filename: "" # Optional: Default to user or group +# group: "" # Group or User, not booth! +# nopassword: true # +# runas: "" # Optional +# state: present # Optional: absent removes this drop-in; defaults to present +# user: "markus": # Group or User, not booth! diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index e57cca5..5cfbe6e 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -41,8 +41,7 @@ - molecule-alice - molecule-bob - # The removal path of the role can only be observed on a drop-in file that exists before the role runs. - - name: Create the drop-in directory the seeded file lives in + - name: Create the drop-in directory for the seeded files ansible.builtin.file: path: /etc/sudoers.d state: directory @@ -50,10 +49,13 @@ group: root mode: "0750" - - name: Seed the drop-in file that the converge removes again + - name: Seed the obsolete and unmanaged drop-in files ansible.builtin.copy: content: "molecule-alice ALL=NOPASSWD: /usr/bin/true\n" - dest: /etc/sudoers.d/molecule-obsolete + dest: '/etc/sudoers.d/{{ item }}' owner: root group: root mode: "0440" + loop: + - molecule-obsolete + - molecule-unmanaged diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml index fe6fc4d..44c25fc 100644 --- a/molecule/default/verify.yml +++ b/molecule/default/verify.yml @@ -26,6 +26,16 @@ - not _directory.results[1].stat.exists fail_msg: "/etc/sudoers.d is not owned by the role or the file molecule-obsolete was not removed" + - name: Read the unmanaged drop-in file + ansible.builtin.slurp: + src: /etc/sudoers.d/molecule-unmanaged + register: _unmanaged_rule + + - name: Assert that the unmanaged drop-in file was preserved unchanged + ansible.builtin.assert: + that: >- + (_unmanaged_rule.content | b64decode) == 'molecule-alice ALL=NOPASSWD: /usr/bin/true\n' + - name: Stat the drop-in files of the declared rules ansible.builtin.stat: path: "/etc/sudoers.d/{{ item.key }}"