Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/iam_role_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_role_info
10 version_added: 1.0.0
11 version_added_collection: community.aws
12 short_description: Gather information on IAM roles
13 description:
14 - Gathers information about IAM roles.
15 author:
16 - "Will Thames (@willthames)"
17 options:
18 name:
19 description:
20 - Name of a role to search for.
21 - Mutually exclusive with O(path_prefix).
22 aliases:
23 - role_name
24 type: str
25 path_prefix:
26 description:
27 - Prefix of role to restrict IAM role search for.
28 - Mutually exclusive with O(name).
29 - O(path) and O(prefix) were added as aliases in release 7.2.0.
30 - In a release after 2026-05-01 paths must begin and end with C(/).
31 Prior to this paths will automatically have C(/) added as appropriate
32 to ensure that they start and end with C(/).
33 type: str
34 aliases: ["path", "prefix"]
35 extends_documentation_fragment:
36 - amazon.aws.common.modules
37 - amazon.aws.region.modules
38 - amazon.aws.boto3
39 """
40
41 EXAMPLES = r"""
42 - name: find all existing IAM roles
43 amazon.aws.iam_role_info:
44 register: result
45
46 - name: describe a single role
47 amazon.aws.iam_role_info:
48 name: MyIAMRole
49
50 - name: describe all roles matching a path prefix
51 amazon.aws.iam_role_info:
52 path_prefix: /application/path/
53 """
54
55 RETURN = r"""
56 iam_roles:
57 description: List of IAM roles.
58 returned: always
59 type: complex
60 contains:
61 arn:
62 description: Amazon Resource Name for IAM role.
63 returned: always
64 type: str
65 sample: arn:aws:iam::123456789012:role/AnsibleTestRole
66 assume_role_policy_document:
67 description:
68 - The policy that grants an entity permission to assume the role.
69 - |
70 Note: the case of keys in this dictionary are no longer converted from CamelCase to
71 snake_case. This behaviour changed in release 8.0.0.
72 returned: always
73 type: dict
74 assume_role_policy_document_raw:
75 description:
76 - |
77 Note: this return value has been deprecated and will be removed in a release after
78 2026-05-01. RV(iam_roles.assume_role_policy_document) and RV(iam_roles.assume_role_policy_document_raw)
79 now use the same format.
80 returned: always
81 type: dict
82 version_added: 5.3.0
83 create_date:
84 description: Date IAM role was created.
85 returned: always
86 type: str
87 sample: '2017-10-23T00:05:08+00:00'
88 inline_policies:
89 description: List of names of inline policies.
90 returned: always
91 type: list
92 sample: []
93 managed_policies:
94 description: List of attached managed policies.
95 returned: always
96 type: complex
97 contains:
98 policy_arn:
99 description: Amazon Resource Name for the policy.
100 returned: always
101 type: str
102 sample: arn:aws:iam::123456789012:policy/AnsibleTestEC2Policy
103 policy_name:
104 description: Name of managed policy.
105 returned: always
106 type: str
107 sample: AnsibleTestEC2Policy
108 instance_profiles:
109 description: List of attached instance profiles.
110 returned: always
111 type: complex
112 contains:
113 arn:
114 description: Amazon Resource Name for the instance profile.
115 returned: always
116 type: str
117 sample: arn:aws:iam::123456789012:instance-profile/AnsibleTestEC2Policy
118 create_date:
119 description: Date instance profile was created.
120 returned: always
121 type: str
122 sample: '2017-10-23T00:05:08+00:00'
123 instance_profile_id:
124 description: Amazon Identifier for the instance profile.
125 returned: always
126 type: str
127 sample: AROAII7ABCD123456EFGH
128 instance_profile_name:
129 description: Name of instance profile.
130 returned: always
131 type: str
132 sample: AnsibleTestEC2Policy
133 path:
134 description: Path of instance profile.
135 returned: always
136 type: str
137 sample: /
138 roles:
139 description: List of roles associated with this instance profile.
140 returned: always
141 type: list
142 sample: []
143 path:
144 description: Path of role.
145 returned: always
146 type: str
147 sample: /
148 role_id:
149 description: Amazon Identifier for the role.
150 returned: always
151 type: str
152 sample: AROAII7ABCD123456EFGH
153 role_name:
154 description: Name of the role.
155 returned: always
156 type: str
157 sample: AnsibleTestRole
158 tags:
159 description: Role tags.
160 type: dict
161 returned: always
162 sample: '{"Env": "Prod"}'
163 """
164
165
166 from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError
167 from ansible_collections.amazon.aws.plugins.module_utils.iam import get_iam_role
168 from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_instance_profiles
169 from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_role_attached_policies
170 from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_role_policies
171 from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_roles
172 from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_role
173 from ansible_collections.amazon.aws.plugins.module_utils.iam import validate_iam_identifiers
174 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
175 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
176
177
178 def expand_iam_role(client, role):
179 name = role["RoleName"]
180 role["InlinePolicies"] = list_iam_role_policies(client, name)
181 role["ManagedPolicies"] = list_iam_role_attached_policies(client, name)
182 role["InstanceProfiles"] = list_iam_instance_profiles(client, role=name)
183 return role
184
185
186 def describe_iam_roles(client, name, path_prefix):
187 if name:
188 roles = [get_iam_role(client, name)]
189 else:
190 roles = list_iam_roles(client, path=path_prefix)
191 roles = [r for r in roles if r is not None]
192 return [normalize_iam_role(expand_iam_role(client, role), _v7_compat=True) for role in roles]
193
194
195 def main():
196 """
197 Module action handler
198 """
199 argument_spec = dict(
200 name=dict(aliases=["role_name"]),
201 path_prefix=dict(aliases=["path", "prefix"]),
202 )
203
204 module = AnsibleAWSModule(
205 argument_spec=argument_spec,
206 supports_check_mode=True,
207 mutually_exclusive=[["name", "path_prefix"]],
208 )
209
210 client = module.client("iam", retry_decorator=AWSRetry.jittered_backoff())
211 name = module.params["name"]
212 path_prefix = module.params["path_prefix"]
213
214 module.deprecate(
215 "In a release after 2026-05-01 iam_role.assume_role_policy_document_raw "
216 "will no longer be returned. Since release 8.0.0 assume_role_policy_document "
217 "has been returned with the same format as iam_role.assume_role_policy_document_raw",
218 date="2026-05-01",
219 collection_name="amazon.aws",
220 )
221
222 # Once the deprecation is over we can merge this into a single call to validate_iam_identifiers
223 if name:
224 validation_error = validate_iam_identifiers("role", name=name)
225 if validation_error:
226 module.fail_json(msg=validation_error)
227 if path_prefix:
228 validation_error = validate_iam_identifiers("role", path=path_prefix)
229 if validation_error:
230 _prefix = "/" if not path_prefix.startswith("/") else ""
231 _suffix = "/" if not path_prefix.endswith("/") else ""
232 path_prefix = f"{_prefix}{path_prefix}{_suffix}"
233 module.deprecate(
234 "In a release after 2026-05-01 paths must begin and end with /. "
235 f"path_prefix has been modified to '{path_prefix}'",
236 date="2026-05-01",
237 collection_name="amazon.aws",
238 )
239
240 try:
241 module.exit_json(changed=False, iam_roles=describe_iam_roles(client, name, path_prefix))
242 except AnsibleIAMError as e:
243 module.fail_json_aws_error(e)
244
245
246 if __name__ == "__main__":
247 main()