From 3270a0adcd642b4042e9a814b91bd01d1425a91a Mon Sep 17 00:00:00 2001 From: Yves Dorfsman Date: Sat, 2 Mar 2013 00:30:28 -0700 Subject: [PATCH 1/3] Added a use_proxy option to get_url. --- library/get_url | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/library/get_url b/library/get_url index f541708338..66f2725822 100644 --- a/library/get_url +++ b/library/get_url @@ -56,6 +56,14 @@ options: choices: [ "yes", "no" ] default: "no" aliases: [ "thirsty" ] + use_proxy: + description: + - By default, if an environment variable C(_proxy) is set on + the target host, request will be sent through the proxy. To override + this behaviour, set C(use_proxy=no). + required: false + default: yes + choices: [yes, no] others: description: - all arguments accepted by the M(file) module also work here @@ -94,7 +102,7 @@ def url_filename(url): return 'index.html' return fn -def url_do_get(module, url, dest): +def url_do_get(module, url, dest, use_proxy): """ Get url and return request and info Credits: http://stackoverflow.com/questions/7006574/how-to-download-file-from-ftp @@ -103,7 +111,10 @@ def url_do_get(module, url, dest): USERAGENT = 'ansible-httpget' info = dict(url=url, dest=dest) r = None + handlers = [] + parsed = urlparse.urlparse(url) + if '@' in parsed[1]: credentials, netloc = parsed[1].split('@', 1) if ':' in credentials: @@ -123,12 +134,17 @@ def url_do_get(module, url, dest): authhandler = urllib2.HTTPBasicAuthHandler(passman) # create the AuthHandler + handlers.append(authhandler) - opener = urllib2.build_opener(authhandler) - urllib2.install_opener(opener) #reconstruct url without credentials url = urlparse.urlunparse(parsed) + if not use_proxy: + proxyhandler = urllib2.ProxyHandler({}) + handlers.append(proxyhandler) + + opener = urllib2.build_opener(*handlers) + urllib2.install_opener(opener) request = urllib2.Request(url) request.add_header('User-agent', USERAGENT) @@ -151,14 +167,14 @@ def url_do_get(module, url, dest): return r, info -def url_get(module, url, dest): +def url_get(module, url, dest, use_proxy): """ Download url and store at dest. If dest is a directory, determine filename from url. Return (tempfile, info about the request) """ - req, info = url_do_get(module, url, dest) + req, info = url_do_get(module, url, dest, use_proxy) # TODO: should really handle 304, but how? src file could exist (and be newer) but empty if info['status'] == 304: @@ -195,7 +211,8 @@ def main(): argument_spec = dict( url = dict(required=True), dest = dict(required=True), - force = dict(default='no', aliases=['thirsty'], type='bool') + force = dict(default='no', aliases=['thirsty'], type='bool'), + use_proxy = dict(default='yes', type='bool') ), add_file_common_args=True ) @@ -203,6 +220,7 @@ def main(): url = module.params['url'] dest = os.path.expanduser(module.params['dest']) force = module.params['force'] + use_proxy = module.params['use_proxy'] if os.path.isdir(dest): dest = os.path.join(dest, url_filename(url)) @@ -212,7 +230,7 @@ def main(): module.exit_json(msg="file already exists", dest=dest, url=url, changed=False) # download to tmpsrc - tmpsrc, info = url_get(module, url, dest) + tmpsrc, info = url_get(module, url, dest, use_proxy) md5sum_src = None md5sum_dest = None From 377351f6fd26fef811150506c52e366e47cad834 Mon Sep 17 00:00:00 2001 From: Yves Dorfsman Date: Sat, 2 Mar 2013 11:42:49 -0700 Subject: [PATCH 2/3] Clarified that proxy can be changed via an variable. --- library/get_url | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/get_url b/library/get_url index 66f2725822..8c937c794e 100644 --- a/library/get_url +++ b/library/get_url @@ -31,6 +31,12 @@ short_description: Downloads files from HTTP, HTTPS, or FTP to node description: - Downloads files from HTTP, HTTPS, or FTP to the remote server. The remote server I(must) have direct access to the remote resource. + - By default, if an environment variable C(_proxy) is set on + the target host, requests will be sent through that proxy. This + behaviour can be overriden by setting a variable for this task + (see `setting the environment + `_), + or by using the use_proxy option. version_added: "0.6" options: url: @@ -58,9 +64,8 @@ options: aliases: [ "thirsty" ] use_proxy: description: - - By default, if an environment variable C(_proxy) is set on - the target host, request will be sent through the proxy. To override - this behaviour, set C(use_proxy=no). + - if C(yes), it will no use a proxy, even if one is defined by + in an environment variable on the target hosts. required: false default: yes choices: [yes, no] From c9a201eb436fb4ab3d7d1fef9cbbd06395bbd991 Mon Sep 17 00:00:00 2001 From: Yves Dorfsman Date: Sat, 2 Mar 2013 12:12:25 -0700 Subject: [PATCH 3/3] fixed typos --- library/get_url | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/get_url b/library/get_url index 8c937c794e..e7f8a1c102 100644 --- a/library/get_url +++ b/library/get_url @@ -64,7 +64,7 @@ options: aliases: [ "thirsty" ] use_proxy: description: - - if C(yes), it will no use a proxy, even if one is defined by + - if C(no), it will not use a proxy, even if one is defined by in an environment variable on the target hosts. required: false default: yes