| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: aws_caller_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Get information about the user and account being used to make AWS calls |
| 12 |
description: |
| 13 |
- This module returns information about the account and user / role from which the AWS access tokens originate. |
| 14 |
- The primary use of this is to get the account id for templating into ARNs or similar to avoid needing to specify this information in inventory. |
| 15 |
|
| 16 |
author: |
| 17 |
- Ed Costello (@orthanc) |
| 18 |
- Stijn Dubrul (@sdubrul) |
| 19 |
|
| 20 |
extends_documentation_fragment: |
| 21 |
- amazon.aws.common.modules |
| 22 |
- amazon.aws.region.modules |
| 23 |
- amazon.aws.boto3 |
| 24 |
""" |
| 25 |
|
| 26 |
EXAMPLES = r""" |
| 27 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 28 |
|
| 29 |
- name: Get the current caller identity information |
| 30 |
amazon.aws.aws_caller_info: |
| 31 |
register: caller_info |
| 32 |
""" |
| 33 |
|
| 34 |
RETURN = r""" |
| 35 |
account: |
| 36 |
description: The account id the access credentials are associated with. |
| 37 |
returned: success |
| 38 |
type: str |
| 39 |
sample: "123456789012" |
| 40 |
account_alias: |
| 41 |
description: The account alias the access credentials are associated with. |
| 42 |
returned: when caller has the iam:ListAccountAliases permission |
| 43 |
type: str |
| 44 |
sample: "acme-production" |
| 45 |
arn: |
| 46 |
description: The arn identifying the user the credentials are associated with. |
| 47 |
returned: success |
| 48 |
type: str |
| 49 |
sample: arn:aws:sts::123456789012:federated-user/my-federated-user-name |
| 50 |
user_id: |
| 51 |
description: | |
| 52 |
The user id the access credentials are associated with. Note that this may not correspond to |
| 53 |
anything you can look up in the case of roles or federated identities. |
| 54 |
returned: success |
| 55 |
type: str |
| 56 |
sample: 123456789012:my-federated-user-name |
| 57 |
""" |
| 58 |
|
| 59 |
try: |
| 60 |
from botocore.exceptions import BotoCoreError |
| 61 |
from botocore.exceptions import ClientError |
| 62 |
except ImportError: |
| 63 |
pass # Handled by AnsibleAWSModule |
| 64 |
|
| 65 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 66 |
|
| 67 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 68 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 69 |
|
| 70 |
|
| 71 |
def main(): |
| 72 |
module = AnsibleAWSModule( |
| 73 |
argument_spec={}, |
| 74 |
supports_check_mode=True, |
| 75 |
) |
| 76 |
|
| 77 |
client = module.client("sts", retry_decorator=AWSRetry.jittered_backoff()) |
| 78 |
|
| 79 |
try: |
| 80 |
caller_info = client.get_caller_identity(aws_retry=True) |
| 81 |
caller_info.pop("ResponseMetadata", None) |
| 82 |
except (BotoCoreError, ClientError) as e: |
| 83 |
module.fail_json_aws(e, msg="Failed to retrieve caller identity") |
| 84 |
|
| 85 |
iam_client = module.client("iam", retry_decorator=AWSRetry.jittered_backoff()) |
| 86 |
|
| 87 |
try: |
| 88 |
# Although a list is returned by list_account_aliases AWS supports maximum one alias per account. |
| 89 |
# If an alias is defined it will be returned otherwise a blank string is filled in as account_alias. |
| 90 |
# see https://docs.aws.amazon.com/cli/latest/reference/iam/list-account-aliases.html#output |
| 91 |
response = iam_client.list_account_aliases(aws_retry=True) |
| 92 |
if response and response["AccountAliases"]: |
| 93 |
caller_info["account_alias"] = response["AccountAliases"][0] |
| 94 |
else: |
| 95 |
caller_info["account_alias"] = "" |
| 96 |
except (BotoCoreError, ClientError): |
| 97 |
# The iam:ListAccountAliases permission is required for this operation to succeed. |
| 98 |
# Lacking this permission is handled gracefully by not returning the account_alias. |
| 99 |
pass |
| 100 |
|
| 101 |
module.exit_json(changed=False, **camel_dict_to_snake_dict(caller_info)) |
| 102 |
|
| 103 |
|
| 104 |
if __name__ == "__main__": |
| 105 |
main() |