diff --git a/packaging/os/apt.py b/packaging/os/apt.py index 84a019ae203..cafe57eaf15 100755 --- a/packaging/os/apt.py +++ b/packaging/os/apt.py @@ -94,6 +94,13 @@ options: - Path to a .deb package on the remote machine. required: false version_added: "1.6" + autoremove: + description: + - If C(yes), remove unused dependency packages for all module states except I(build-dep). + required: false + default: no + choices: [ "yes", "no" ] + aliases: [ 'autoclean'] requirements: [ python-apt, aptitude ] author: "Matthew Williams (@mgwilliams)" notes: @@ -342,7 +349,7 @@ def expand_pkgspec_from_fnmatches(m, pkgspec, cache): def install(m, pkgspec, cache, upgrade=False, default_release=None, install_recommends=None, force=False, dpkg_options=expand_dpkg_options(DPKG_OPTIONS), - build_dep=False): + build_dep=False, autoremove=False): pkg_list = [] packages = "" pkgspec = expand_pkgspec_from_fnmatches(m, pkgspec, cache) @@ -376,13 +383,18 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None, else: check_arg = '' + if autoremove: + autoremove = '--auto-remove' + else: + autoremove = '' + for (k,v) in APT_ENV_VARS.iteritems(): os.environ[k] = v if build_dep: cmd = "%s -y %s %s %s build-dep %s" % (APT_GET_CMD, dpkg_options, force_yes, check_arg, packages) else: - cmd = "%s -y %s %s %s install %s" % (APT_GET_CMD, dpkg_options, force_yes, check_arg, packages) + cmd = "%s -y %s %s %s %s install %s" % (APT_GET_CMD, dpkg_options, force_yes, autoremove, check_arg, packages) if default_release: cmd += " -t '%s'" % (default_release,) @@ -465,7 +477,7 @@ def install_deb(m, debs, cache, force, install_recommends, dpkg_options): m.exit_json(changed=changed, stdout=retvals.get('stdout',''), stderr=retvals.get('stderr','')) def remove(m, pkgspec, cache, purge=False, - dpkg_options=expand_dpkg_options(DPKG_OPTIONS)): + dpkg_options=expand_dpkg_options(DPKG_OPTIONS), autoremove=False): pkg_list = [] pkgspec = expand_pkgspec_from_fnmatches(m, pkgspec, cache) for package in pkgspec: @@ -486,7 +498,12 @@ def remove(m, pkgspec, cache, purge=False, for (k,v) in APT_ENV_VARS.iteritems(): os.environ[k] = v - cmd = "%s -q -y %s %s remove %s" % (APT_GET_CMD, dpkg_options, purge, packages) + if autoremove: + autoremove = '--auto-remove' + else: + autoremove = '' + + cmd = "%s -q -y %s %s %s remove %s" % (APT_GET_CMD, dpkg_options, purge, autoremove, packages) if m.check_mode: m.exit_json(changed=True) @@ -558,7 +575,8 @@ def main(): install_recommends = dict(default=None, aliases=['install-recommends'], type='bool'), force = dict(default='no', type='bool'), upgrade = dict(choices=['no', 'yes', 'safe', 'full', 'dist']), - dpkg_options = dict(default=DPKG_OPTIONS) + dpkg_options = dict(default=DPKG_OPTIONS), + autoremove = dict(type='bool', default=False, aliases=['autoclean']) ), mutually_exclusive = [['package', 'upgrade', 'deb']], required_one_of = [['package', 'upgrade', 'update_cache', 'deb']], @@ -592,6 +610,7 @@ def main(): updated_cache_time = 0 install_recommends = p['install_recommends'] dpkg_options = expand_dpkg_options(p['dpkg_options']) + autoremove = p['autoremove'] # Deal with deprecated aliases if p['state'] == 'installed': @@ -674,7 +693,7 @@ def main(): default_release=p['default_release'], install_recommends=install_recommends, force=force_yes, dpkg_options=dpkg_options, - build_dep=state_builddep) + build_dep=state_builddep, autoremove=autoremove) (success, retvals) = result retvals['cache_updated']=updated_cache retvals['cache_update_time']=updated_cache_time @@ -683,7 +702,7 @@ def main(): else: module.fail_json(**retvals) elif p['state'] == 'absent': - remove(module, packages, cache, p['purge'], dpkg_options) + remove(module, packages, cache, p['purge'], dpkg_options, autoremove) except apt.cache.LockFailedException: module.fail_json(msg="Failed to lock apt for exclusive operation")