90ba14d6 |
#!/usr/bin/python -tt |
7e9e2901 |
# -*- coding: utf-8 -*-
|
90ba14d6 |
# (c) 2012, Flowroute LLC
# Written by Matthew Williams <matthew@flowroute.com>
# Based on yum module written by Seth Vidal <skvidal at fedoraproject.org>
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
|
c976238a |
DOCUMENTATION = '''
---
module: apt |
1193810d |
short_description: Manages apt-packages |
c976238a |
description: |
caf003c8 |
- Manages I(apt) packages (such as for Debian/Ubuntu). |
c976238a |
version_added: "0.0.2"
options: |
e1c83b03 |
pkg: |
c976238a |
description: |
1193810d |
- A package name or package specifier with version, like C(foo) or C(foo=1.0) |
e1c83b03 |
required: true |
c976238a |
default: null
state:
description: |
f67aa85c |
- Indicates the desired package state |
c976238a |
required: false
default: present |
29d49d41 |
choices: [ "latest", "absent", "present" ] |
c976238a |
update_cache:
description: |
caf003c8 |
- Run the equivalent of C(apt-get update) before the operation. Can be run as part of the package installation or as a separate step |
c976238a |
required: false
default: "no"
choices: [ "yes", "no" ]
purge:
description: |
caf003c8 |
- Will force purging of configuration files if the module state is set to I(absent). |
c976238a |
required: false
default: "no"
choices: [ "yes", "no" ]
default_release:
description: |
1193810d |
- Corresponds to the C(-t) option for I(apt) and sets pin priorities |
c976238a |
required: false
default: null
install_recommends:
description: |
1193810d |
- Corresponds to the C(--no-install-recommends) option for I(apt), default behavior works as apt's default behavior, C(no) does not install recommended packages. Suggested packages are never installed. |
c976238a |
required: false
default: "no"
choices: [ "yes", "no" ]
force:
description: |
f67aa85c |
- If C(yes), force installs/removes. |
c976238a |
required: false
default: "no"
choices: [ "yes", "no" ]
author: Matthew Williams |
1193810d |
notes: []
examples: |
feab57e2 |
- code: "apt: pkg=foo update-cache=yes" |
1193810d |
description: Update repositories cache and install C(foo) package |
feab57e2 |
- code: "apt: pkg=foo state=removed" |
1193810d |
description: Remove C(foo) package |
feab57e2 |
- code: "apt: pkg=foo state=installed" |
54257a6a |
description: Install the package C(foo) |
feab57e2 |
- code: "apt: pkg=foo=1.00 state=installed" |
1193810d |
description: Install the version '1.00' of package C(foo) |
feab57e2 |
- code: "apt: pkg=nginx state=latest default-release=squeeze-backports update-cache=yes" |
1193810d |
description: Update the repository cache and update package C(ngnix) to latest version using default release C(squeeze-backport) |
feab57e2 |
- code: "apt: pkg=openjdk-6-jdk state=latest install-recommends=no" |
caf003c8 |
description: Install latest version of C(openjdk-6-jdk) ignoring C(install-reccomends) |
c976238a |
'''
|
e85355f0 |
import traceback |
f38b9d1c |
# added to stave off future warnings about apt api |
477ca2ed |
import warnings |
f38b9d1c |
warnings.filterwarnings('ignore', "apt API not stable yet", FutureWarning)
|
daf44331 |
# APT related constants |
9fdd7a83 |
APT_PATH = "/usr/bin/apt-get" |
1a88a336 |
APT = "DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical %s" % APT_PATH |
90ba14d6 |
def run_apt(command):
try: |
477ca2ed |
cmd = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
90ba14d6 |
out, err = cmd.communicate()
except (OSError, IOError), e:
rc = 1
err = str(e)
out = ''
except:
rc = 1
err = traceback.format_exc()
out = ''
else:
rc = cmd.returncode |
9fdd7a83 |
return rc, out, err |
e85355f0 |
|
ec4d5fa2 |
def package_split(pkgspec):
parts = pkgspec.split('=')
if len(parts) > 1:
return parts[0], parts[1]
else:
return parts[0], None
|
daf44331 |
def package_status(m, pkgname, version, cache): |
90ba14d6 |
try: |
ec4d5fa2 |
pkg = cache[pkgname]
except KeyError: |
daf44331 |
m.fail_json(msg="No package matching '%s' is available" % pkgname) |
ec4d5fa2 |
if version: |
1d553830 |
try :
return pkg.is_installed and pkg.installed.version == version, False |
7a67145e |
except AttributeError: |
1d553830 |
#assume older version of python-apt is installed
return pkg.isInstalled and pkg.installedVersion == version, False |
ec4d5fa2 |
else: |
1d553830 |
try :
return pkg.is_installed, pkg.is_upgradable |
7a67145e |
except AttributeError: |
1d553830 |
#assume older version of python-apt is installed
return pkg.isInstalled, pkg.isUpgradable |
e85355f0 |
|
daf44331 |
def install(m, pkgspec, cache, upgrade=False, default_release=None, install_recommends=True, force=False): |
b9739102 |
packages = "" |
418445d3 |
for package in pkgspec:
name, version = package_split(package)
installed, upgradable = package_status(m, name, version, cache)
if not installed or (upgrade and upgradable): |
b9739102 |
packages += "'%s' " % package |
faed4b5a |
|
b9739102 |
if len(packages) != 0: |
a35a0f88 |
if force:
force_yes = '--force-yes'
else:
force_yes = ''
|
b9739102 |
cmd = "%s --option Dpkg::Options::=--force-confold -q -y %s install %s" % (APT, force_yes,packages) |
d5f3760a |
if default_release:
cmd += " -t '%s'" % (default_release,) |
fced8cf6 |
if not install_recommends:
cmd += " --no-install-recommends" |
4c8d9496 |
|
90ba14d6 |
rc, out, err = run_apt(cmd) |
daf44331 |
if rc: |
6baaea90 |
m.fail_json(msg="'apt-get install %s' failed: %s" % (packages, err)) |
daf44331 |
else:
m.exit_json(changed=True) |
c819f171 |
else: |
daf44331 |
m.exit_json(changed=False) |
e85355f0 |
|
daf44331 |
def remove(m, pkgspec, cache, purge=False): |
b9739102 |
packages = "" |
418445d3 |
for package in pkgspec:
name, version = package_split(package)
installed, upgradable = package_status(m, name, version, cache)
if installed: |
b9739102 |
packages += "'%s' " % package |
faed4b5a |
|
418445d3 |
if len(packages) == 0: |
daf44331 |
m.exit_json(changed=False) |
90ba14d6 |
else: |
9fdd7a83 |
purge = '--purge' if purge else '' |
b9739102 |
cmd = "%s -q -y %s remove %s" % (APT, purge,packages) |
e85355f0 |
rc, out, err = run_apt(cmd) |
daf44331 |
if rc: |
418445d3 |
m.fail_json(msg="'apt-get remove %s' failed: %s" % (packages, err)) |
daf44331 |
m.exit_json(changed=True) |
7a67145e |
|
90ba14d6 |
|
4c8d9496 |
def main():
module = AnsibleModule(
argument_spec = dict( |
798c35d8 |
state = dict(default='installed', choices=['installed', 'latest', 'removed', 'absent', 'present']), |
9bcc18d1 |
update_cache = dict(default='no', choices=['yes', 'no'], aliases=['update-cache']), |
4c8d9496 |
purge = dict(default='no', choices=['yes', 'no']),
package = dict(default=None, aliases=['pkg', 'name']),
default_release = dict(default=None, aliases=['default-release']),
install_recommends = dict(default='yes', aliases=['install-recommends'], choices=['yes', 'no']),
force = dict(default='no', choices=['yes', 'no'])
)
)
|
daf44331 |
try: |
477ca2ed |
import apt
import apt_pkg |
daf44331 |
except: |
14c2e8de |
module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.") |
daf44331 |
|
4c8d9496 |
if not os.path.exists(APT_PATH):
module.fail_json(msg="Cannot find apt-get")
p = module.params
if p['package'] is None and p['update_cache'] != 'yes': |
a9c2e597 |
module.fail_json(msg='pkg=name and/or update_cache=yes is required') |
7a67145e |
|
21a35bde |
install_recommends = module.boolean(p['install_recommends']) |
7a67145e |
|
4c8d9496 |
cache = apt.Cache()
if p['default_release']:
apt_pkg.config['APT::Default-Release'] = p['default_release']
# reopen cache w/ modified config
cache.open(progress=None) |
7a67145e |
if module.boolean(p['update_cache']): |
4c8d9496 |
cache.update()
cache.open(progress=None)
if p['package'] == None:
module.exit_json(changed=False) |
7a67145e |
force_yes = module.boolean(p['force']) |
faed4b5a |
|
418445d3 |
packages = p['package'].split(',') |
faed4b5a |
latest = p['state'] == 'latest' |
418445d3 |
for package in packages:
if package.count('=') > 1: |
b9739102 |
module.fail_json(msg="invalid package spec: %s" % package)
if latest and '=' in package:
module.fail_json(msg='version number inconsistent with state=latest: %s' % package) |
7a67145e |
|
4c8d9496 |
if p['state'] == 'latest': |
418445d3 |
install(module, packages, cache, upgrade=True,
default_release=p['default_release'],
install_recommends=install_recommends,
force=force_yes) |
798c35d8 |
elif p['state'] in [ 'installed', 'present' ]: |
418445d3 |
install(module, packages, cache, default_release=p['default_release'], |
daf44331 |
install_recommends=install_recommends,force=force_yes) |
798c35d8 |
elif p['state'] in [ 'removed', 'absent' ]: |
418445d3 |
remove(module, packages, cache, purge = module.boolean(p['purge'])) |
7a67145e |
|
4c8d9496 |
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> |
90ba14d6 |
|
4c8d9496 |
main() |
90ba14d6 |
|