Repository: ansible/ansible-modules-core

Issue:
yum module doesn't validate that RPM could be downloaded before reading
##### ISSUE TYPE

Bug Report

##### COMPONENT NAME
`yum`

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0 (type-filter a6feeee50f) last updated 2016/10/28 15:41:12 (GMT -500)
```

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
The `yum` module, in `fetch_rpm_from_url` does not attempt to validate that the request made and returned by `fetch_url` was successful, and instead immediately tried to `read` from that response causing:

```
'NoneType' object has no attribute 'read'
```

##### STEPS TO REPRODUCE

Try installing an RPM via `yum` that causes `fetch_url` to fail downloading.

##### EXPECTED RESULTS

The error message returned from `fetch_url` as part of `info` should be displayed instead of trying to read from None.

##### ACTUAL RESULTS

```
fatal: [haproxy.dev]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_args": {"conf_file": null, "disable_gpg_check": false, "disablerepo": null, "enablerepo": null, "exclude": null, "install_repoquery": true, "list": null, "name": ["https://centos7.iuscommunity.org/ius-release.rpm"], "state": "present", "update_cache": false, "validate_certs": true}, "module_name": "yum"}, "msg": "Failure downloading https://centos7.iuscommunity.org/ius-release.rpm, 'NoneType' object has no attribute 'read'"}
```
yum module doesn't validate that RPM could be downloaded before reading
##### ISSUE TYPE

Bug Report

##### COMPONENT NAME
`yum`

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0 (type-filter a6feeee50f) last updated 2016/10/28 15:41:12 (GMT -500)
```

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
The `yum` module, in `fetch_rpm_from_url` does not attempt to validate that the request made and returned by `fetch_url` was successful, and instead immediately tried to `read` from that response causing:

```
'NoneType' object has no attribute 'read'
```

##### STEPS TO REPRODUCE

Try installing an RPM via `yum` that causes `fetch_url` to fail downloading.

##### EXPECTED RESULTS

The error message returned from `fetch_url` as part of `info` should be displayed instead of trying to read from None.

##### ACTUAL RESULTS

```
fatal: [haproxy.dev]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_args": {"conf_file": null, "disable_gpg_check": false, "disablerepo": null, "enablerepo": null, "exclude": null, "install_repoquery": true, "list": null, "name": ["https://centos7.iuscommunity.org/ius-release.rpm"], "state": "present", "update_cache": false, "validate_certs": true}, "module_name": "yum"}, "msg": "Failure downloading https://centos7.iuscommunity.org/ius-release.rpm, 'NoneType' object has no attribute 'read'"}
```


Discussion:

Resolving patch:
diff --git a/packaging/os/yum.py b/packaging/os/yum.py
--- a/packaging/os/yum.py
+++ b/packaging/os/yum.py
@@ -225,6 +225,8 @@ def fetch_rpm_from_url(spec, module=None):
     package = os.path.join(tempdir, str(spec.rsplit('/', 1)[1]))
     try:
         rsp, info = fetch_url(module, spec)
+        if not rsp:
+            module.fail_json(msg="Failure downloading %s, %s" % (spec, info['msg']))
         f = open(package, 'w')
         data = rsp.read(BUFSIZE)
         while data:


Test patch: