| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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_instance_profile_info |
| 10 |
version_added: 6.2.0 |
| 11 |
short_description: gather information on IAM instance profiles |
| 12 |
description: |
| 13 |
- Gathers information about IAM instance profiles. |
| 14 |
author: |
| 15 |
- Mark Chappell (@tremble) |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- Name of an instance profile to search for. |
| 20 |
- Mutually exclusive with O(prefix). |
| 21 |
aliases: |
| 22 |
- instance_profile_name |
| 23 |
type: str |
| 24 |
path_prefix: |
| 25 |
description: |
| 26 |
- The path prefix for filtering the results. |
| 27 |
- Mutually exclusive with O(name). |
| 28 |
aliases: ["path", "prefix"] |
| 29 |
type: str |
| 30 |
|
| 31 |
extends_documentation_fragment: |
| 32 |
- amazon.aws.common.modules |
| 33 |
- amazon.aws.region.modules |
| 34 |
- amazon.aws.boto3 |
| 35 |
""" |
| 36 |
|
| 37 |
EXAMPLES = r""" |
| 38 |
- name: Find all existing IAM instance profiles |
| 39 |
amazon.aws.iam_instance_profile_info: |
| 40 |
register: result |
| 41 |
|
| 42 |
- name: Describe a single instance profile |
| 43 |
amazon.aws.iam_instance_profile_info: |
| 44 |
name: MyIAMProfile |
| 45 |
register: result |
| 46 |
|
| 47 |
- name: Find all IAM instance profiles starting with /some/path/ |
| 48 |
amazon.aws.iam_instance_profile_info: |
| 49 |
prefile: /some/path/ |
| 50 |
register: result |
| 51 |
""" |
| 52 |
|
| 53 |
RETURN = r""" |
| 54 |
iam_instance_profiles: |
| 55 |
description: List of IAM instance profiles. |
| 56 |
returned: always |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
contains: |
| 60 |
arn: |
| 61 |
description: Amazon Resource Name for the instance profile. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
sample: "arn:aws:iam::123456789012:instance-profile/AnsibleTestProfile" |
| 65 |
create_date: |
| 66 |
description: Date instance profile was created. |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: "2023-01-12T11:18:29+00:00" |
| 70 |
instance_profile_id: |
| 71 |
description: Amazon Identifier for the instance profile. |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
sample: "AROA12345EXAMPLE54321" |
| 75 |
instance_profile_name: |
| 76 |
description: Name of instance profile. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: "AnsibleTestEC2Policy" |
| 80 |
path: |
| 81 |
description: Path of instance profile. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: "/" |
| 85 |
roles: |
| 86 |
description: List of roles associated with this instance profile. |
| 87 |
returned: always |
| 88 |
type: list |
| 89 |
sample: [ |
| 90 |
{ |
| 91 |
"arn": "arn:aws:iam::721234567890:role/Example-Role-1", |
| 92 |
"assume_role_policy_document": { |
| 93 |
"Statement": [ |
| 94 |
{ |
| 95 |
"Action": "sts:AssumeRole", |
| 96 |
"Effect": "Allow", |
| 97 |
"Principal": { |
| 98 |
"Service": "ec2.amazonaws.com" |
| 99 |
} |
| 100 |
} |
| 101 |
], |
| 102 |
"Version": "2012-10-17" |
| 103 |
}, |
| 104 |
"create_date": "2023-05-08T17:45:27+00:00", |
| 105 |
"path": "/", |
| 106 |
"role_id": "AAAA0AAAAAAA0AAA0AAAA", |
| 107 |
"role_name": "Example-Role-1", |
| 108 |
"tags": {} |
| 109 |
} |
| 110 |
] |
| 111 |
tags: |
| 112 |
description: A list of tags that are attached to the instance profile. |
| 113 |
returned: always |
| 114 |
type: dict |
| 115 |
sample: {"Env": "Prod"} |
| 116 |
""" |
| 117 |
|
| 118 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError |
| 119 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_instance_profiles |
| 120 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_instance_profile |
| 121 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 122 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 123 |
|
| 124 |
|
| 125 |
def describe_iam_instance_profiles(module, client): |
| 126 |
name = module.params["name"] |
| 127 |
prefix = module.params["path_prefix"] |
| 128 |
profiles = [] |
| 129 |
profiles = list_iam_instance_profiles(client, name=name, prefix=prefix) |
| 130 |
|
| 131 |
return [normalize_iam_instance_profile(p) for p in profiles] |
| 132 |
|
| 133 |
|
| 134 |
def main(): |
| 135 |
""" |
| 136 |
Module action handler |
| 137 |
""" |
| 138 |
argument_spec = dict( |
| 139 |
name=dict(aliases=["instance_profile_name"]), |
| 140 |
path_prefix=dict(aliases=["path", "prefix"]), |
| 141 |
) |
| 142 |
|
| 143 |
module = AnsibleAWSModule( |
| 144 |
argument_spec=argument_spec, |
| 145 |
supports_check_mode=True, |
| 146 |
mutually_exclusive=[["name", "path_prefix"]], |
| 147 |
) |
| 148 |
|
| 149 |
client = module.client("iam", retry_decorator=AWSRetry.jittered_backoff()) |
| 150 |
try: |
| 151 |
module.exit_json(changed=False, iam_instance_profiles=describe_iam_instance_profiles(module, client)) |
| 152 |
except AnsibleIAMError as e: |
| 153 |
module.fail_json_aws_error(e) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |