From 326f7166c0dceb1addc383f579427258fc680b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Fri, 6 May 2016 09:17:04 +0200 Subject: [PATCH] apt: fix always changed when using deb file on Debian 8 (#3541) Extends d74e82a3f1e3293ffb70c172e5bb2b6e1ff6dbcc with also support downgrade. --- lib/ansible/modules/packaging/os/apt.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py index 450579eadd..7548225786 100644 --- a/lib/ansible/modules/packaging/os/apt.py +++ b/lib/ansible/modules/packaging/os/apt.py @@ -469,6 +469,14 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None, else: return (True, dict(changed=False)) +def get_field_of_deb(m, deb_file, field="Version"): + cmd_dpkg = m.get_bin_path("dpkg", True) + cmd = cmd_dpkg + " --field %s %s" % (deb_file, field) + rc, stdout, stderr = m.run_command(cmd) + if rc != 0: + m.fail_json(msg="%s failed" % cmd, stdout=stdout, stderr=stderr) + return stdout.strip('\n') + def install_deb(m, debs, cache, force, install_recommends, allow_unauthenticated, dpkg_options): changed=False deps_to_install = [] @@ -476,10 +484,17 @@ def install_deb(m, debs, cache, force, install_recommends, allow_unauthenticated for deb_file in debs.split(','): try: pkg = apt.debfile.DebPackage(deb_file) - - # Check if it's already installed - if pkg.compare_to_version_in_cache() == pkg.VERSION_SAME: - continue + pkg_name = get_field_of_deb(m, deb_file, "Package") + pkg_version = get_field_of_deb(m, deb_file, "Version") + try: + installed_pkg = apt.Cache()[pkg_name] + installed_version = installed_pkg.installed.version + if package_version_compare(pkg_version, installed_version) == 0: + # Does not need to down-/upgrade, move on to next package + continue + except Exception, e: + # Must not be installed, continue with installation + pass # Check if package is installable if not pkg.check() and not force: m.fail_json(msg=pkg._failure_string) @@ -659,7 +674,7 @@ def main(): cache_valid_time = dict(type='int'), purge = dict(default=False, type='bool'), package = dict(default=None, aliases=['pkg', 'name'], type='list'), - deb = dict(default=None), + deb = dict(default=None, type='path'), default_release = dict(default=None, aliases=['default-release']), install_recommends = dict(default=None, aliases=['install-recommends'], type='bool'), force = dict(default='no', type='bool'),