Skip to content

Commit

Permalink
When force=yes, get_url should always download the specified file
Browse files Browse the repository at this point in the history
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 ansible#5104
  • Loading branch information
jimi-c committed Feb 5, 2014
1 parent f50f29f commit 77d5a18
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions library/network/get_url
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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', ''))

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 77d5a18

Please sign in to comment.