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

Correct yum and dnf autoremove behavior #47902

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/yumdnf-autoremove.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- "fix yum and dnf autoremove input sanitization to properly warn user if invalid options passed and update documentation to match"
15 changes: 14 additions & 1 deletion lib/ansible/module_utils/yumdnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
security=dict(type='bool', default=False),
skip_broken=dict(type='bool', default=False),
# removed==absent, installed==present, these are accepted as aliases
state=dict(type='str', default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
state=dict(type='str', default=None, choices=['absent', 'installed', 'latest', 'present', 'removed']),
update_cache=dict(type='bool', default=False, aliases=['expire-cache']),
update_only=dict(required=False, default="no", type='bool'),
validate_certs=dict(type='bool', default=True),
Expand Down Expand Up @@ -104,6 +104,19 @@ def __init__(self, module):
'string of packages or a list of packages.'
)

# Sanity checking for autoremove
if self.state is None:
if self.autoremove:
self.state = "absent"
else:
self.state = "present"

if self.autoremove and (self.state != "absent"):
self.module.fail_json(
msg="Autoremove should be used alone or with state=absent",
results=[],
)

# This should really be redefined by both the yum and dnf module but a
# default isn't a bad idea
self.lockfile = '/var/run/yum.pid'
Expand Down
8 changes: 2 additions & 6 deletions lib/ansible/modules/packaging/os/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
state:
description:
- Whether to install (C(present), C(latest)), or remove (C(absent)) a package.
- Default is C(None), however in effect the default action is C(present) unless the C(autoremove) option is
enabled for this module, then C(absent) is inferred.
choices: ['absent', 'present', 'installed', 'removed', 'latest']
default: "present"

enablerepo:
description:
Expand Down Expand Up @@ -1041,11 +1042,6 @@ def run(self):
msg="Autoremove requires dnf>=2.0.1. Current dnf version is %s" % dnf.__version__,
results=[],
)
if self.state not in ["absent", None]:
self.module.fail_json(
msg="Autoremove should be used alone or with state=absent",
results=[],
)

if self.update_cache and not self.names and not self.list:
self.base = self._base(
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/packaging/os/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
- C(present) and C(installed) will simply ensure that a desired package is installed.
- C(latest) will update the specified package if it's not of the latest available version.
- C(absent) and C(removed) will remove the specified package.
- Default is C(None), however in effect the default action is C(present) unless the C(autoremove) option is¬
enabled for this module, then C(absent) is inferred.
choices: [ absent, installed, latest, present, removed ]
default: present
enablerepo:
description:
- I(Repoid) of repositories to enable for the install/update operation.
Expand Down