From 77d5a1839250956ba6b3c2b69946d1867773de10 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 5 Feb 2014 09:45:24 -0600 Subject: [PATCH] When force=yes, get_url should always download the specified file This is accomplished by not setting the If-Modified-Since header, and setting "cache-control: no-cache" instead. Note that if the file content has not changed, the module will still report that changed=false, as the md5's of the tmp file and existing file are compared before swapping Fixes #5104 --- library/network/get_url | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/library/network/get_url b/library/network/get_url index 740eb631b1..28ee72a51f 100644 --- a/library/network/get_url +++ b/library/network/get_url @@ -130,7 +130,7 @@ def url_filename(url): return 'index.html' return fn -def url_do_get(module, url, dest, use_proxy, last_mod_time): +def url_do_get(module, url, dest, use_proxy, last_mod_time, force): """ Get url and return request and info Credits: http://stackoverflow.com/questions/7006574/how-to-download-file-from-ftp @@ -176,9 +176,11 @@ def url_do_get(module, url, dest, use_proxy, last_mod_time): request = urllib2.Request(url) request.add_header('User-agent', USERAGENT) - if last_mod_time: + if last_mod_time and not force: tstamp = last_mod_time.strftime('%a, %d %b %Y %H:%M:%S +0000') request.add_header('If-Modified-Since', tstamp) + else: + request.add_header('cache-control', 'no-cache') try: r = urllib2.urlopen(request) @@ -194,16 +196,15 @@ def url_do_get(module, url, dest, use_proxy, last_mod_time): return r, info -def url_get(module, url, dest, use_proxy, last_mod_time): +def url_get(module, url, dest, use_proxy, last_mod_time, force): """ Download data from the url and store in a temporary file. Return (tempfile, info about the request) """ - req, info = url_do_get(module, url, dest, use_proxy, last_mod_time) + req, info = url_do_get(module, url, dest, use_proxy, last_mod_time, force) - # TODO: should really handle 304, but how? src file could exist (and be newer) but empty if info['status'] == 304: module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', '')) @@ -283,7 +284,7 @@ def main(): last_mod_time = datetime.datetime.utcfromtimestamp(mtime) # download to tmpsrc - tmpsrc, info = url_get(module, url, dest, use_proxy, last_mod_time) + tmpsrc, info = url_get(module, url, dest, use_proxy, last_mod_time, force) # Now the request has completed, we can finally generate the final # destination file name from the info dict.