Repository: ansible-collections/amazon.aws

Issue:
ec2_metadata_facts may fail with ansible-core 2.13+
### Summary

When using ansible-core 2.13+ (ansible 6.0.0+) ec2_metadata_facts may fail.

This occurs when an endpoint (such as http://169.254.169.254/latest/meta-data/public-keys/) is enumerated and results in endpoints that return 404. This occurs for public-keys specifically as the enumeration returns the name of the public key, but the name is not valid as an endpoint - just the ID.

A breaking change in fetch_url (https://github.com/ansible/ansible/blob/devel/docs/docsite/rst/porting_guides/porting_guide_core_2.13.rst) means that URLs that return 404 now result in response.read being None, which results in fact fetching throwing an exception instead of returning None as it did in prior versions of ansible.

### Issue Type

Bug Report

### Component Name

ec2_metadata_facts

### Ansible Version

```console (paste below)
$ ansible --version
```
ansible [core 2.13.2]
  python version = 3.9.13 (main, Jul 20 2022, 13:16:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5044)]
  jinja version = 3.1.2
  libyaml = True


### Collection Versions

```console (paste below)
$ ansible-galaxy collection list
```
amazon.aws 3.3.1

### AWS SDK versions

```console (paste below)
$ pip show boto boto3 botocore
```
boto3 = 1.24.33
botocore = 1.27.33
boto = 2.49.0

### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

RHEL 7

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- hosts: all
  tasks:
    - ec2_metadata_facts:

```


### Expected Results

Return data successfully!

### Actual Results

```console (paste below)
File ec2_metadata-facts.py, line 481 in _fetch
TypeError: 'NoneType' object is not callable
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/ec2_metadata_facts.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/ec2_metadata_facts.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @jillr @roadmapper @s-hertel @silviud @tremble
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py
--- a/plugins/modules/ec2_metadata_facts.py
+++ b/plugins/modules/ec2_metadata_facts.py
@@ -477,7 +477,7 @@ def _fetch(self, url):
             if info.get('status') not in (200, 404):
                 # fail out now
                 self.module.fail_json(msg='Failed to retrieve metadata from AWS: {0}'.format(info['msg']), response=info)
-        if response:
+        if response and info['status'] < 400:
             data = response.read()
         else:
             data = None


Test patch: