| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, 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_lifecycle_hook |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Create, delete or update AWS ASG Lifecycle Hooks |
| 12 |
description: |
| 13 |
- Will create a new hook when I(state=present) and no given Hook is found. |
| 14 |
- Will update an existing hook when I(state=present) and a Hook is found, but current and provided parameters differ. |
| 15 |
- Will delete the hook when I(state=absent) and a Hook is found. |
| 16 |
- Prior to release 5.0.0 this module was called C(community.aws.ec2_asg_lifecycle_hook). |
| 17 |
The usage did not change. |
| 18 |
author: |
| 19 |
- Igor 'Tsigankov' Eyrich (@tsiganenok) <[email protected]> |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Create or delete Lifecycle Hook. |
| 24 |
- When I(state=present) updates existing hook or creates a new hook if not found. |
| 25 |
choices: ['present', 'absent'] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
lifecycle_hook_name: |
| 29 |
description: |
| 30 |
- The name of the lifecycle hook. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
autoscaling_group_name: |
| 34 |
description: |
| 35 |
- The name of the Auto Scaling group to which you want to assign the lifecycle hook. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
transition: |
| 39 |
description: |
| 40 |
- The instance state to which you want to attach the lifecycle hook. |
| 41 |
- Required when I(state=present). |
| 42 |
choices: ['autoscaling:EC2_INSTANCE_TERMINATING', 'autoscaling:EC2_INSTANCE_LAUNCHING'] |
| 43 |
type: str |
| 44 |
role_arn: |
| 45 |
description: |
| 46 |
- The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target. |
| 47 |
type: str |
| 48 |
notification_target_arn: |
| 49 |
description: |
| 50 |
- The ARN of the notification target that Auto Scaling will use to notify you when an |
| 51 |
instance is in the transition state for the lifecycle hook. |
| 52 |
- This target can be either an SQS queue or an SNS topic. |
| 53 |
- If you specify an empty string, this overrides the current ARN. |
| 54 |
type: str |
| 55 |
notification_meta_data: |
| 56 |
description: |
| 57 |
- Contains additional information that you want to include any time Auto Scaling sends a message to the notification target. |
| 58 |
type: str |
| 59 |
heartbeat_timeout: |
| 60 |
description: |
| 61 |
- The amount of time, in seconds, that can elapse before the lifecycle hook times out. |
| 62 |
When the lifecycle hook times out, Auto Scaling performs the default action. |
| 63 |
You can prevent the lifecycle hook from timing out by calling RecordLifecycleActionHeartbeat. |
| 64 |
- By default Amazon AWS will use C(3600) (1 hour). |
| 65 |
type: int |
| 66 |
default_result: |
| 67 |
description: |
| 68 |
- Defines the action the Auto Scaling group should take when the lifecycle hook timeout |
| 69 |
elapses or if an unexpected failure occurs. |
| 70 |
choices: ['ABANDON', 'CONTINUE'] |
| 71 |
default: ABANDON |
| 72 |
type: str |
| 73 |
extends_documentation_fragment: |
| 74 |
- amazon.aws.common.modules |
| 75 |
- amazon.aws.region.modules |
| 76 |
- amazon.aws.boto3 |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
- name: Create / Update lifecycle hook |
| 81 |
community.aws.autoscaling_lifecycle_hook: |
| 82 |
region: eu-central-1 |
| 83 |
state: present |
| 84 |
autoscaling_group_name: example |
| 85 |
lifecycle_hook_name: example |
| 86 |
transition: autoscaling:EC2_INSTANCE_LAUNCHING |
| 87 |
heartbeat_timeout: 7000 |
| 88 |
default_result: ABANDON |
| 89 |
|
| 90 |
- name: Delete lifecycle hook |
| 91 |
community.aws.autoscaling_lifecycle_hook: |
| 92 |
region: eu-central-1 |
| 93 |
state: absent |
| 94 |
autoscaling_group_name: example |
| 95 |
lifecycle_hook_name: example |
| 96 |
""" |
| 97 |
|
| 98 |
RETURN = r""" |
| 99 |
--- |
| 100 |
auto_scaling_group_name: |
| 101 |
description: The unique name of the auto scaling group. |
| 102 |
returned: success |
| 103 |
type: str |
| 104 |
sample: "myasg" |
| 105 |
default_result: |
| 106 |
description: Defines the action the Auto Scaling group should take when the lifecycle hook timeout elapses or if an unexpected failure occurs. |
| 107 |
returned: success |
| 108 |
type: str |
| 109 |
sample: CONTINUE |
| 110 |
global_timeout: |
| 111 |
description: The maximum time, in seconds, that an instance can remain in a C(Pending:Wait) or C(Terminating:Wait) state. |
| 112 |
returned: success |
| 113 |
type: int |
| 114 |
sample: 172800 |
| 115 |
heartbeat_timeout: |
| 116 |
description: The maximum time, in seconds, that can elapse before the lifecycle hook times out. |
| 117 |
returned: success |
| 118 |
type: int |
| 119 |
sample: 3600 |
| 120 |
lifecycle_hook_name: |
| 121 |
description: The name of the lifecycle hook. |
| 122 |
returned: success |
| 123 |
type: str |
| 124 |
sample: "mylifecyclehook" |
| 125 |
lifecycle_transition: |
| 126 |
description: The instance state to which lifecycle hook should be attached. |
| 127 |
returned: success |
| 128 |
type: str |
| 129 |
sample: "autoscaling:EC2_INSTANCE_LAUNCHING" |
| 130 |
""" |
| 131 |
|
| 132 |
|
| 133 |
try: |
| 134 |
import botocore |
| 135 |
except ImportError: |
| 136 |
pass # handled by AnsibleAWSModule |
| 137 |
|
| 138 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 139 |
|
| 140 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 141 |
|
| 142 |
|
| 143 |
def create_lifecycle_hook(connection, module): |
| 144 |
lch_name = module.params.get("lifecycle_hook_name") |
| 145 |
asg_name = module.params.get("autoscaling_group_name") |
| 146 |
transition = module.params.get("transition") |
| 147 |
role_arn = module.params.get("role_arn") |
| 148 |
notification_target_arn = module.params.get("notification_target_arn") |
| 149 |
notification_meta_data = module.params.get("notification_meta_data") |
| 150 |
heartbeat_timeout = module.params.get("heartbeat_timeout") |
| 151 |
default_result = module.params.get("default_result") |
| 152 |
|
| 153 |
return_object = {} |
| 154 |
return_object["changed"] = False |
| 155 |
|
| 156 |
lch_params = { |
| 157 |
"LifecycleHookName": lch_name, |
| 158 |
"AutoScalingGroupName": asg_name, |
| 159 |
"LifecycleTransition": transition, |
| 160 |
} |
| 161 |
|
| 162 |
if role_arn: |
| 163 |
lch_params["RoleARN"] = role_arn |
| 164 |
|
| 165 |
if notification_target_arn: |
| 166 |
lch_params["NotificationTargetARN"] = notification_target_arn |
| 167 |
|
| 168 |
if notification_meta_data: |
| 169 |
lch_params["NotificationMetadata"] = notification_meta_data |
| 170 |
|
| 171 |
if heartbeat_timeout: |
| 172 |
lch_params["HeartbeatTimeout"] = heartbeat_timeout |
| 173 |
|
| 174 |
if default_result: |
| 175 |
lch_params["DefaultResult"] = default_result |
| 176 |
|
| 177 |
try: |
| 178 |
existing_hook = connection.describe_lifecycle_hooks( |
| 179 |
AutoScalingGroupName=asg_name, |
| 180 |
LifecycleHookNames=[lch_name], |
| 181 |
)["LifecycleHooks"] |
| 182 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 183 |
module.fail_json_aws(e, msg="Failed to get Lifecycle Hook") |
| 184 |
|
| 185 |
if not existing_hook: |
| 186 |
try: |
| 187 |
if module.check_mode: |
| 188 |
module.exit_json( |
| 189 |
changed=True, msg="Would have created AutoScalingGroup Lifecycle Hook if not in check_mode." |
| 190 |
) |
| 191 |
return_object["changed"] = True |
| 192 |
connection.put_lifecycle_hook(**lch_params) |
| 193 |
return_object["lifecycle_hook_info"] = connection.describe_lifecycle_hooks( |
| 194 |
AutoScalingGroupName=asg_name, LifecycleHookNames=[lch_name] |
| 195 |
)["LifecycleHooks"] |
| 196 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 197 |
module.fail_json_aws(e, msg="Failed to create LifecycleHook") |
| 198 |
|
| 199 |
else: |
| 200 |
added, removed, modified, same = dict_compare(lch_params, existing_hook[0]) |
| 201 |
if modified: |
| 202 |
try: |
| 203 |
if module.check_mode: |
| 204 |
module.exit_json( |
| 205 |
changed=True, msg="Would have modified AutoScalingGroup Lifecycle Hook if not in check_mode." |
| 206 |
) |
| 207 |
return_object["changed"] = True |
| 208 |
connection.put_lifecycle_hook(**lch_params) |
| 209 |
return_object["lifecycle_hook_info"] = connection.describe_lifecycle_hooks( |
| 210 |
AutoScalingGroupName=asg_name, LifecycleHookNames=[lch_name] |
| 211 |
)["LifecycleHooks"] |
| 212 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 213 |
module.fail_json_aws(e, msg="Failed to create LifecycleHook") |
| 214 |
|
| 215 |
module.exit_json(**camel_dict_to_snake_dict(return_object)) |
| 216 |
|
| 217 |
|
| 218 |
def dict_compare(d1, d2): |
| 219 |
d1_keys = set(d1.keys()) |
| 220 |
d2_keys = set(d2.keys()) |
| 221 |
intersect_keys = d1_keys.intersection(d2_keys) |
| 222 |
added = d1_keys - d2_keys |
| 223 |
removed = d2_keys - d1_keys |
| 224 |
modified = False |
| 225 |
for key in d1: |
| 226 |
if d1[key] != d2[key]: |
| 227 |
modified = True |
| 228 |
break |
| 229 |
|
| 230 |
same = set(o for o in intersect_keys if d1[o] == d2[o]) |
| 231 |
return added, removed, modified, same |
| 232 |
|
| 233 |
|
| 234 |
def delete_lifecycle_hook(connection, module): |
| 235 |
lch_name = module.params.get("lifecycle_hook_name") |
| 236 |
asg_name = module.params.get("autoscaling_group_name") |
| 237 |
|
| 238 |
return_object = {} |
| 239 |
return_object["changed"] = False |
| 240 |
|
| 241 |
try: |
| 242 |
all_hooks = connection.describe_lifecycle_hooks( |
| 243 |
AutoScalingGroupName=asg_name, |
| 244 |
) |
| 245 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 246 |
module.fail_json_aws(e, msg="Failed to get Lifecycle Hooks") |
| 247 |
|
| 248 |
for hook in all_hooks["LifecycleHooks"]: |
| 249 |
if hook["LifecycleHookName"] == lch_name: |
| 250 |
lch_params = { |
| 251 |
"LifecycleHookName": lch_name, |
| 252 |
"AutoScalingGroupName": asg_name, |
| 253 |
} |
| 254 |
|
| 255 |
try: |
| 256 |
if module.check_mode: |
| 257 |
module.exit_json( |
| 258 |
changed=True, msg="Would have deleted AutoScalingGroup Lifecycle Hook if not in check_mode." |
| 259 |
) |
| 260 |
connection.delete_lifecycle_hook(**lch_params) |
| 261 |
return_object["changed"] = True |
| 262 |
return_object["lifecycle_hook_removed"] = { |
| 263 |
"LifecycleHookName": lch_name, |
| 264 |
"AutoScalingGroupName": asg_name, |
| 265 |
} |
| 266 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 267 |
module.fail_json_aws(e, msg="Failed to delete LifecycleHook") |
| 268 |
else: |
| 269 |
pass |
| 270 |
|
| 271 |
module.exit_json(**camel_dict_to_snake_dict(return_object)) |
| 272 |
|
| 273 |
|
| 274 |
def main(): |
| 275 |
argument_spec = dict( |
| 276 |
autoscaling_group_name=dict(required=True, type="str"), |
| 277 |
lifecycle_hook_name=dict(required=True, type="str"), |
| 278 |
transition=dict( |
| 279 |
type="str", choices=["autoscaling:EC2_INSTANCE_TERMINATING", "autoscaling:EC2_INSTANCE_LAUNCHING"] |
| 280 |
), |
| 281 |
role_arn=dict(type="str"), |
| 282 |
notification_target_arn=dict(type="str"), |
| 283 |
notification_meta_data=dict(type="str"), |
| 284 |
heartbeat_timeout=dict(type="int"), |
| 285 |
default_result=dict(default="ABANDON", choices=["ABANDON", "CONTINUE"]), |
| 286 |
state=dict(default="present", choices=["present", "absent"]), |
| 287 |
) |
| 288 |
|
| 289 |
module = AnsibleAWSModule( |
| 290 |
argument_spec=argument_spec, |
| 291 |
supports_check_mode=True, |
| 292 |
required_if=[["state", "present", ["transition"]]], |
| 293 |
) |
| 294 |
|
| 295 |
state = module.params.get("state") |
| 296 |
|
| 297 |
connection = module.client("autoscaling") |
| 298 |
|
| 299 |
if state == "present": |
| 300 |
create_lifecycle_hook(connection, module) |
| 301 |
elif state == "absent": |
| 302 |
delete_lifecycle_hook(connection, module) |
| 303 |
|
| 304 |
|
| 305 |
if __name__ == "__main__": |
| 306 |
main() |