make yum update_only option actually work (#47942)

* make yum update_only option actually work

Fixes #40615

Signed-off-by: Adam Miller <admiller@redhat.com>

* fix changlog fragment for sanity check

Signed-off-by: Adam Miller <admiller@redhat.com>

* only attempt an update when there are packages to update

Signed-off-by: Adam Miller <admiller@redhat.com>

* fix logic to properly handle the precedent of operations

Signed-off-by: Adam Miller <admiller@redhat.com>

* remove q debug statements

Signed-off-by: Adam Miller <admiller@redhat.com>
This commit is contained in:
Adam Miller 2018-11-13 14:04:19 -06:00 committed by ansibot
parent 50da276544
commit 34fc66185e
2 changed files with 34 additions and 13 deletions

View File

@ -0,0 +1,3 @@
---
minor_changes:
- "yum now properly supports update_only option"

View File

@ -1184,7 +1184,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
@ -1199,13 +1201,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)
@ -1275,7 +1279,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
@ -1289,7 +1296,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)
@ -1407,13 +1425,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:
@ -1421,6 +1433,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(