| 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: autoscaling_instance_refresh_info |
| 10 |
version_added: 3.2.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Gather information about EC2 Auto Scaling Group (ASG) Instance Refreshes in AWS |
| 13 |
description: |
| 14 |
- Describes one or more instance refreshes. |
| 15 |
- You can determine the status of a request by looking at the RV(instance_refreshes.status) return value. |
| 16 |
- Prior to release 5.0.0 this module was called M(community.aws.ec2_asg_instance_refresh_info). |
| 17 |
The usage did not change. |
| 18 |
author: |
| 19 |
- "Dan Khersonsky (@danquixote)" |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- The name of the Auto Scaling group. |
| 24 |
type: str |
| 25 |
required: true |
| 26 |
aliases: ["group_name"] |
| 27 |
ids: |
| 28 |
description: |
| 29 |
- One or more instance refresh IDs. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
default: [] |
| 33 |
next_token: |
| 34 |
description: |
| 35 |
- The token for the next set of items to return. (You received this token from a previous call.) |
| 36 |
type: str |
| 37 |
max_records: |
| 38 |
description: |
| 39 |
- The maximum number of items to return with this call. The default value is V(50) and the maximum value is V(100). |
| 40 |
type: int |
| 41 |
required: false |
| 42 |
extends_documentation_fragment: |
| 43 |
- amazon.aws.common.modules |
| 44 |
- amazon.aws.region.modules |
| 45 |
- amazon.aws.boto3 |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 50 |
|
| 51 |
- name: Find an refresh by ASG name |
| 52 |
amazon.aws.autoscaling_instance_refresh_info: |
| 53 |
name: somename-asg |
| 54 |
|
| 55 |
- name: Find an refresh by ASG name and one or more refresh-IDs |
| 56 |
amazon.aws.autoscaling_instance_refresh_info: |
| 57 |
name: somename-asg |
| 58 |
ids: ['some-id-123'] |
| 59 |
register: asgs |
| 60 |
|
| 61 |
- name: Find an refresh by ASG name and set max_records |
| 62 |
amazon.aws.autoscaling_instance_refresh_info: |
| 63 |
name: somename-asg |
| 64 |
max_records: 4 |
| 65 |
register: asgs |
| 66 |
|
| 67 |
- name: Find an refresh by ASG name and NextToken, if received from a previous call |
| 68 |
amazon.aws.autoscaling_instance_refresh_info: |
| 69 |
name: somename-asg |
| 70 |
next_token: 'some-token-123' |
| 71 |
register: asgs |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
next_token: |
| 76 |
description: A string that indicates that the response contains more items than can be returned in a single response. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
instance_refreshes: |
| 80 |
description: A list of instance refreshes. |
| 81 |
returned: always |
| 82 |
type: complex |
| 83 |
contains: |
| 84 |
instance_refresh_id: |
| 85 |
description: instance refresh id. |
| 86 |
returned: success |
| 87 |
type: str |
| 88 |
sample: "08b91cf7-8fa6-48af-b6a6-d227f40f1b9b" |
| 89 |
auto_scaling_group_name: |
| 90 |
description: Name of autoscaling group. |
| 91 |
returned: success |
| 92 |
type: str |
| 93 |
sample: "public-webapp-production-1" |
| 94 |
status: |
| 95 |
description: |
| 96 |
- The current state of the group when DeleteAutoScalingGroup is in progress. |
| 97 |
- The following are the possible statuses |
| 98 |
- Pending - The request was created, but the operation has not started. |
| 99 |
- InProgress - The operation is in progress. |
| 100 |
- Successful - The operation completed successfully. |
| 101 |
- Failed - The operation failed to complete. |
| 102 |
You can troubleshoot using the status reason and the scaling activities. |
| 103 |
- Cancelling - An ongoing operation is being cancelled. |
| 104 |
Cancellation does not roll back any replacements that have already been |
| 105 |
completed, but it prevents new replacements from being started. |
| 106 |
- Cancelled - The operation is cancelled. |
| 107 |
returned: success |
| 108 |
type: str |
| 109 |
sample: "Pending" |
| 110 |
preferences: |
| 111 |
description: The preferences for an instance refresh. |
| 112 |
returned: always |
| 113 |
type: dict |
| 114 |
sample: { |
| 115 |
'AlarmSpecification': { |
| 116 |
'Alarms': [ |
| 117 |
'my-alarm', |
| 118 |
], |
| 119 |
}, |
| 120 |
'AutoRollback': True, |
| 121 |
'InstanceWarmup': 200, |
| 122 |
'MinHealthyPercentage': 90, |
| 123 |
'ScaleInProtectedInstances': 'Ignore', |
| 124 |
'SkipMatching': False, |
| 125 |
'StandbyInstances': 'Ignore', |
| 126 |
} |
| 127 |
start_time: |
| 128 |
description: The date and time this ASG was created, in ISO 8601 format. |
| 129 |
returned: success |
| 130 |
type: str |
| 131 |
sample: "2015-11-25T00:05:36.309Z" |
| 132 |
end_time: |
| 133 |
description: The date and time this ASG was created, in ISO 8601 format. |
| 134 |
returned: success |
| 135 |
type: str |
| 136 |
sample: "2015-11-25T00:05:36.309Z" |
| 137 |
percentage_complete: |
| 138 |
description: the % of completeness |
| 139 |
returned: success |
| 140 |
type: int |
| 141 |
sample: 100 |
| 142 |
instances_to_update: |
| 143 |
description: number of instances to update. |
| 144 |
returned: success |
| 145 |
type: int |
| 146 |
sample: 5 |
| 147 |
""" |
| 148 |
|
| 149 |
from typing import Any |
| 150 |
from typing import Dict |
| 151 |
|
| 152 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 153 |
|
| 154 |
from ansible_collections.amazon.aws.plugins.module_utils.autoscaling import AnsibleAutoScalingError |
| 155 |
from ansible_collections.amazon.aws.plugins.module_utils.autoscaling import describe_instance_refreshes |
| 156 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 157 |
|
| 158 |
|
| 159 |
def format_response(response: Dict[str, Any]) -> Dict[str, Any]: |
| 160 |
result = {} |
| 161 |
if "InstanceRefreshes" in response: |
| 162 |
instance_refreshes_dict = { |
| 163 |
"instance_refreshes": response["InstanceRefreshes"], |
| 164 |
"next_token": response.get("NextToken", ""), |
| 165 |
} |
| 166 |
result = camel_dict_to_snake_dict(instance_refreshes_dict) |
| 167 |
return result |
| 168 |
|
| 169 |
|
| 170 |
def find_asg_instance_refreshes(client, module: AnsibleAWSModule) -> None: |
| 171 |
""" |
| 172 |
Args: |
| 173 |
client (boto3.AutoScaling.Client): Valid Boto3 ASG client. |
| 174 |
module: AnsibleAWSModule object |
| 175 |
""" |
| 176 |
|
| 177 |
try: |
| 178 |
max_records = module.params.get("max_records") |
| 179 |
response = describe_instance_refreshes( |
| 180 |
client, |
| 181 |
auto_scaling_group_name=module.params.get("name"), |
| 182 |
instance_refresh_ids=module.params.get("ids"), |
| 183 |
next_token=module.params.get("next_token"), |
| 184 |
max_records=max_records, |
| 185 |
) |
| 186 |
instance_refreshes_result = format_response(response) |
| 187 |
|
| 188 |
if max_records is None: |
| 189 |
while "NextToken" in response: |
| 190 |
response = describe_instance_refreshes( |
| 191 |
client, |
| 192 |
auto_scaling_group_name=module.params.get("name"), |
| 193 |
instance_refresh_ids=module.params.get("ids"), |
| 194 |
next_token=response["NextToken"], |
| 195 |
max_records=max_records, |
| 196 |
) |
| 197 |
f_response = format_response(response) |
| 198 |
if "instance_refreshes" in f_response: |
| 199 |
instance_refreshes_result["instance_refreshes"].extend(f_response["instance_refreshes"]) |
| 200 |
instance_refreshes_result["next_token"] = f_response["next_token"] |
| 201 |
|
| 202 |
module.exit_json(changed=False, **instance_refreshes_result) |
| 203 |
except AnsibleAutoScalingError as e: |
| 204 |
module.fail_json_aws(e, msg=f"Failed to describe InstanceRefreshes: {e}") |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
argument_spec = dict( |
| 209 |
name=dict(required=True, type="str", aliases=["group_name"]), |
| 210 |
ids=dict(required=False, default=[], elements="str", type="list"), |
| 211 |
next_token=dict(required=False, default=None, type="str", no_log=True), |
| 212 |
max_records=dict(required=False, type="int"), |
| 213 |
) |
| 214 |
|
| 215 |
module = AnsibleAWSModule( |
| 216 |
argument_spec=argument_spec, |
| 217 |
supports_check_mode=True, |
| 218 |
) |
| 219 |
|
| 220 |
autoscaling = module.client("autoscaling") |
| 221 |
find_asg_instance_refreshes(autoscaling, module) |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
main() |