From c8ed5c29e9f8dfe1debfbb576996beda7e75205f Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Mon, 8 Oct 2018 07:39:49 -0500 Subject: [PATCH] allow update_cache as stand alone operation for yum/dnf (#46183) * allow update_cache as stand alone operation for yum/dnf Fixes #40068 Signed-off-by: Adam Miller * make sanity tests happy Signed-off-by: Adam Miller --- changelogs/fragments/yumdnf-update-cache.yaml | 3 ++ lib/ansible/module_utils/yumdnf.py | 2 +- lib/ansible/modules/packaging/os/dnf.py | 30 +++++++++++++++++-- lib/ansible/modules/packaging/os/yum.py | 17 +++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/yumdnf-update-cache.yaml diff --git a/changelogs/fragments/yumdnf-update-cache.yaml b/changelogs/fragments/yumdnf-update-cache.yaml new file mode 100644 index 0000000000..23e4fa3b29 --- /dev/null +++ b/changelogs/fragments/yumdnf-update-cache.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - "yum and dnf can now perform C(update_cache) as a standalone operation for consistency with other package manager modules" diff --git a/lib/ansible/module_utils/yumdnf.py b/lib/ansible/module_utils/yumdnf.py index eed3ebf21b..97165b3055 100644 --- a/lib/ansible/module_utils/yumdnf.py +++ b/lib/ansible/module_utils/yumdnf.py @@ -48,7 +48,7 @@ yumdnf_argument_spec = dict( lock_poll=dict(type='int', default=-1), lock_timeout=dict(type='int', default=10), ), - required_one_of=[['name', 'list']], + required_one_of=[['name', 'list', 'update_cache']], mutually_exclusive=[['name', 'list']], supports_check_mode=True, ) diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index 33b855ec93..77ddaf277c 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -576,7 +576,14 @@ class DnfModule(YumDnf): base = dnf.Base() self._configure_base(base, conf_file, disable_gpg_check, installroot) self._specify_repositories(base, disablerepo, enablerepo) - base.fill_sack(load_system_repo='auto') + try: + base.fill_sack(load_system_repo='auto') + except dnf.exceptions.RepoError as e: + self.module.fail_json( + msg="{0}".format(to_text(e)), + results=[], + rc=1 + ) if self.bugfix: key = {'advisory_type__eq': 'bugfix'} base._update_security_filters = [base.sack.query().filter(**key)] @@ -584,7 +591,14 @@ class DnfModule(YumDnf): key = {'advisory_type__eq': 'security'} base._update_security_filters = [base.sack.query().filter(**key)] if self.update_cache: - base.update_cache() + try: + base.update_cache() + except dnf.exceptions.RepoError as e: + self.module.fail_json( + msg="{0}".format(to_text(e)), + results=[], + rc=1 + ) return base def list_items(self, command): @@ -1041,6 +1055,18 @@ class DnfModule(YumDnf): results=[], ) + if self.update_cache and not self.names and not self.list: + self.base = self._base( + self.conf_file, self.disable_gpg_check, self.disablerepo, + self.enablerepo, self.installroot + ) + self.module.exit_json( + msg="Cache updated", + changed=False, + results=[], + rc=0 + ) + # Set state as installed by default # This is not set in AnsibleModule() because the following shouldn't happend # - dnf: autoremove=yes state=installed diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index 7cb31baedf..ac1080ea4d 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -1461,6 +1461,23 @@ class YumModule(YumDnf): if error_msgs: self.module.fail_json(msg='. '.join(error_msgs)) + if self.update_cache and not self.names and not self.list: + rc, stdout, stderr = self.module.run_command(self.yum_basecmd + ['clean', 'expire-cache']) + if rc == 0: + self.module.exit_json( + changed=False, + msg="Cache updated", + rc=rc, + results=[] + ) + else: + self.module.exit_json( + changed=False, + msg="Failed to update cache", + rc=rc, + results=[stderr], + ) + # fedora will redirect yum to dnf, which has incompatibilities # with how this module expects yum to operate. If yum-deprecated # is available, use that instead to emulate the old behaviors.