Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #4853/44e21dd4 backport][stable-4] sudoers: fix handling of state: absent (#4852) #4857

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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