mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
apt_rpm: add check if package version is upgradable (ansible-collections#7414) (#7577)
* apt_rpm: add check if package version is upgradable (ansible-collections#7414) * add changelog fragment * apt_rpm: apply the recommended changes and fix the sanity checks --------- Co-authored-by: Nikolay Burykin <bne@altlinux.org>
This commit is contained in:
parent
30edafabe7
commit
ecea4a2f38
2 changed files with 22 additions and 1 deletions
2
changelogs/fragments/7577-fix-apt_rpm-module.yml
Normal file
2
changelogs/fragments/7577-fix-apt_rpm-module.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- apt-rpm - the module did not upgrade packages if a newer version exists. Now the package will be reinstalled if the candidate is newer than the installed version (https://github.com/ansible-collections/community.general/issues/7414).
|
|
@ -115,6 +115,7 @@ EXAMPLES = '''
|
|||
'''
|
||||
|
||||
import os
|
||||
import re
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import (
|
||||
|
@ -132,6 +133,7 @@ else:
|
|||
HAS_RPM_PYTHON = True
|
||||
RPM_PYTHON_IMPORT_ERROR = None
|
||||
|
||||
APT_CACHE = "/usr/bin/apt-cache"
|
||||
APT_PATH = "/usr/bin/apt-get"
|
||||
RPM_PATH = "/usr/bin/rpm"
|
||||
APT_GET_ZERO = "\n0 upgraded, 0 newly installed"
|
||||
|
@ -165,6 +167,19 @@ def query_package(module, name):
|
|||
return False
|
||||
|
||||
|
||||
def check_package_version(module, name):
|
||||
# compare installed and candidate version
|
||||
# if newest version already installed return True
|
||||
# otherwise return False
|
||||
|
||||
rc, out, err = module.run_command([APT_CACHE, "policy", name], environ_update={"LANG": "C"})
|
||||
installed = re.split("\n |: ", out)[2]
|
||||
candidate = re.split("\n |: ", out)[4]
|
||||
if installed >= candidate:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def query_package_provides(module, name):
|
||||
# rpm -q returns 0 if the package is installed,
|
||||
# 1 if it is not installed
|
||||
|
@ -179,7 +194,11 @@ def query_package_provides(module, name):
|
|||
name = local_rpm_package_name(name)
|
||||
|
||||
rc, out, err = module.run_command("%s -q --provides %s" % (RPM_PATH, name))
|
||||
return rc == 0
|
||||
if rc == 0:
|
||||
if check_package_version(module, name):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def update_package_db(module):
|
||||
|
|
Loading…
Reference in a new issue