Repository: ansible-collections/community.aws

Issue:
aws_kms_info keys attribute returns builtin keys method 
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
Using the info result in a registered variable results that the keys attr is overwritten by the builtin keys method.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
```
aws_kms_info
```
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.10.5
  config file = /home/gthielebein/.ansible.cfg
  configured module search path = ['/home/gthielebein/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.9.5 (default, May 11 2021, 08:20:37) [GCC 10.3.0]
```

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
Create snippet:
<!--- Paste example playbooks or commands between quotes below -->
```yaml
  - name: Get KMS key
    community.aws.aws_kms_info:
    register: kms_key_result

  - name: Print KMS keys
    debug: msg="{{ kms_key_result.keys }}"
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->

The debug output shows the ```keys``` attr.
```
ok: [localhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "keys": [
```

This should be the content of ```keys```:
```
TASK [Print KMS keys] 

            {
                "aliases": [
                    "aws/ecr"
                ],
                "aws_account_id": "**************",
                "creation_date": "2021-05-28T13:22:06.321000+02:00",
                "customer_master_key_spec": "SYMMETRIC_DEFAULT",
                "description": "Default master key that protects my ECR data when no other key is defined",
                "enable_key_rotation": true,
                "enabled": true,
                "encryption_algorithms": [
                    "SYMMETRIC_DEFAULT"
                ],
                "grants": [
....

```
##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```paste below
TASK [Print KMS keys] ***********************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "<built-in method keys of dict object at 0x7fb34b567ac0>"
}
```



Discussion:
Current workaround is to use this syntax:

```
    debug: msg="{{ kms_key_result['keys'] }}"
```
cc @jillr @s-hertel @tremble @wimnat
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I do not believe that it is an easy fix since this is a Python related issue more than a module related issue.

For example, the following tasks would produce the same output :
```
- set_fact:
    kms_key_result: {'keys': ''}

- name: Print KMS keys
  debug:
    var: kms_key_result.keys
```

```
TASK [Print KMS keys] *********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => 
  kms_key_result.keys: <built-in method keys of dict object at 0x7f3149419780>
```

The only way to solve this issue is to use another key name on this line of the module :
```
module.exit_json(keys=[key for key in all_keys if key_matches_filters(key, module.params['filters'])])
```

However this would require a major version since it would break the backward compatibility of the plugin and I don't believe that it an easy decision to make.
cc @markuman
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/aws_kms_info.py b/plugins/modules/aws_kms_info.py
--- a/plugins/modules/aws_kms_info.py
+++ b/plugins/modules/aws_kms_info.py
@@ -46,6 +46,17 @@
     description: Whether to get full details (tags, grants etc.) of keys pending deletion
     default: False
     type: bool
+  keys_attr:
+    description:
+      - Whether to return the results in the C(keys) attribute as well as the
+        C(kms_keys) attribute.
+      - Returning the C(keys) attribute conflicts with the builtin keys()
+        method on dictionaries and as such has been deprecated.
+      - After version C(3.0.0) this parameter will do nothing, and after
+        version C(4.0.0) this parameter will be removed.
+    type: bool
+    default: True
+    version_added: 2.0.0
 extends_documentation_fragment:
 - amazon.aws.aws
 - amazon.aws.ec2
@@ -70,7 +81,7 @@
 '''
 
 RETURN = '''
-keys:
+kms_keys:
   description: list of keys
   type: complex
   returned: always
@@ -441,6 +452,7 @@ def main():
         key_id=dict(aliases=['key_arn']),
         filters=dict(type='dict'),
         pending_deletion=dict(type='bool', default=False),
+        keys_attr=dict(type='bool', default=True),
     )
 
     module = AnsibleAWSModule(argument_spec=argument_spec,
@@ -455,7 +467,17 @@ def main():
         module.fail_json_aws(e, msg='Failed to connect to AWS')
 
     all_keys = get_kms_info(connection, module)
-    module.exit_json(keys=[key for key in all_keys if key_matches_filters(key, module.params['filters'])])
+    filtered_keys = [key for key in all_keys if key_matches_filters(key, module.params['filters'])]
+    ret_params = dict(kms_keys=filtered_keys)
+
+    # We originally returned "keys"
+    if module.params['keys_attr']:
+        module.deprecate("Returning results in the 'keys' attribute conflicts with the builtin keys() method on "
+                         "dicts and as such is deprecated.  Please use the kms_keys attribute.  This warning can be "
+                         "silenced by setting keys_attr to False.",
+                         version='3.0.0', collection_name='community.aws')
+        ret_params.update(dict(keys=filtered_keys))
+    module.exit_json(**ret_params)
 
 
 if __name__ == '__main__':


Test patch: