From 05608ea6580e0e74eb3aa2a17a189aa3f97c2a4b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 2 Dec 2023 22:03:32 +0100 Subject: [PATCH] [stable-8] apt_rpm: add check if package version is upgradable (ansible-collections#7414) (#7669) 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 (cherry picked from commit ecea4a2f38976db11ae7250172e14e1a84ae2f0b) Co-authored-by: burykinne <67783534+burykinne@users.noreply.github.com> --- .../fragments/7577-fix-apt_rpm-module.yml | 2 ++ plugins/modules/apt_rpm.py | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/7577-fix-apt_rpm-module.yml diff --git a/changelogs/fragments/7577-fix-apt_rpm-module.yml b/changelogs/fragments/7577-fix-apt_rpm-module.yml new file mode 100644 index 0000000000..ef55eb5bd2 --- /dev/null +++ b/changelogs/fragments/7577-fix-apt_rpm-module.yml @@ -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). diff --git a/plugins/modules/apt_rpm.py b/plugins/modules/apt_rpm.py index d93db03e02..de1b574114 100644 --- a/plugins/modules/apt_rpm.py +++ b/plugins/modules/apt_rpm.py @@ -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):