From 295174b3fdf439b3db91fd149f57f7af5bb6c6ae Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Tue, 13 Nov 2018 14:04:19 -0600 Subject: [PATCH] make yum update_only option actually work (#47942) * make yum update_only option actually work Fixes #40615 Signed-off-by: Adam Miller * fix changlog fragment for sanity check Signed-off-by: Adam Miller * only attempt an update when there are packages to update Signed-off-by: Adam Miller * fix logic to properly handle the precedent of operations Signed-off-by: Adam Miller * remove q debug statements Signed-off-by: Adam Miller (cherry picked from commit 34fc66185e8f62f784c5d14233d504a00b3e5a05) --- changelogs/yum-update-only.yaml | 3 ++ lib/ansible/modules/packaging/os/yum.py | 44 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 changelogs/yum-update-only.yaml diff --git a/changelogs/yum-update-only.yaml b/changelogs/yum-update-only.yaml new file mode 100644 index 00000000000..31c4bd0b12d --- /dev/null +++ b/changelogs/yum-update-only.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - "yum now properly supports update_only option" diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index 9bede71fc52..790c2779c01 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -1199,7 +1199,9 @@ class YumModule(YumDnf): self.module.fail_json(msg="Failed to get nevra information from RPM package: %s" % spec) # local rpm files can't be updated - if not self.is_installed(repoq, envra): + if self.is_installed(repoq, envra): + pkgs['update'].append(spec) + else: pkgs['install'].append(spec) continue @@ -1214,13 +1216,15 @@ class YumModule(YumDnf): self.module.fail_json(msg="Failed to get nevra information from RPM package: %s" % spec) # local rpm files can't be updated - if not self.is_installed(repoq, envra): - pkgs['install'].append(package) + if self.is_installed(repoq, envra): + pkgs['update'].append(spec) + else: + pkgs['install'].append(spec) continue # dep/pkgname - find it else: - if self.is_installed(repoq, spec) or self.update_only: + if self.is_installed(repoq, spec): pkgs['update'].append(spec) else: pkgs['install'].append(spec) @@ -1290,7 +1294,10 @@ class YumModule(YumDnf): else: to_update.append((w, '%s.%s from %s' % (updates[w]['version'], updates[w]['dist'], updates[w]['repo']))) - res['changes'] = dict(installed=pkgs['install'], updated=to_update) + if self.update_only: + res['changes'] = dict(installed=[], updated=to_update) + else: + res['changes'] = dict(installed=pkgs['install'], updated=to_update) if will_update or pkgs['install']: res['changed'] = True @@ -1304,7 +1311,18 @@ class YumModule(YumDnf): if cmd: # update all rc, out, err = self.module.run_command(cmd) res['changed'] = True - elif pkgs['install'] or will_update: + elif self.update_only: + if pkgs['update']: + cmd = self.yum_basecmd + ['update'] + pkgs['update'] + lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C') + rc, out, err = self.module.run_command(cmd, environ_update=lang_env) + out_lower = out.strip().lower() + if not out_lower.endswith("no packages marked for update") and \ + not out_lower.endswith("nothing to do"): + res['changed'] = True + else: + rc, out, err = [0, '', ''] + elif pkgs['install'] or will_update and not self.update_only: cmd = self.yum_basecmd + ['install'] + pkgs['install'] + pkgs['update'] lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C') rc, out, err = self.module.run_command(cmd, environ_update=lang_env) @@ -1423,13 +1441,7 @@ class YumModule(YumDnf): self.module.fail_json(msg="Error setting/accessing repos: %s" % to_native(e)) except yum.Errors.YumBaseError as e: self.module.fail_json(msg="Error accessing repos: %s" % to_native(e)) - if self.state in ('installed', 'present'): - if self.disable_gpg_check: - self.yum_basecmd.append('--nogpgcheck') - res = self.install(pkgs, repoq) - elif self.state in ('removed', 'absent'): - res = self.remove(pkgs, repoq) - elif self.state == 'latest': + if self.state == 'latest' or self.update_only: if self.disable_gpg_check: self.yum_basecmd.append('--nogpgcheck') if self.security: @@ -1437,6 +1449,12 @@ class YumModule(YumDnf): if self.bugfix: self.yum_basecmd.append('--bugfix') res = self.latest(pkgs, repoq) + elif self.state in ('installed', 'present'): + if self.disable_gpg_check: + self.yum_basecmd.append('--nogpgcheck') + res = self.install(pkgs, repoq) + elif self.state in ('removed', 'absent'): + res = self.remove(pkgs, repoq) else: # should be caught by AnsibleModule argument_spec self.module.fail_json(