Skip to content

Commit

Permalink
sudoers: fix handling of state: absent (#4852) (#4853)
Browse files Browse the repository at this point in the history
* sudoers: fix handling of state: absent (#4852)

* typo fixes

(cherry picked from commit 44e21dd)
  • Loading branch information
s-hamann authored and patchback[bot] committed Jun 19, 2022
1 parent 48fbd69 commit 83358c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/4852-sudoers-state-absent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "sudoers - fix incorrect handling of ``state: absent`` (https://github.com/ansible-collections/community.general/issues/4852)."
9 changes: 6 additions & 3 deletions plugins/modules/system/sudoers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ def content(self):
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
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/targets/sudoers/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -166,3 +179,4 @@
ansible.builtin.assert:
that:
- not revoke_rule_1_stat.stat.exists
- not revoke_non_existing_rule_stat.stat.exists

0 comments on commit 83358c3

Please sign in to comment.