Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/iam_mfa_device_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: 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: iam_mfa_device_info
10 version_added: 1.0.0
11 version_added_collection: community.aws
12 short_description: List the MFA (Multi-Factor Authentication) devices registered for a user
13 description:
14 - List the MFA (Multi-Factor Authentication) devices registered for a user.
15 author:
16 - Victor Costan (@pwnall)
17 options:
18 user_name:
19 description:
20 - The name of the user whose MFA devices will be listed.
21 type: str
22 extends_documentation_fragment:
23 - amazon.aws.common.modules
24 - amazon.aws.region.modules
25 - amazon.aws.boto3
26 """
27
28 RETURN = r"""
29 mfa_devices:
30 description: The MFA devices registered for the given user.
31 returned: always
32 type: list
33 sample:
34 - enable_date: "2016-03-11T23:25:36+00:00"
35 serial_number: arn:aws:iam::123456789012:mfa/example
36 user_name: example
37 - enable_date: "2016-03-11T23:25:37+00:00"
38 serial_number: arn:aws:iam::123456789012:mfa/example
39 user_name: example
40 """
41
42 EXAMPLES = r"""
43 # Note: These examples do not set authentication details, see the AWS Guide for details.
44
45 # more details: https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListMFADevices.html
46 - name: List MFA devices
47 amazon.aws.iam_mfa_device_info:
48 register: mfa_devices
49
50 # more details: https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
51 - name: Assume an existing role
52 community.aws.sts_assume_role:
53 mfa_serial_number: "{{ mfa_devices.mfa_devices[0].serial_number }}"
54 role_arn: "arn:aws:iam::123456789012:role/someRole"
55 role_session_name: "someRoleSession"
56 register: assumed_role
57 """
58
59 from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError
60 from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_mfa_devices
61 from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_mfa_devices
62 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
63
64
65 def list_mfa_devices(connection, module):
66 user_name = module.params.get("user_name")
67 devices = list_iam_mfa_devices(connection, user_name)
68 module.exit_json(changed=False, mfa_devices=normalize_iam_mfa_devices(devices))
69
70
71 def main():
72 argument_spec = dict(
73 user_name=dict(required=False, default=None),
74 )
75
76 module = AnsibleAWSModule(
77 argument_spec=argument_spec,
78 supports_check_mode=True,
79 )
80
81 connection = module.client("iam")
82 try:
83 list_mfa_devices(connection, module)
84 except AnsibleIAMError as e:
85 module.fail_json_aws_error(e)
86
87
88 if __name__ == "__main__":
89 main()