2012-03-10 00:33:58 +01:00
#!/usr/bin/python -tt
2012-08-03 03:29:10 +02:00
# -*- coding: utf-8 -*-
2012-03-10 00:33:58 +01:00
# (c) 2012, Red Hat, Inc
# Written by Seth Vidal <skvidal at fedoraproject.org>
#
# This file is part of Ansible
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
#
2012-08-02 07:47:48 +02:00
2012-03-10 00:33:58 +01:00
import traceback
2012-08-02 07:47:48 +02:00
import os
2012-08-14 00:13:50 +02:00
import yum
2012-09-29 00:49:02 +02:00
DOCUMENTATION = '''
---
module: yum
short_description: Manages packages with the I(yum) package manager
description:
- Will install, upgrade, remove, and list packages with the I(yum) package manager.
options:
name:
description:
- package name, or package specifier with version, like C(name-1.0).
required: true
default: null
aliases: []
list:
description:
- various non-idempotent commands for usage with C(/usr/bin/ansible) and I(not) playbooks. See examples.
required: false
default: null
state:
description:
- whether to install (C(present), C(latest)), or remove (C(absent)) a package.
required: false
choices: [ "present", "latest", "absent" ]
default: "present"
2012-10-24 06:21:24 +02:00
enablerepo:
description:
2012-10-24 15:08:42 +02:00
- Repoid of repositories to enable for the install/update operation.
These repos will not persist beyond the transaction
multiple repos separated with a ','
New in version 0.9.
2012-10-24 06:21:24 +02:00
required: false
default: null
aliases: []
2012-10-24 15:08:42 +02:00
2012-10-24 06:21:24 +02:00
disablerepo:
description:
2012-10-24 15:08:42 +02:00
- Repoid of repositories to disable for the install/update operation
These repos will not persist beyond the transaction
Multiple repos separated with a ','
New in version 0.9.
2012-10-24 06:21:24 +02:00
required: false
default: null
aliases: []
2012-09-29 00:49:02 +02:00
examples:
2012-10-24 06:21:24 +02:00
- code: yum name=httpd state=latest
- code: yum name=httpd state=removed
- code: yum name=httpd enablerepo=testing state=installed
2012-09-29 00:49:02 +02:00
notes: []
# informational: requirements for nodes
requirements: [ yum, rpm ]
author: Seth Vidal
'''
2012-08-02 07:47:48 +02:00
def_qf = "%{name}-%{version}-%{release}.%{arch}"
2012-08-15 01:33:11 +02:00
2012-08-02 23:02:37 +02:00
repoquery='/usr/bin/repoquery'
2012-08-14 00:13:50 +02:00
if not os.path.exists(repoquery):
repoquery = None
2012-08-02 07:47:48 +02:00
2012-08-15 01:33:11 +02:00
yumbin='/usr/bin/yum'
2012-08-14 00:13:50 +02:00
def yum_base(conf_file=None, cachedir=False):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
my = yum.YumBase()
my.preconf.debuglevel=0
my.preconf.errorlevel=0
if conf_file and os.path.exists(conf_file):
my.preconf.fn = conf_file
if cachedir or os.geteuid() != 0:
if hasattr(my, 'setCacheDir'):
my.setCacheDir()
else:
cachedir = yum.misc.getCacheDir()
my.repos.setCacheDir(cachedir)
my.conf.cache = 0
return my
def po_to_nevra(po):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
if hasattr(po, 'ui_nevra'):
return po.ui_nevra
else:
return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
2012-10-24 06:21:24 +02:00
def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
if not repoq:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
pkgs = []
try:
my = yum_base(conf_file)
2012-10-24 06:21:24 +02:00
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
2012-08-14 00:13:50 +02:00
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
pkgs = e + m
if not pkgs:
pkgs.extend(my.returnInstalledPackagesByDep(pkgspec))
except Exception, e:
module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ]
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
else:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
rc,out,err = run(cmd)
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
rc2,out2,err2 = run(cmd)
if rc == 0 and rc2 == 0:
out += out2
return [ p for p in out.split('\n') if p.strip() ]
else:
2012-10-24 06:46:15 +02:00
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
2012-08-14 00:13:50 +02:00
2012-08-02 07:47:48 +02:00
return []
2012-10-24 06:21:24 +02:00
def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
if not repoq:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
pkgs = []
try:
my = yum_base(conf_file)
2012-10-24 06:21:24 +02:00
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
2012-08-14 00:13:50 +02:00
e,m,u = my.pkgSack.matchPackageNames([pkgspec])
pkgs = e + m
if not pkgs:
pkgs.extend(my.returnPackagesByDep(pkgspec))
except Exception, e:
module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ]
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
else:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
cmd = repoq + ["--qf", qf, pkgspec]
rc,out,err = run(cmd)
if rc == 0:
return [ p for p in out.split('\n') if p.strip() ]
else:
2012-10-24 06:46:15 +02:00
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
2012-08-14 00:13:50 +02:00
2012-08-02 07:47:48 +02:00
return []
2012-10-24 06:21:24 +02:00
def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
if not repoq:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
retpkgs = []
pkgs = []
updates = []
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
try:
my = yum_base(conf_file)
2012-10-24 06:21:24 +02:00
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
2012-08-14 00:13:50 +02:00
pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec)
if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([pkgspec])
pkgs = e + m
updates = my.doPackageLists(pkgnarrow='updates').updates
except Exception, e:
module.fail_json(msg="Failure talking to yum: %s" % e)
for pkg in pkgs:
if pkg in updates:
retpkgs.append(pkg)
return set([ po_to_nevra(p) for p in retpkgs ])
2012-08-07 02:07:02 +02:00
2012-08-14 00:13:50 +02:00
else:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
cmd = repoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
rc,out,err = run(cmd)
if rc == 0:
return set([ p for p in out.split('\n') if p.strip() ])
else:
2012-10-24 06:46:15 +02:00
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
2012-08-14 00:13:50 +02:00
2012-08-02 07:47:48 +02:00
return []
2012-10-24 06:21:24 +02:00
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
if not repoq:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
pkgs = []
try:
my = yum_base(conf_file)
2012-10-24 06:21:24 +02:00
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
2012-08-14 00:13:50 +02:00
pkgs = my.returnPackagesByDep(req_spec) + my.returnInstalledPackagesByDep(req_spec)
if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([req_spec])
pkgs.extend(e)
pkgs.extend(m)
e,m,u = my.rpmdb.matchPackageNames([req_spec])
pkgs.extend(e)
pkgs.extend(m)
except Exception, e:
module.fail_json(msg="Failure talking to yum: %s" % e)
return set([ po_to_nevra(p) for p in pkgs ])
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
else:
2012-08-15 01:33:11 +02:00
2012-08-14 00:13:50 +02:00
cmd = repoq + ["--qf", qf, "--whatprovides", req_spec]
rc,out,err = run(cmd)
cmd = repoq + ["--qf", qf, req_spec]
rc2,out2,err2 = run(cmd)
if rc == 0 and rc2 == 0:
out += out2
2012-10-19 22:28:39 +02:00
pkgs = set([ p for p in out.split('\n') if p.strip() ])
if not pkgs:
pkgs = is_installed(module, repoq, req_spec, conf_file, qf=qf)
return pkgs
2012-08-14 00:13:50 +02:00
else:
2012-10-24 06:46:15 +02:00
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
2012-08-14 00:13:50 +02:00
return []
2012-03-10 00:33:58 +01:00
2012-08-14 22:23:40 +02:00
def local_nvra(path):
"""return nvra of a local rpm passed in"""
cmd = ['/bin/rpm', '-qp' ,'--qf',
2012-08-24 19:51:07 +02:00
'%{name}-%{version}-%{release}.%{arch}\n', path ]
2012-08-14 22:23:40 +02:00
rc, out, err = run(cmd)
if rc != 0:
return None
nvra = out.split('\n')[0]
return nvra
2012-08-02 07:47:48 +02:00
def pkg_to_dict(pkgstr):
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
if pkgstr.strip():
n,e,v,r,a,repo = pkgstr.split('|')
else:
return {'error_parsing': pkgstr}
2012-08-07 02:07:02 +02:00
2012-03-10 00:33:58 +01:00
d = {
2012-08-02 07:47:48 +02:00
'name':n,
'arch':a,
'epoch':e,
'release':r,
'version':v,
'repo':repo,
'nevra': '%s:%s-%s-%s.%s' % (e,n,v,r,a)
2012-08-12 00:13:29 +02:00
}
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
if repo == 'installed':
2012-03-10 04:06:44 +01:00
d['yumstate'] = 'installed'
2012-03-10 00:33:58 +01:00
else:
2012-03-10 04:06:44 +01:00
d['yumstate'] = 'available'
2012-08-02 07:47:48 +02:00
return d
def repolist(repoq, qf="%{repoid}"):
2012-08-15 01:33:11 +02:00
2012-08-09 16:12:22 +02:00
cmd = repoq + ["--qf", qf, "-a"]
2012-08-02 07:47:48 +02:00
rc,out,err = run(cmd)
ret = []
if rc == 0:
ret = set([ p for p in out.split('\n') if p.strip() ])
return ret
2012-08-07 02:07:02 +02:00
2012-08-14 00:13:50 +02:00
def list_stuff(module, conf_file, stuff):
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
2012-08-10 03:29:59 +02:00
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
2012-08-02 07:47:48 +02:00
if conf_file and os.path.exists(conf_file):
2012-08-09 16:12:22 +02:00
repoq += ['-c', conf_file]
2012-03-10 00:33:58 +01:00
if stuff == 'installed':
2012-08-14 00:13:50 +02:00
return [ pkg_to_dict(p) for p in is_installed(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
2012-03-10 00:33:58 +01:00
elif stuff == 'updates':
2012-08-14 00:13:50 +02:00
return [ pkg_to_dict(p) for p in is_update(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
2012-03-10 00:33:58 +01:00
elif stuff == 'available':
2012-08-14 00:13:50 +02:00
return [ pkg_to_dict(p) for p in is_available(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
2012-03-10 00:33:58 +01:00
elif stuff == 'repos':
2012-08-02 07:47:48 +02:00
return [ dict(repoid=name, state='enabled') for name in repolist(repoq) if name.strip() ]
2012-03-10 00:33:58 +01:00
else:
2012-08-14 00:13:50 +02:00
return [ pkg_to_dict(p) for p in is_installed(module, repoq, stuff, conf_file, qf=qf) + is_available(module, repoq, stuff, conf_file, qf=qf) if p.strip() ]
2012-03-10 00:33:58 +01:00
2012-08-02 07:47:48 +02:00
def run(command):
2012-08-15 01:33:11 +02:00
2012-03-10 00:33:58 +01:00
try:
2012-08-09 16:12:22 +02:00
cmd = subprocess.Popen(command,
2012-03-10 00:33:58 +01:00
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
except (OSError, IOError), e:
rc = 1
err = str(e)
out = ''
except:
rc = 1
err = traceback.format_exc()
out = ''
if out is None:
2012-08-11 18:35:58 +02:00
out = ''
2012-03-10 00:33:58 +01:00
if err is None:
2012-08-11 18:35:58 +02:00
err = ''
2012-03-10 00:33:58 +01:00
else:
rc = cmd.returncode
2012-08-07 02:07:02 +02:00
2012-03-10 00:33:58 +01:00
return rc, out, err
2012-08-09 16:12:22 +02:00
2012-10-24 06:21:24 +02:00
def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
2012-08-15 01:33:11 +02:00
2012-03-12 21:31:13 +01:00
res = {}
2012-08-02 23:02:37 +02:00
res['results'] = []
2012-08-02 07:47:48 +02:00
res['msg'] = ''
res['rc'] = 0
res['changed'] = False
for spec in items:
pkg = None
# check if pkgspec is installed (if possible for idempotence)
# localpkg
if spec.endswith('.rpm'):
2012-08-07 02:07:02 +02:00
# get the pkg name-v-r.arch
2012-08-24 19:51:07 +02:00
if not os.path.exists(spec):
res['msg'] += "No Package file matching '%s' found on system" % spec
module.fail_json(**res)
2012-08-07 02:07:02 +02:00
nvra = local_nvra(spec)
2012-08-02 07:47:48 +02:00
# look for them in the rpmdb
2012-10-24 06:46:15 +02:00
if is_installed(module, repoq, nvra, conf_file, en_repos=en_repos, dis_repos=dis_repos):
2012-08-02 07:47:48 +02:00
# if they are there, skip it
continue
pkg = spec
#groups :(
elif spec.startswith('@'):
# complete wild ass guess b/c it's a group
pkg = spec
# range requires or file-requires or pkgname :(
2012-03-14 05:38:38 +01:00
else:
2012-08-02 07:47:48 +02:00
# look up what pkgs provide this
2012-10-24 06:46:15 +02:00
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
2012-08-02 07:47:48 +02:00
if not pkglist:
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
2012-08-15 01:29:09 +02:00
module.fail_json(**res)
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
# if any of them are installed
# then nothing to do
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
found = False
for this in pkglist:
2012-10-24 06:46:15 +02:00
if is_installed(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
2012-08-02 07:47:48 +02:00
found = True
2012-08-02 23:02:37 +02:00
res['results'].append('%s providing %s is already installed' % (this, spec))
2012-10-19 22:28:39 +02:00
break
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
if found:
continue
# if not - then pass in the spec as what to install
2012-08-07 02:07:02 +02:00
# we could get here if nothing provides it but that's not
# the error we're catching here
2012-08-02 07:47:48 +02:00
pkg = spec
2012-08-07 02:07:02 +02:00
2012-08-09 16:12:22 +02:00
cmd = yum_basecmd + ['install', pkg]
2012-08-02 07:47:48 +02:00
rc, out, err = run(cmd)
2012-03-12 21:31:13 +01:00
# FIXME - if we did an install - go and check the rpmdb to see if it actually installed
# look for the pkg in rpmdb
# look for the pkg via obsoletes
2012-03-10 00:33:58 +01:00
if rc:
2012-08-02 07:47:48 +02:00
res['changed'] = False
res['rc'] = rc
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
2012-03-10 00:33:58 +01:00
else:
2012-08-02 07:47:48 +02:00
res['changed'] = True
res['rc'] = 0
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
module.exit_json(**res)
2012-08-07 02:07:02 +02:00
2012-03-10 00:33:58 +01:00
2012-10-24 06:21:24 +02:00
def remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
2012-08-15 01:27:02 +02:00
2012-08-02 07:47:48 +02:00
res = {}
2012-08-02 23:02:37 +02:00
res['results'] = []
2012-08-02 07:47:48 +02:00
res['msg'] = ''
res['changed'] = False
res['rc'] = 0
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
for spec in items:
pkg = None
# group remove - hope you like things dying!
if spec.startswith('@'):
pkg = spec
# req or pkgname remove
2012-03-10 00:33:58 +01:00
else:
2012-10-24 06:46:15 +02:00
pkglist = is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
2012-08-02 07:47:48 +02:00
if not pkglist:
2012-10-29 18:48:20 +01:00
found = False
else:
found = True
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
if not found:
2012-08-02 23:02:37 +02:00
res['results'].append('%s is not installed' % spec)
2012-08-02 07:47:48 +02:00
continue
pkg = spec
2012-03-10 00:33:58 +01:00
2012-08-09 16:12:22 +02:00
cmd = yum_basecmd + ["remove", pkg]
2012-08-02 07:47:48 +02:00
rc, out, err = run(cmd)
2012-08-07 02:07:02 +02:00
2012-03-12 21:31:13 +01:00
# FIXME if we ran the remove - check to make sure it actually removed :(
2012-08-02 07:47:48 +02:00
# look for the pkg in the rpmdb - this is notoriously hard for groups :(
if rc != 0:
res['changed'] = False
res['failed'] = True
res['rc'] = rc
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
2012-03-10 00:33:58 +01:00
else:
2012-08-02 07:47:48 +02:00
res['changed'] = True
res['rc'] = 0
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
module.exit_json(**res)
2012-03-12 21:31:13 +01:00
2012-10-24 06:21:24 +02:00
def latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
res = {}
2012-08-02 23:02:37 +02:00
res['results'] = []
2012-08-02 07:47:48 +02:00
res['msg'] = ''
res['changed'] = False
res['rc'] = 0
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
for spec in items:
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
pkg = None
2012-08-08 16:43:45 +02:00
basecmd = 'update'
2012-08-02 07:47:48 +02:00
# groups, again
if spec.startswith('@'):
pkg = spec
# dep/pkgname - find it
2012-03-27 19:58:49 +02:00
else:
2012-10-24 06:46:15 +02:00
if is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos):
2012-08-14 00:13:50 +02:00
basecmd = 'update'
else:
basecmd = 'install'
2012-10-24 06:46:15 +02:00
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
2012-08-02 07:47:48 +02:00
if not pkglist:
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
res['failed']=True
module.exit_json(**res)
2012-08-14 00:13:50 +02:00
nothing_to_do = True
2012-08-02 07:47:48 +02:00
for this in pkglist:
2012-10-24 06:46:15 +02:00
if basecmd == 'install' and is_available(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
2012-08-14 00:13:50 +02:00
nothing_to_do = False
break
2012-10-24 06:46:15 +02:00
if basecmd == 'update' and is_update(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=en_repos):
2012-08-14 00:13:50 +02:00
nothing_to_do = False
break
2012-08-02 07:47:48 +02:00
if nothing_to_do:
2012-08-02 23:02:37 +02:00
res['results'].append("All packages providing %s are up to date" % spec)
2012-08-02 07:47:48 +02:00
continue
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
pkg = spec
2012-08-09 16:12:22 +02:00
cmd = yum_basecmd + [basecmd, pkg]
2012-08-02 07:47:48 +02:00
rc, out, err = run(cmd)
2012-03-27 20:15:48 +02:00
2012-03-12 21:31:13 +01:00
# FIXME if it is - update it and check to see if it applied
# check to see if there is no longer an update available for the pkgspec
if rc:
changed = False
failed = True
else:
changed = True
failed = False
2012-08-02 07:47:48 +02:00
if rc:
res['changed'] = False
res['failed'] = True
res['rc'] = rc
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
else:
res['changed'] = True
res['rc'] = 0
2012-08-02 23:02:37 +02:00
res['results'].append(out)
2012-08-02 07:47:48 +02:00
res['msg'] += err
2012-08-07 02:07:02 +02:00
2012-08-02 07:47:48 +02:00
module.exit_json(**res)
2012-03-12 21:31:13 +01:00
2012-10-24 06:21:24 +02:00
def ensure(module, state, pkgspec, conf_file, enablerepo, disablerepo):
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
# take multiple args comma separated
2012-08-09 16:12:22 +02:00
items = pkgspec.split(',')
2012-07-26 02:14:15 +02:00
2012-08-09 16:12:22 +02:00
yum_basecmd = [yumbin, '-d', '1', '-y']
2012-08-15 01:33:11 +02:00
2012-10-24 06:21:24 +02:00
2012-08-14 00:13:50 +02:00
if not repoquery:
repoq = None
else:
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
2012-08-15 01:33:11 +02:00
2012-08-02 07:47:48 +02:00
if conf_file and os.path.exists(conf_file):
2012-08-09 16:12:22 +02:00
yum_basecmd += ['-c', conf_file]
2012-08-14 00:13:50 +02:00
if repoq:
repoq += ['-c', conf_file]
2012-10-24 06:21:24 +02:00
dis_repos =[]
en_repos = []
if disablerepo:
dis_repos = disablerepo.split(',')
if enablerepo:
en_repos = enablerepo.split(',')
for repoid in en_repos:
r_cmd = ['--enablerepo', repoid]
yum_basecmd.extend(r_cmd)
if repoq:
repoq.extend(r_cmd)
for repoid in dis_repos:
r_cmd = ['--disablerepo', repoid]
yum_basecmd.extend(r_cmd)
if repoq:
repoq.extend(r_cmd)
2012-08-14 00:13:50 +02:00
if state in ['installed', 'present']:
2012-10-24 06:21:24 +02:00
install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
2012-08-14 00:13:50 +02:00
elif state in ['removed', 'absent']:
2012-10-24 06:21:24 +02:00
remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
2012-08-14 00:13:50 +02:00
elif state == 'latest':
2012-10-24 06:21:24 +02:00
latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
2012-08-09 16:12:22 +02:00
2012-08-07 02:07:02 +02:00
# should be caught by AnsibleModule argument_spec
2012-08-02 07:47:48 +02:00
return dict(changed=False, failed=True, results='', errors='unexpected state')
2012-08-07 02:07:02 +02:00
2012-03-10 00:33:58 +01:00
def main():
2012-08-15 01:33:11 +02:00
2012-08-12 00:16:10 +02:00
# state=installed name=pkgspec
# state=removed name=pkgspec
# state=latest name=pkgspec
2012-03-10 00:33:58 +01:00
#
2012-04-01 08:35:19 +02:00
# informational commands:
# list=installed
# list=updates
# list=available
# list=repos
# list=pkgspec
2012-08-07 02:07:02 +02:00
2012-07-25 16:15:41 +02:00
module = AnsibleModule(
2012-07-26 02:14:15 +02:00
argument_spec = dict(
2012-08-15 01:27:02 +02:00
name=dict(aliases=['pkg']),
2012-07-26 02:14:15 +02:00
# removed==absent, installed==present, these are accepted as aliases
state=dict(default='installed', choices=['absent','present','installed','removed','latest']),
2012-10-24 06:21:24 +02:00
enablerepo=dict(),
disablerepo=dict(),
2012-08-09 19:22:51 +02:00
list=dict(),
2012-08-02 07:47:48 +02:00
conf_file=dict(default=None),
2012-08-12 00:13:29 +02:00
),
2012-08-12 00:16:10 +02:00
required_one_of = [['name','list']],
mutually_exclusive = [['name','list']]
2012-07-25 16:15:41 +02:00
)
2012-03-10 08:19:57 +01:00
2012-07-25 16:15:41 +02:00
params = module.params
2012-03-10 00:33:58 +01:00
2012-08-02 02:33:13 +02:00
if params['list']:
2012-08-14 00:13:50 +02:00
if not repoquery:
module.fail_json(msg="repoquery is required to use list= with this module. Please install the yum-utils package.")
results = dict(results=list_stuff(module, params['conf_file'], params['list']))
2012-07-26 02:14:15 +02:00
module.exit_json(**results)
2012-08-07 02:07:02 +02:00
2012-04-12 02:47:38 +02:00
else:
2012-08-12 00:16:10 +02:00
pkg = params['name']
2012-08-12 00:13:29 +02:00
state = params['state']
2012-10-24 06:21:24 +02:00
enablerepo = params.get('enablerepo', '')
disablerepo = params.get('disablerepo', '')
res = ensure(module, state, pkg, params['conf_file'], enablerepo, disablerepo)
2012-08-12 00:13:29 +02:00
module.fail_json(msg="we should never get here unless this all failed", **res)
2012-03-14 05:38:38 +01:00
2012-07-25 16:15:41 +02:00
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()