mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
701 lines
22 KiB
Plaintext
701 lines
22 KiB
Plaintext
13 years ago
|
#!/usr/bin/python -tt
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
|
|
||
13 years ago
|
# (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/>.
|
||
|
#
|
||
|
|
||
12 years ago
|
|
||
13 years ago
|
import traceback
|
||
12 years ago
|
import os
|
||
12 years ago
|
import yum
|
||
|
|
||
12 years ago
|
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"
|
||
12 years ago
|
enablerepo:
|
||
|
description:
|
||
12 years ago
|
- Repoid of repositories to enable for the install/update operation.
|
||
|
These repos will not persist beyond the transaction
|
||
|
multiple repos separated with a ','
|
||
12 years ago
|
required: false
|
||
12 years ago
|
version_added: "0.9"
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
|
||
12 years ago
|
disablerepo:
|
||
|
description:
|
||
12 years ago
|
- I(repoid) of repositories to disable for the install/update operation
|
||
12 years ago
|
These repos will not persist beyond the transaction
|
||
|
Multiple repos separated with a ','
|
||
12 years ago
|
required: false
|
||
12 years ago
|
version_added: "0.9"
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
|
||
|
conf_file:
|
||
|
description:
|
||
|
- The remote yum configuration file to use for the transaction.
|
||
|
required: false
|
||
|
version_added: "0.6"
|
||
|
default: null
|
||
|
aliases: []
|
||
|
|
||
|
disable_gpg_check:
|
||
|
description:
|
||
|
- Whether to disable the GPG checking of signatures of packages being
|
||
|
installed. Has an effect only if state is I(present) or I(latest).
|
||
|
required: false
|
||
|
version_added: "1.2"
|
||
|
default: "no"
|
||
|
choices: ["yes", "no"]
|
||
|
aliases: []
|
||
|
|
||
12 years ago
|
examples:
|
||
12 years ago
|
- code: yum name=httpd state=latest
|
||
|
- code: yum name=httpd state=removed
|
||
|
- code: yum name=httpd enablerepo=testing state=installed
|
||
12 years ago
|
notes: []
|
||
|
# informational: requirements for nodes
|
||
|
requirements: [ yum, rpm ]
|
||
|
author: Seth Vidal
|
||
|
'''
|
||
|
|
||
12 years ago
|
def_qf = "%{name}-%{version}-%{release}.%{arch}"
|
||
12 years ago
|
|
||
12 years ago
|
repoquery='/usr/bin/repoquery'
|
||
12 years ago
|
if not os.path.exists(repoquery):
|
||
|
repoquery = None
|
||
12 years ago
|
|
||
12 years ago
|
yumbin='/usr/bin/yum'
|
||
12 years ago
|
|
||
|
def yum_base(conf_file=None, cachedir=False):
|
||
12 years ago
|
|
||
12 years ago
|
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
|
||
12 years ago
|
|
||
12 years ago
|
return my
|
||
|
|
||
|
def po_to_nevra(po):
|
||
12 years ago
|
|
||
12 years ago
|
if hasattr(po, 'ui_nevra'):
|
||
|
return po.ui_nevra
|
||
|
else:
|
||
|
return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
|
||
|
|
||
12 years ago
|
def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
|
||
12 years ago
|
|
||
12 years ago
|
if not repoq:
|
||
12 years ago
|
|
||
12 years ago
|
pkgs = []
|
||
|
try:
|
||
|
my = yum_base(conf_file)
|
||
12 years ago
|
for rid in en_repos:
|
||
|
my.repos.enableRepo(rid)
|
||
|
for rid in dis_repos:
|
||
|
my.repos.disableRepo(rid)
|
||
|
|
||
12 years ago
|
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 ]
|
||
12 years ago
|
|
||
12 years ago
|
else:
|
||
12 years ago
|
|
||
12 years ago
|
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, pkgspec]
|
||
12 years ago
|
rc,out,err = module.run_command(cmd)
|
||
12 years ago
|
cmd = repoq + ["--disablerepo=*", "--pkgnarrow=installed", "--qf", qf, "--whatprovides", pkgspec]
|
||
12 years ago
|
rc2,out2,err2 = module.run_command(cmd)
|
||
12 years ago
|
if rc == 0 and rc2 == 0:
|
||
|
out += out2
|
||
|
return [ p for p in out.split('\n') if p.strip() ]
|
||
|
else:
|
||
12 years ago
|
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
|
||
12 years ago
|
|
||
12 years ago
|
return []
|
||
|
|
||
12 years ago
|
def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
|
||
12 years ago
|
|
||
12 years ago
|
if not repoq:
|
||
12 years ago
|
|
||
12 years ago
|
pkgs = []
|
||
|
try:
|
||
|
my = yum_base(conf_file)
|
||
12 years ago
|
for rid in en_repos:
|
||
|
my.repos.enableRepo(rid)
|
||
|
for rid in dis_repos:
|
||
|
my.repos.disableRepo(rid)
|
||
|
|
||
12 years ago
|
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 ]
|
||
12 years ago
|
|
||
12 years ago
|
else:
|
||
12 years ago
|
myrepoq = list(repoq)
|
||
|
|
||
12 years ago
|
for repoid in en_repos:
|
||
|
r_cmd = ['--enablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
|
for repoid in dis_repos:
|
||
|
r_cmd = ['--disablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
12 years ago
|
cmd = myrepoq + ["--qf", qf, pkgspec]
|
||
12 years ago
|
rc,out,err = module.run_command(cmd)
|
||
12 years ago
|
if rc == 0:
|
||
|
return [ p for p in out.split('\n') if p.strip() ]
|
||
|
else:
|
||
12 years ago
|
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err))
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
return []
|
||
|
|
||
12 years ago
|
def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
|
||
12 years ago
|
|
||
12 years ago
|
if not repoq:
|
||
12 years ago
|
|
||
12 years ago
|
retpkgs = []
|
||
|
pkgs = []
|
||
|
updates = []
|
||
12 years ago
|
|
||
12 years ago
|
try:
|
||
|
my = yum_base(conf_file)
|
||
12 years ago
|
for rid in en_repos:
|
||
|
my.repos.enableRepo(rid)
|
||
|
for rid in dis_repos:
|
||
|
my.repos.disableRepo(rid)
|
||
|
|
||
12 years ago
|
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 ])
|
||
12 years ago
|
|
||
12 years ago
|
else:
|
||
12 years ago
|
myrepoq = list(repoq)
|
||
12 years ago
|
for repoid in en_repos:
|
||
|
r_cmd = ['--enablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
|
for repoid in dis_repos:
|
||
|
r_cmd = ['--disablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
cmd = myrepoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
|
||
12 years ago
|
rc,out,err = module.run_command(cmd)
|
||
12 years ago
|
|
||
|
if rc == 0:
|
||
|
return set([ p for p in out.split('\n') if p.strip() ])
|
||
|
else:
|
||
12 years ago
|
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err))
|
||
12 years ago
|
|
||
12 years ago
|
return []
|
||
|
|
||
12 years ago
|
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
|
||
12 years ago
|
|
||
12 years ago
|
if not repoq:
|
||
12 years ago
|
|
||
12 years ago
|
pkgs = []
|
||
|
try:
|
||
|
my = yum_base(conf_file)
|
||
12 years ago
|
for rid in en_repos:
|
||
|
my.repos.enableRepo(rid)
|
||
|
for rid in dis_repos:
|
||
|
my.repos.disableRepo(rid)
|
||
|
|
||
12 years ago
|
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 ])
|
||
12 years ago
|
|
||
12 years ago
|
else:
|
||
12 years ago
|
myrepoq = list(repoq)
|
||
12 years ago
|
for repoid in en_repos:
|
||
|
r_cmd = ['--enablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
|
for repoid in dis_repos:
|
||
|
r_cmd = ['--disablerepo', repoid]
|
||
12 years ago
|
myrepoq.extend(r_cmd)
|
||
12 years ago
|
|
||
12 years ago
|
cmd = myrepoq + ["--qf", qf, "--whatprovides", req_spec]
|
||
12 years ago
|
rc,out,err = module.run_command(cmd)
|
||
12 years ago
|
cmd = myrepoq + ["--qf", qf, req_spec]
|
||
12 years ago
|
rc2,out2,err2 = module.run_command(cmd)
|
||
12 years ago
|
if rc == 0 and rc2 == 0:
|
||
|
out += out2
|
||
12 years ago
|
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
|
||
12 years ago
|
else:
|
||
12 years ago
|
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err + err2))
|
||
|
|
||
12 years ago
|
return []
|
||
13 years ago
|
|
||
12 years ago
|
def local_nvra(module, path):
|
||
12 years ago
|
"""return nvra of a local rpm passed in"""
|
||
|
|
||
|
cmd = ['/bin/rpm', '-qp' ,'--qf',
|
||
12 years ago
|
'%{name}-%{version}-%{release}.%{arch}\n', path ]
|
||
12 years ago
|
rc, out, err = module.run_command(cmd)
|
||
12 years ago
|
if rc != 0:
|
||
|
return None
|
||
|
nvra = out.split('\n')[0]
|
||
|
return nvra
|
||
|
|
||
12 years ago
|
def pkg_to_dict(pkgstr):
|
||
12 years ago
|
|
||
12 years ago
|
if pkgstr.strip():
|
||
|
n,e,v,r,a,repo = pkgstr.split('|')
|
||
|
else:
|
||
|
return {'error_parsing': pkgstr}
|
||
12 years ago
|
|
||
13 years ago
|
d = {
|
||
12 years ago
|
'name':n,
|
||
|
'arch':a,
|
||
|
'epoch':e,
|
||
|
'release':r,
|
||
|
'version':v,
|
||
|
'repo':repo,
|
||
|
'nevra': '%s:%s-%s-%s.%s' % (e,n,v,r,a)
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if repo == 'installed':
|
||
13 years ago
|
d['yumstate'] = 'installed'
|
||
13 years ago
|
else:
|
||
13 years ago
|
d['yumstate'] = 'available'
|
||
12 years ago
|
|
||
|
return d
|
||
|
|
||
12 years ago
|
def repolist(module, repoq, qf="%{repoid}"):
|
||
12 years ago
|
|
||
12 years ago
|
cmd = repoq + ["--qf", qf, "-a"]
|
||
12 years ago
|
rc,out,err = module.run_command(cmd)
|
||
12 years ago
|
ret = []
|
||
|
if rc == 0:
|
||
|
ret = set([ p for p in out.split('\n') if p.strip() ])
|
||
|
return ret
|
||
12 years ago
|
|
||
12 years ago
|
def list_stuff(module, conf_file, stuff):
|
||
12 years ago
|
|
||
12 years ago
|
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
|
||
12 years ago
|
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
|
||
12 years ago
|
if conf_file and os.path.exists(conf_file):
|
||
12 years ago
|
repoq += ['-c', conf_file]
|
||
13 years ago
|
|
||
|
if stuff == 'installed':
|
||
12 years ago
|
return [ pkg_to_dict(p) for p in is_installed(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
|
||
13 years ago
|
elif stuff == 'updates':
|
||
12 years ago
|
return [ pkg_to_dict(p) for p in is_update(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
|
||
13 years ago
|
elif stuff == 'available':
|
||
12 years ago
|
return [ pkg_to_dict(p) for p in is_available(module, repoq, '-a', conf_file, qf=qf) if p.strip() ]
|
||
13 years ago
|
elif stuff == 'repos':
|
||
12 years ago
|
return [ dict(repoid=name, state='enabled') for name in repolist(module, repoq) if name.strip() ]
|
||
13 years ago
|
else:
|
||
12 years ago
|
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() ]
|
||
13 years ago
|
|
||
12 years ago
|
def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||
12 years ago
|
|
||
13 years ago
|
res = {}
|
||
12 years ago
|
res['results'] = []
|
||
12 years ago
|
res['msg'] = ''
|
||
|
res['rc'] = 0
|
||
|
res['changed'] = False
|
||
|
|
||
|
for spec in items:
|
||
|
pkg = None
|
||
|
|
||
|
# check if pkgspec is installed (if possible for idempotence)
|
||
|
# localpkg
|
||
12 years ago
|
if spec.endswith('.rpm') and '://' not in spec:
|
||
12 years ago
|
# get the pkg name-v-r.arch
|
||
12 years ago
|
if not os.path.exists(spec):
|
||
|
res['msg'] += "No Package file matching '%s' found on system" % spec
|
||
|
module.fail_json(**res)
|
||
|
|
||
12 years ago
|
nvra = local_nvra(module, spec)
|
||
12 years ago
|
# look for them in the rpmdb
|
||
12 years ago
|
if is_installed(module, repoq, nvra, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
12 years ago
|
# if they are there, skip it
|
||
|
continue
|
||
|
pkg = spec
|
||
12 years ago
|
|
||
|
# URL
|
||
|
elif '://' in spec:
|
||
|
pkg = spec
|
||
|
|
||
12 years ago
|
#groups :(
|
||
|
elif spec.startswith('@'):
|
||
|
# complete wild ass guess b/c it's a group
|
||
|
pkg = spec
|
||
|
|
||
|
# range requires or file-requires or pkgname :(
|
||
13 years ago
|
else:
|
||
12 years ago
|
# look up what pkgs provide this
|
||
12 years ago
|
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
|
||
12 years ago
|
if not pkglist:
|
||
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
||
12 years ago
|
module.fail_json(**res)
|
||
12 years ago
|
|
||
12 years ago
|
# if any of them are installed
|
||
|
# then nothing to do
|
||
12 years ago
|
|
||
12 years ago
|
found = False
|
||
|
for this in pkglist:
|
||
12 years ago
|
if is_installed(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
12 years ago
|
found = True
|
||
12 years ago
|
res['results'].append('%s providing %s is already installed' % (this, spec))
|
||
12 years ago
|
break
|
||
12 years ago
|
|
||
12 years ago
|
# if the version of the pkg you have installed is not in ANY repo, but there are
|
||
|
# other versions in the repos (both higher and lower) then the previous checks won't work.
|
||
|
# so we check one more time. This really only works for pkgname - not for file provides or virt provides
|
||
|
# but virt provides should be all caught in what_provides on its own.
|
||
|
# highly irritating
|
||
|
if not found:
|
||
|
if is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
|
found = True
|
||
|
res['results'].append('package providing %s is already installed' % (spec))
|
||
|
|
||
12 years ago
|
if found:
|
||
|
continue
|
||
|
# if not - then pass in the spec as what to install
|
||
12 years ago
|
# we could get here if nothing provides it but that's not
|
||
|
# the error we're catching here
|
||
12 years ago
|
pkg = spec
|
||
12 years ago
|
|
||
12 years ago
|
cmd = yum_basecmd + ['install', pkg]
|
||
12 years ago
|
|
||
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
|
|
||
12 years ago
|
changed = True
|
||
|
|
||
12 years ago
|
rc, out, err = module.run_command(cmd)
|
||
12 years ago
|
|
||
12 years ago
|
if rc != 0 and 'Nothing to do' in err:
|
||
|
# avoid failing in the 'Nothing To Do' case
|
||
|
# this may happen with an URL spec
|
||
|
rc = 0
|
||
|
err = ''
|
||
|
out = '%s: Nothing to do' % spec
|
||
|
changed = False
|
||
|
|
||
12 years ago
|
res['rc'] += rc
|
||
|
res['results'].append(out)
|
||
|
res['msg'] += err
|
||
|
|
||
13 years ago
|
# 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
|
||
12 years ago
|
|
||
|
# accumulate any changes
|
||
|
res['changed'] |= changed
|
||
12 years ago
|
|
||
12 years ago
|
module.exit_json(**res)
|
||
12 years ago
|
|
||
13 years ago
|
|
||
12 years ago
|
def remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||
12 years ago
|
|
||
12 years ago
|
res = {}
|
||
12 years ago
|
res['results'] = []
|
||
12 years ago
|
res['msg'] = ''
|
||
|
res['changed'] = False
|
||
|
res['rc'] = 0
|
||
12 years ago
|
|
||
12 years ago
|
for pkg in items:
|
||
|
is_group = False
|
||
|
# group remove - this is doom on a stick
|
||
|
if pkg.startswith('@'):
|
||
|
is_group = True
|
||
13 years ago
|
else:
|
||
12 years ago
|
if not is_installed(module, repoq, pkg, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
|
res['results'].append('%s is not installed' % pkg)
|
||
12 years ago
|
continue
|
||
13 years ago
|
|
||
12 years ago
|
# run an actual yum transaction
|
||
12 years ago
|
cmd = yum_basecmd + ["remove", pkg]
|
||
12 years ago
|
|
||
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
|
|
||
12 years ago
|
rc, out, err = module.run_command(cmd)
|
||
12 years ago
|
|
||
12 years ago
|
res['rc'] += rc
|
||
|
res['results'].append(out)
|
||
|
res['msg'] += err
|
||
|
|
||
|
# compile the results into one batch. If anything is changed
|
||
|
# then mark changed
|
||
|
# at the end - if we've end up failed then fail out of the rest
|
||
|
# of the process
|
||
|
|
||
12 years ago
|
# at this point we should check to see if the pkg is no longer present
|
||
|
|
||
|
if not is_group: # we can't sensibly check for a group being uninstalled reliably
|
||
|
# look to see if the pkg shows up from is_installed. If it doesn't
|
||
|
if not is_installed(module, repoq, pkg, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
|
res['changed'] = True
|
||
|
else:
|
||
12 years ago
|
module.fail_json(**res)
|
||
12 years ago
|
|
||
12 years ago
|
if rc != 0:
|
||
12 years ago
|
module.fail_json(**res)
|
||
12 years ago
|
|
||
12 years ago
|
module.exit_json(**res)
|
||
13 years ago
|
|
||
12 years ago
|
def latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
|
||
12 years ago
|
|
||
12 years ago
|
res = {}
|
||
12 years ago
|
res['results'] = []
|
||
12 years ago
|
res['msg'] = ''
|
||
|
res['changed'] = False
|
||
|
res['rc'] = 0
|
||
12 years ago
|
|
||
12 years ago
|
for spec in items:
|
||
12 years ago
|
|
||
12 years ago
|
pkg = None
|
||
12 years ago
|
basecmd = 'update'
|
||
12 years ago
|
# groups, again
|
||
|
if spec.startswith('@'):
|
||
|
pkg = spec
|
||
|
# dep/pkgname - find it
|
||
13 years ago
|
else:
|
||
12 years ago
|
if is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
12 years ago
|
basecmd = 'update'
|
||
|
else:
|
||
|
basecmd = 'install'
|
||
|
|
||
12 years ago
|
pkglist = what_provides(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos)
|
||
12 years ago
|
if not pkglist:
|
||
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
||
12 years ago
|
module.fail_json(**res)
|
||
12 years ago
|
|
||
|
nothing_to_do = True
|
||
12 years ago
|
for this in pkglist:
|
||
12 years ago
|
if basecmd == 'install' and is_available(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=dis_repos):
|
||
12 years ago
|
nothing_to_do = False
|
||
|
break
|
||
|
|
||
12 years ago
|
if basecmd == 'update' and is_update(module, repoq, this, conf_file, en_repos=en_repos, dis_repos=en_repos):
|
||
12 years ago
|
nothing_to_do = False
|
||
|
break
|
||
|
|
||
12 years ago
|
if nothing_to_do:
|
||
12 years ago
|
res['results'].append("All packages providing %s are up to date" % spec)
|
||
12 years ago
|
continue
|
||
12 years ago
|
|
||
12 years ago
|
pkg = spec
|
||
|
|
||
12 years ago
|
cmd = yum_basecmd + [basecmd, pkg]
|
||
12 years ago
|
|
||
|
if module.check_mode:
|
||
|
return module.exit_json(changed=True)
|
||
|
|
||
12 years ago
|
rc, out, err = module.run_command(cmd)
|
||
13 years ago
|
|
||
12 years ago
|
res['rc'] += rc
|
||
|
res['results'].append(out)
|
||
|
res['msg'] += err
|
||
|
|
||
13 years ago
|
# 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
|
||
|
|
||
12 years ago
|
if rc:
|
||
|
res['failed'] = True
|
||
|
else:
|
||
|
res['changed'] = True
|
||
12 years ago
|
|
||
12 years ago
|
module.exit_json(**res)
|
||
13 years ago
|
|
||
12 years ago
|
def ensure(module, state, pkgspec, conf_file, enablerepo, disablerepo,
|
||
|
disable_gpg_check):
|
||
12 years ago
|
|
||
12 years ago
|
# take multiple args comma separated
|
||
12 years ago
|
items = pkgspec.split(',')
|
||
13 years ago
|
|
||
12 years ago
|
yum_basecmd = [yumbin, '-d', '1', '-y']
|
||
12 years ago
|
|
||
12 years ago
|
|
||
12 years ago
|
if not repoquery:
|
||
|
repoq = None
|
||
|
else:
|
||
|
repoq = [repoquery, '--show-duplicates', '--plugins', '--quiet', '-q']
|
||
12 years ago
|
|
||
12 years ago
|
if conf_file and os.path.exists(conf_file):
|
||
12 years ago
|
yum_basecmd += ['-c', conf_file]
|
||
12 years ago
|
if repoq:
|
||
|
repoq += ['-c', conf_file]
|
||
|
|
||
12 years ago
|
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)
|
||
|
|
||
|
for repoid in dis_repos:
|
||
|
r_cmd = ['--disablerepo', repoid]
|
||
|
yum_basecmd.extend(r_cmd)
|
||
|
|
||
12 years ago
|
if state in ['installed', 'present', 'latest']:
|
||
|
my = yum_base(conf_file)
|
||
|
try:
|
||
|
for r in dis_repos:
|
||
|
my.repos.disableRepo(r)
|
||
|
|
||
|
for r in en_repos:
|
||
|
try:
|
||
|
my.repos.enableRepo(r)
|
||
|
rid = my.repos.getRepo(r)
|
||
|
a = rid.repoXML.repoid
|
||
|
except yum.Errors.YumBaseError, e:
|
||
|
module.fail_json(msg="Error setting/accessing repo %s: %s" % (r, e))
|
||
|
except yum.Errors.YumBaseError, e:
|
||
|
module.fail_json(msg="Error accessing repos: %s" % e)
|
||
|
|
||
12 years ago
|
if state in ['installed', 'present']:
|
||
12 years ago
|
if disable_gpg_check:
|
||
|
yum_basecmd.append('--nogpgcheck')
|
||
12 years ago
|
install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
|
||
12 years ago
|
elif state in ['removed', 'absent']:
|
||
12 years ago
|
remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
|
||
12 years ago
|
elif state == 'latest':
|
||
12 years ago
|
if disable_gpg_check:
|
||
|
yum_basecmd.append('--nogpgcheck')
|
||
12 years ago
|
latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
|
||
12 years ago
|
|
||
12 years ago
|
# should be caught by AnsibleModule argument_spec
|
||
12 years ago
|
return dict(changed=False, failed=True, results='', errors='unexpected state')
|
||
12 years ago
|
|
||
13 years ago
|
def main():
|
||
12 years ago
|
|
||
12 years ago
|
# state=installed name=pkgspec
|
||
|
# state=removed name=pkgspec
|
||
|
# state=latest name=pkgspec
|
||
13 years ago
|
#
|
||
13 years ago
|
# informational commands:
|
||
|
# list=installed
|
||
|
# list=updates
|
||
|
# list=available
|
||
|
# list=repos
|
||
|
# list=pkgspec
|
||
12 years ago
|
|
||
13 years ago
|
module = AnsibleModule(
|
||
13 years ago
|
argument_spec = dict(
|
||
12 years ago
|
name=dict(aliases=['pkg']),
|
||
13 years ago
|
# removed==absent, installed==present, these are accepted as aliases
|
||
|
state=dict(default='installed', choices=['absent','present','installed','removed','latest']),
|
||
12 years ago
|
enablerepo=dict(),
|
||
|
disablerepo=dict(),
|
||
12 years ago
|
list=dict(),
|
||
12 years ago
|
conf_file=dict(default=None),
|
||
12 years ago
|
disable_gpg_check=dict(required=False, default="no",
|
||
|
choices=BOOLEANS, type='bool'),
|
||
12 years ago
|
),
|
||
12 years ago
|
required_one_of = [['name','list']],
|
||
12 years ago
|
mutually_exclusive = [['name','list']],
|
||
|
supports_check_mode = True
|
||
13 years ago
|
)
|
||
13 years ago
|
|
||
13 years ago
|
params = module.params
|
||
13 years ago
|
|
||
12 years ago
|
if params['list']:
|
||
12 years ago
|
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']))
|
||
13 years ago
|
module.exit_json(**results)
|
||
12 years ago
|
|
||
13 years ago
|
else:
|
||
12 years ago
|
pkg = params['name']
|
||
12 years ago
|
state = params['state']
|
||
12 years ago
|
enablerepo = params.get('enablerepo', '')
|
||
|
disablerepo = params.get('disablerepo', '')
|
||
12 years ago
|
disable_gpg_check = params['disable_gpg_check']
|
||
|
res = ensure(module, state, pkg, params['conf_file'], enablerepo,
|
||
|
disablerepo, disable_gpg_check)
|
||
12 years ago
|
module.fail_json(msg="we should never get here unless this all failed", **res)
|
||
13 years ago
|
|
||
13 years ago
|
# this is magic, see lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
main()
|
||
|
|