From 1c777976c5134f310a97609da12776fc08551271 Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Mon, 5 Nov 2018 15:00:42 -0600 Subject: [PATCH] Correct yum and dnf autoremove behavior (#47902) * Correct yum and dnf autoremove behavior Sanity check args passed to autoremove Fixes #47184 Signed-off-by: Adam Miller * fix docs Signed-off-by: Adam Miller --- changelogs/fragments/yumdnf-autoremove.yaml | 3 +++ lib/ansible/module_utils/yumdnf.py | 15 ++++++++++++++- lib/ansible/modules/packaging/os/dnf.py | 8 ++------ lib/ansible/modules/packaging/os/yum.py | 3 ++- 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/yumdnf-autoremove.yaml diff --git a/changelogs/fragments/yumdnf-autoremove.yaml b/changelogs/fragments/yumdnf-autoremove.yaml new file mode 100644 index 0000000000..264356b688 --- /dev/null +++ b/changelogs/fragments/yumdnf-autoremove.yaml @@ -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" diff --git a/lib/ansible/module_utils/yumdnf.py b/lib/ansible/module_utils/yumdnf.py index a158e89157..7f5e550093 100644 --- a/lib/ansible/module_utils/yumdnf.py +++ b/lib/ansible/module_utils/yumdnf.py @@ -40,7 +40,7 @@ yumdnf_argument_spec = dict( 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), @@ -104,6 +104,19 @@ class YumDnf(with_metaclass(ABCMeta, object)): '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' diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index b286f2b666..3924007538 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -41,8 +41,9 @@ options: 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: @@ -1041,11 +1042,6 @@ class DnfModule(YumDnf): 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( diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index 2cbee1ea3b..ecf0081170 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -55,8 +55,9 @@ options: - 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.