Repository: ansible/ansible

Issue:
get_url incorrectly returning a 302 error
##### Issue Type:
- Bug Report
##### Ansible Version:

```
ansible 2.1.0 (devel 4cfd042839) last updated 2016/03/03 11:14:21 (GMT -700)
  lib/ansible/modules/core: (detached HEAD 0bbb7ba38d) last updated 2016/03/01 11:10:24 (GMT -700)
  lib/ansible/modules/extras: (detached HEAD 39e4040685) last updated 2016/03/01 11:10:24 (GMT -700)
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```
##### Environment:

running from OS X El Capitan
controlling CentOS
##### Summary:

Getting a 302 error trying to download rpm. The rpm is there.
##### Steps To Reproduce:

<!---
For bugs, please show exactly how to reproduce the problem. For new
features, show how the feature would be used.
-->

```
vars/rmpforge.yml:

---
rpmforge_version: 0.5.3-1.el6.rf.x86_64

install_rpmforge.yml:

---
- include_vars: vars/rpmforge.yml

- name: get rpmforge
  get_url:
    url: 'http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-{{ rpmforge_version }}.rpm'
    dest: /usr/src
    force: no


```
##### Expected Results:

If the file exists locally, I expect it to not do much at all
If the file doesn't exist locally, I expect it to get it
##### Actual Results:

Get a 302 error in either case

```
TASK [get rpmforge] ************************************************************
fatal: [trac]: FAILED! => {"changed": false, "dest": "/usr/src", "failed": true, "gid": 0, "group": "root", "mode": "0755", "msg": "Request failed", "owner": "root", "response": "HTTP Error 302: Found", "secontext": "system_u:object_r:usr_t:s0", "size": 4096, "state": "directory", "status_code": 302, "uid": 0, "url": "http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm"}
    to retry, use: --limit @install_utilities.retry

```



Discussion:
It seems to be a redirect from http to https causing the problem.

I hit this same bug.  Using `git bisect` I tracked down the problematic commit, which is prior to the version indicated by @jgrigonis. 

```
6ff0b079b4688df943af2da71c7533a0bf9ba0cb is the first bad commit
commit 6ff0b079b4688df943af2da71c7533a0bf9ba0cb
Author: Matt Martz <matt@sivel.net>
Date:   Wed Mar 2 16:30:16 2016 -0600

    Support SSL validation with redirect control for python versions without ssl context

:040000 040000 d595c903efd6b41b105836c3cd65e4bb8fae0492 800093d3266c5adb3620c8ada479240149c43539 M  lib
```

FYI @sivel 

Thanks for the report.  I'll look into this soon.


Resolving patch:
diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py
--- a/lib/ansible/module_utils/urls.py
+++ b/lib/ansible/module_utils/urls.py
@@ -676,7 +676,7 @@ def maybe_add_ssl_handler(url, validate_certs):
 def open_url(url, data=None, headers=None, method=None, use_proxy=True,
         force=False, last_mod_time=None, timeout=10, validate_certs=True,
         url_username=None, url_password=None, http_agent=None,
-        force_basic_auth=False, follow_redirects=False):
+        force_basic_auth=False, follow_redirects='urllib2'):
     '''
     Fetches a file from an HTTP/FTP server using urllib2
     '''
@@ -809,8 +809,7 @@ def url_argument_spec():
     )
 
 def fetch_url(module, url, data=None, headers=None, method=None,
-              use_proxy=True, force=False, last_mod_time=None, timeout=10,
-              follow_redirects=False):
+              use_proxy=True, force=False, last_mod_time=None, timeout=10):
     '''
     Fetches a file from an HTTP/FTP server using urllib2.  Requires the module environment
     '''
@@ -828,8 +827,7 @@ def fetch_url(module, url, data=None, headers=None, method=None,
     http_agent = module.params.get('http_agent', None)
     force_basic_auth = module.params.get('force_basic_auth', '')
 
-    if not follow_redirects:
-        follow_redirects = module.params.get('follow_redirects', False)
+    follow_redirects = module.params.get('follow_redirects', 'urllib2')
 
     r = None
     info = dict(url=url)


Test patch:
diff --git a/test/integration/roles/test_get_url/tasks/main.yml b/test/integration/roles/test_get_url/tasks/main.yml
--- a/test/integration/roles/test_get_url/tasks/main.yml
+++ b/test/integration/roles/test_get_url/tasks/main.yml
@@ -145,3 +145,8 @@
       - 'get_url_result["failed"]'
   when: "{{ not python_has_ssl_context }}"
 # End hacky SNI test section
+
+- name: Test get_url with redirect
+  get_url:
+    url: 'http://httpbin.org/redirect/6'
+    dest: "{{ output_dir }}/redirect.json"