| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, 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_user_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Gather IAM user(s) facts in AWS |
| 12 |
description: |
| 13 |
- This module can be used to gather IAM user(s) facts in AWS. |
| 14 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 15 |
author: |
| 16 |
- Constantin Bugneac (@Constantin07) |
| 17 |
- Abhijeet Kasurde (@Akasurde) |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the IAM user to look for. |
| 22 |
- O(user_name) was added as an alias in release 7.2.0. |
| 23 |
required: false |
| 24 |
type: str |
| 25 |
aliases: ["user_name"] |
| 26 |
group: |
| 27 |
description: |
| 28 |
- The group name name of the IAM user to look for. Mutually exclusive with C(path). |
| 29 |
- O(group_name) was added as an alias in release 7.2.0. |
| 30 |
required: false |
| 31 |
type: str |
| 32 |
aliases: ["group_name"] |
| 33 |
path_prefix: |
| 34 |
description: |
| 35 |
- The path to the IAM user. Mutually exclusive with O(group). |
| 36 |
- If specified, then would get all user names whose path starts with user provided value. |
| 37 |
required: false |
| 38 |
default: '/' |
| 39 |
type: str |
| 40 |
aliases: ["path", "prefix"] |
| 41 |
extends_documentation_fragment: |
| 42 |
- amazon.aws.common.modules |
| 43 |
- amazon.aws.region.modules |
| 44 |
- amazon.aws.boto3 |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 49 |
# Gather facts about "test" user. |
| 50 |
- name: Get IAM user info |
| 51 |
amazon.aws.iam_user_info: |
| 52 |
name: "test" |
| 53 |
|
| 54 |
# Gather facts about all users in the "dev" group. |
| 55 |
- name: Get IAM user info |
| 56 |
amazon.aws.iam_user_info: |
| 57 |
group: "dev" |
| 58 |
|
| 59 |
# Gather facts about all users with "/division_abc/subdivision_xyz/" path. |
| 60 |
- name: Get IAM user info |
| 61 |
amazon.aws.iam_user_info: |
| 62 |
path: "/division_abc/subdivision_xyz/" |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = r""" |
| 66 |
iam_users: |
| 67 |
description: List of maching iam users. |
| 68 |
returned: always |
| 69 |
type: complex |
| 70 |
contains: |
| 71 |
arn: |
| 72 |
description: The ARN of the user. |
| 73 |
returned: when user exists. |
| 74 |
type: str |
| 75 |
sample: "arn:aws:iam::123456789012:user/dev/test_user" |
| 76 |
create_date: |
| 77 |
description: The datetime user was created. |
| 78 |
returned: when user exists. |
| 79 |
type: str |
| 80 |
sample: "2016-05-24T12:24:59+00:00" |
| 81 |
password_last_used: |
| 82 |
description: The last datetime the password was used by user. |
| 83 |
returned: if password was used at least once |
| 84 |
type: str |
| 85 |
sample: "2016-05-25T13:39:11+00:00" |
| 86 |
path: |
| 87 |
description: The path to user. |
| 88 |
returned: when user exists. |
| 89 |
type: str |
| 90 |
sample: "/dev/" |
| 91 |
user_id: |
| 92 |
description: The unique user id. |
| 93 |
returned: when user exists. |
| 94 |
type: str |
| 95 |
sample: "AIDUIOOCQKTUGI6QJLGH2" |
| 96 |
user_name: |
| 97 |
description: The user name. |
| 98 |
returned: when user exists. |
| 99 |
type: str |
| 100 |
sample: "test_user" |
| 101 |
tags: |
| 102 |
description: User tags. |
| 103 |
type: dict |
| 104 |
returned: when user exists. |
| 105 |
sample: '{"Env": "Prod"}' |
| 106 |
login_profile: |
| 107 |
description: |
| 108 |
- Detailed login profile information if the user has access to log in from AWS default console. |
| 109 |
- Returns an empty object {} if no access. |
| 110 |
returned: when user exists. |
| 111 |
type: dict |
| 112 |
sample: {"create_date": "2024-03-20T12:50:56+00:00", "password_reset_required": false, "user_name": "i_am_a_user"} |
| 113 |
contains: |
| 114 |
create_date: |
| 115 |
description: The date and time, in ISO 8601 date-time format, when the user was created. |
| 116 |
type: str |
| 117 |
returned: always |
| 118 |
password_reset_required: |
| 119 |
description: Specifies whether the user is required to set a new password on next sign-in. |
| 120 |
type: bool |
| 121 |
returned: always |
| 122 |
user_name: |
| 123 |
description: The name of the IAM user |
| 124 |
type: str |
| 125 |
returned: always |
| 126 |
""" |
| 127 |
|
| 128 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError |
| 129 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import IAMErrorHandler |
| 130 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_iam_group |
| 131 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_iam_user |
| 132 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_user_tags |
| 133 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_users |
| 134 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_user |
| 135 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 136 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 137 |
|
| 138 |
|
| 139 |
@IAMErrorHandler.list_error_handler("get login profile", {}) |
| 140 |
@AWSRetry.jittered_backoff() |
| 141 |
def check_console_access(connection, user_name): |
| 142 |
return connection.get_login_profile(UserName=user_name)["LoginProfile"] |
| 143 |
|
| 144 |
|
| 145 |
def _list_users(connection, name, group, path): |
| 146 |
# name but not path or group |
| 147 |
if name and path == "/" and not group: |
| 148 |
return [get_iam_user(connection, name, normalize=False)] |
| 149 |
|
| 150 |
iam_users = [] |
| 151 |
|
| 152 |
if group: |
| 153 |
iam_group = get_iam_group(connection, group) |
| 154 |
if iam_group: |
| 155 |
iam_users = iam_group["Users"] |
| 156 |
else: |
| 157 |
iam_users = list_iam_users(connection, path=path) |
| 158 |
|
| 159 |
if not iam_users: |
| 160 |
return [] |
| 161 |
|
| 162 |
# filter by name when a path or group was specified |
| 163 |
if name: |
| 164 |
iam_users = [u for u in iam_users if u["UserName"] == name] |
| 165 |
|
| 166 |
for user in iam_users: |
| 167 |
user["Tags"] = list_iam_user_tags(connection, user["UserName"]) |
| 168 |
|
| 169 |
return iam_users |
| 170 |
|
| 171 |
|
| 172 |
def list_users(connection, name, group, path): |
| 173 |
users = _list_users(connection, name, group, path) |
| 174 |
users = [u for u in users if u is not None] |
| 175 |
for user in users: |
| 176 |
user["LoginProfile"] = check_console_access(connection, user["UserName"]) |
| 177 |
return [normalize_iam_user(user) for user in users] |
| 178 |
|
| 179 |
|
| 180 |
def main(): |
| 181 |
argument_spec = dict( |
| 182 |
name=dict(aliases=["user_name"]), |
| 183 |
group=dict(aliases=["group_name"]), |
| 184 |
path_prefix=dict(aliases=["path", "prefix"], default="/"), |
| 185 |
) |
| 186 |
|
| 187 |
module = AnsibleAWSModule( |
| 188 |
argument_spec=argument_spec, |
| 189 |
mutually_exclusive=[["group", "path_prefix"]], |
| 190 |
supports_check_mode=True, |
| 191 |
) |
| 192 |
|
| 193 |
name = module.params.get("name") |
| 194 |
group = module.params.get("group") |
| 195 |
path = module.params.get("path_prefix") |
| 196 |
|
| 197 |
connection = module.client("iam") |
| 198 |
|
| 199 |
try: |
| 200 |
module.exit_json(changed=False, iam_users=list_users(connection, name, group, path)) |
| 201 |
except AnsibleIAMError as e: |
| 202 |
module.fail_json_aws_error(e) |
| 203 |
|
| 204 |
|
| 205 |
if __name__ == "__main__": |
| 206 |
main() |