From 44e21dd407fa86a7de78258c52871b3db1b0f187 Mon Sep 17 00:00:00 2001 From: s-hamann <10639154+s-hamann@users.noreply.github.com> Date: Sun, 19 Jun 2022 13:34:24 +0000 Subject: [PATCH] sudoers: fix handling of state: absent (#4852) (#4853) * sudoers: fix handling of state: absent (#4852) * typo fixes --- changelogs/fragments/4852-sudoers-state-absent.yml | 2 ++ plugins/modules/system/sudoers.py | 9 ++++++--- tests/integration/targets/sudoers/tasks/main.yml | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/4852-sudoers-state-absent.yml diff --git a/changelogs/fragments/4852-sudoers-state-absent.yml b/changelogs/fragments/4852-sudoers-state-absent.yml new file mode 100644 index 0000000000..013041a15f --- /dev/null +++ b/changelogs/fragments/4852-sudoers-state-absent.yml @@ -0,0 +1,2 @@ +bugfixes: + - "sudoers - fix incorrect handling of ``state: absent`` (https://github.com/ansible-collections/community.general/issues/4852)." diff --git a/plugins/modules/system/sudoers.py b/plugins/modules/system/sudoers.py index 8b8ad50405..d96716c7f9 100644 --- a/plugins/modules/system/sudoers.py +++ b/plugins/modules/system/sudoers.py @@ -168,9 +168,12 @@ class Sudoers(object): return "{owner} ALL={runas}{nopasswd} {commands}\n".format(owner=owner, runas=runas_str, nopasswd=nopasswd_str, commands=commands_str) def run(self): - if self.state == 'absent' and self.exists(): - self.delete() - return True + if self.state == 'absent': + if self.exists(): + self.delete() + return True + else: + return False if self.exists() and self.matches(): return False diff --git a/tests/integration/targets/sudoers/tasks/main.yml b/tests/integration/targets/sudoers/tasks/main.yml index 634eded779..f3be2d8092 100644 --- a/tests/integration/targets/sudoers/tasks/main.yml +++ b/tests/integration/targets/sudoers/tasks/main.yml @@ -135,6 +135,18 @@ register: revoke_rule_1_stat +- name: Revoke non-existing rule + community.general.sudoers: + name: non-existing-rule + state: absent + register: revoke_non_existing_rule + +- name: Stat non-existing rule + ansible.builtin.stat: + path: "{{ sudoers_path }}/non-existing-rule" + register: revoke_non_existing_rule_stat + + # Run assertions - name: Check rule 1 file stat @@ -151,6 +163,7 @@ - rule_1_again is not changed - rule_5 is changed - revoke_rule_1 is changed + - revoke_non_existing_rule is not changed - name: Check contents ansible.builtin.assert: @@ -166,3 +179,4 @@ ansible.builtin.assert: that: - not revoke_rule_1_stat.stat.exists + - not revoke_non_existing_rule_stat.stat.exists