From 17ebf51bd48c4680b0117ccfaa21e93e3b70af17 Mon Sep 17 00:00:00 2001 From: jctanner Date: Tue, 21 Jun 2016 20:59:17 -0400 Subject: [PATCH] Add local and url support to yum state=latest (#4003) Fixes #3807 --- lib/ansible/modules/packaging/os/yum.py | 43 +++++++++++++++++-------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index f8a9e1e7093..0e3436e38f6 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -219,6 +219,25 @@ def ensure_yum_utils(module): return repoquerybin +def fetch_rpm_from_url(spec, module=None): + # download package so that we can query it + tempdir = tempfile.mkdtemp() + package = os.path.join(tempdir, str(spec.rsplit('/', 1)[1])) + try: + rsp, info = fetch_url(module, spec) + f = open(package, 'w') + data = rsp.read(BUFSIZE) + while data: + f.write(data) + data = rsp.read(BUFSIZE) + f.close() + except Exception: + e = get_exception() + shutil.rmtree(tempdir) + if module: + module.fail_json(msg="Failure downloading %s, %s" % (spec, e)) + return package + def po_to_nevra(po): if hasattr(po, 'ui_nevra'): @@ -384,6 +403,13 @@ def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=None, if dis_repos is None: dis_repos = [] + if req_spec.endswith('.rpm') and '://' not in req_spec: + return req_spec + + elif '://' in req_spec: + local_path = fetch_rpm_from_url(req_spec, module=module) + return local_path + if not repoq: pkgs = [] @@ -574,6 +600,8 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos): module.fail_json(**res) pkg_name = local_name(module, spec) + pkg_nevra = local_nvra(module, spec) + # look for them in the rpmdb if is_installed(module, repoq, pkg_name, conf_file, en_repos=en_repos, dis_repos=dis_repos): # if they are there, skip it @@ -583,20 +611,7 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos): # URL elif '://' in spec: # download package so that we can check if it's already installed - package = os.path.join(tempdir, str(spec.rsplit('/', 1)[1])) - try: - rsp, info = fetch_url(module, spec) - f = open(package, 'w') - data = rsp.read(BUFSIZE) - while data: - f.write(data) - data = rsp.read(BUFSIZE) - f.close() - except Exception: - e = get_exception() - shutil.rmtree(tempdir) - module.fail_json(msg="Failure downloading %s, %s" % (spec, e)) - + package = fetch_rpm_from_url(spec, module=module) pkg_name = local_name(module, package) if is_installed(module, repoq, pkg_name, conf_file, en_repos=en_repos, dis_repos=dis_repos): # if it's there, skip it