adding autoremove to apt

This commit is contained in:
Charles Paul 2016-02-05 12:42:25 -06:00
parent a6605c9c6f
commit d7a0a3b7e3

View file

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