Repository: ansible/ansible

Issue:
Cannot download deb package via apt: 'NoneType' object has no attribute 'read'
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
`apt`

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 21 2016, 07:16:46) [GCC 6.2.1 20160830]
```

##### CONFIGURATION
```
couchdb_deb: couchdb_2.0.0-8.deb
```

##### OS / ENVIRONMENT
Arch Linux (Client) / Ubuntu 16.04 (Server)

##### SUMMARY
Cannot download deb and install via apt module

##### STEPS TO REPRODUCE
Run the following task

```yaml
- name: Install CouchDB
  become: yes 
  become_user: root
  apt:
    deb: https://onee3.org/deb/{{ couchdb_deb }}
```

##### EXPECTED RESULTS
Ansible could install deb after downloading it.

##### ACTUAL RESULTS
It failed to download.

<!--- Paste verbatim command output between quotes below -->
```
<x.x.x.x> (0, '\r\n{"msg": "Failure downloading https://onee3.org/deb/couchdb_2.0.0-8.deb, \'NoneType\' object has no attribute \'read\'", "failed": true, "invocation": {"module_args": {"dpkg_options": "force-confdef,force-confold", "upgrade": null, "force": false, "package": null, "purge": false, "allow_unauthenticated": false, "state": "present", "autoremove": null, "update_cache": null, "deb": "https://onee3.org/deb/couchdb_2.0.0-8.deb", "only_upgrade": false, "cache_valid_time": 0, "default_release": null, "install_recommends": null}}}\r\n', 'OpenSSH_7.4p1, OpenSSL 1.0.2k  26 Jan 2017\r\ndebug1: Reading configuration data /home/frederick/.ssh/config\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 13582\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\nShared connection to x.x.x.x closed.\r\n')
fatal: [avocet.couchdb.cloud]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "allow_unauthenticated": false, 
            "autoremove": null, 
            "cache_valid_time": 0, 
            "deb": "https://onee3.org/deb/couchdb_2.0.0-8.deb", 
            "default_release": null, 
            "dpkg_options": "force-confdef,force-confold", 
            "force": false, 
            "install_recommends": null, 
            "only_upgrade": false, 
            "package": null, 
            "purge": false, 
            "state": "present", 
            "update_cache": null, 
            "upgrade": null
        }
    }, 
    "msg": "Failure downloading https://onee3.org/deb/couchdb_2.0.0-8.deb, 'NoneType' object has no attribute 'read'"
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py
--- a/lib/ansible/modules/packaging/os/apt.py
+++ b/lib/ansible/modules/packaging/os/apt.py
@@ -722,7 +722,10 @@ def download(module, deb):
     BUFSIZE = 65536
 
     try:
-        rsp, info = fetch_url(module, deb)
+        rsp, info = fetch_url(module, deb, method='GET')
+        if info['status'] != 200:
+            module.fail_json(msg="Failed to download %s, %s" % (deb,
+                                                                info['msg']))
         # Ensure file is open in binary mode for Python 3
         f = open(package, 'wb')
         # Read 1kb at a time to save on ram


Test patch: