From c119d54e4ac10a2f805a88c2f226ce8b4ed70b4e Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 20 Feb 2018 08:40:45 -0500 Subject: [PATCH] now get_url defaults to module temp dir (#36218) * now get_url and other modules default to module temp dir also fixed 'bare' exception * allow modules to work with older versions * updated docs per feedback --- CHANGELOG.md | 1 + lib/ansible/modules/files/assemble.py | 6 +++--- lib/ansible/modules/files/blockinfile.py | 2 +- lib/ansible/modules/files/replace.py | 2 +- lib/ansible/modules/net_tools/basics/get_url.py | 14 +++++++------- lib/ansible/modules/net_tools/basics/uri.py | 2 +- lib/ansible/modules/packaging/os/yum.py | 2 +- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30268dbeda..80d114f022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ See [Porting Guide](https://docs.ansible.com/ansible/devel/porting_guides/portin * The ANSIBLE_REMOTE_TMP environment variable has been added to supplement (and override) ANSIBLE_REMOTE_TEMP. This matches with the spelling of the config value. ANSIBLE_REMOTE_TEMP will be deprecated in the future. +* A few modules were updated to put temporary files in the existing temp dir already created for the module itself, including get_url, assemble, uri and yum. #### Removed Modules (previously deprecated): * accelerate. diff --git a/lib/ansible/modules/files/assemble.py b/lib/ansible/modules/files/assemble.py index 4969675381..2d751b500f 100644 --- a/lib/ansible/modules/files/assemble.py +++ b/lib/ansible/modules/files/assemble.py @@ -107,9 +107,9 @@ from ansible.module_utils.six import b from ansible.module_utils._text import to_native -def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False): +def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False, tmpdir=None): ''' assemble a file from a directory of fragments ''' - tmpfd, temp_path = tempfile.mkstemp() + tmpfd, temp_path = tempfile.mkstemp(dir=tmpdir) tmp = os.fdopen(tmpfd, 'wb') delimit_me = False add_newline = False @@ -204,7 +204,7 @@ def main(): if validate and "%s" not in validate: module.fail_json(msg="validate must contain %%s: %s" % validate) - path = assemble_from_fragments(src, delimiter, compiled_regexp, ignore_hidden) + path = assemble_from_fragments(src, delimiter, compiled_regexp, ignore_hidden, getattr(module, 'tmpdir', None)) path_hash = module.sha1(path) result['checksum'] = path_hash diff --git a/lib/ansible/modules/files/blockinfile.py b/lib/ansible/modules/files/blockinfile.py index 0b69abc63e..16b2b741e4 100644 --- a/lib/ansible/modules/files/blockinfile.py +++ b/lib/ansible/modules/files/blockinfile.py @@ -159,7 +159,7 @@ from ansible.module_utils._text import to_bytes def write_changes(module, contents, path): - tmpfd, tmpfile = tempfile.mkstemp() + tmpfd, tmpfile = tempfile.mkstemp(dir=getattr(module, 'tmpdir', None)) f = os.fdopen(tmpfd, 'wb') f.write(contents) f.close() diff --git a/lib/ansible/modules/files/replace.py b/lib/ansible/modules/files/replace.py index 1a652b6b45..c5f6026a3f 100644 --- a/lib/ansible/modules/files/replace.py +++ b/lib/ansible/modules/files/replace.py @@ -154,7 +154,7 @@ from ansible.module_utils.basic import AnsibleModule def write_changes(module, contents, path): - tmpfd, tmpfile = tempfile.mkstemp() + tmpfd, tmpfile = tempfile.mkstemp(dir=getattr(module, 'tmpdir', None)) f = os.fdopen(tmpfd, 'wb') f.write(contents) f.close() diff --git a/lib/ansible/modules/net_tools/basics/get_url.py b/lib/ansible/modules/net_tools/basics/get_url.py index d589f238d8..46692d7691 100644 --- a/lib/ansible/modules/net_tools/basics/get_url.py +++ b/lib/ansible/modules/net_tools/basics/get_url.py @@ -49,7 +49,8 @@ options: tmp_dest: description: - Absolute path of where temporary file is downloaded to. - - Defaults to C(TMPDIR), C(TEMP) or C(TMP) env variables or a platform specific value. + - When run on Ansible 2.5 or greater, path defaults to ansible's remote_tmp setting + - When run on Ansible prior to 2.5, it defaults to C(TMPDIR), C(TEMP) or C(TMP) env variables or a platform specific value. - U(https://docs.python.org/2/library/tempfile.html#tempfile.tempdir) version_added: '2.1' force: @@ -338,18 +339,17 @@ def url_get(module, url, dest, use_proxy, last_mod_time, force, timeout=10, head module.fail_json(msg="%s is a file but should be a directory." % tmp_dest) else: module.fail_json(msg="%s directory does not exist." % tmp_dest) - - fd, tempname = tempfile.mkstemp(dir=tmp_dest) else: - fd, tempname = tempfile.mkstemp() + tmp_dest = getattr(module, 'tmpdir', None) + + fd, tempname = tempfile.mkstemp(dir=tmp_dest) f = os.fdopen(fd, 'wb') try: shutil.copyfileobj(rsp, f) except Exception as e: os.remove(tempname) - module.fail_json(msg="failed to create temporary content file: %s" % to_native(e), - exception=traceback.format_exc()) + module.fail_json(msg="failed to create temporary content file: %s" % to_native(e), exception=traceback.format_exc()) f.close() rsp.close() return tempname, info @@ -413,7 +413,7 @@ def main(): if module.params['headers']: try: headers = dict(item.split(':', 1) for item in module.params['headers'].split(',')) - except: + except Exception: module.fail_json(msg="The header parameter requires a key:value,key:value syntax to be properly parsed.") else: headers = None diff --git a/lib/ansible/modules/net_tools/basics/uri.py b/lib/ansible/modules/net_tools/basics/uri.py index 199f2aa025..bf5d602608 100644 --- a/lib/ansible/modules/net_tools/basics/uri.py +++ b/lib/ansible/modules/net_tools/basics/uri.py @@ -241,7 +241,7 @@ JSON_CANDIDATES = ('text', 'json', 'javascript') def write_file(module, url, dest, content): # create a tempfile with some test content - fd, tmpsrc = tempfile.mkstemp() + fd, tmpsrc = tempfile.mkstemp(dir=getattr(module, 'tmpdir', None)) f = open(tmpsrc, 'wb') try: f.write(content) diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index adad0ffe63..87cb485825 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -316,7 +316,7 @@ def ensure_yum_utils(module): def fetch_rpm_from_url(spec, module=None): # download package so that we can query it package_name, _ = os.path.splitext(str(spec.rsplit('/', 1)[1])) - package_file = tempfile.NamedTemporaryFile(prefix=package_name, suffix='.rpm', delete=False) + package_file = tempfile.NamedTemporaryFile(dir=getattr(module, 'tmpdir', None), prefix=package_name, suffix='.rpm', delete=False) module.add_cleanup_file(package_file.name) try: rsp, info = fetch_url(module, spec)