From 7c1b49fe793db253220b35da847ab53e0d0b9a90 Mon Sep 17 00:00:00 2001 From: "Saleh A. Saber" Date: Sun, 14 Mar 2021 14:26:11 +0000 Subject: [PATCH] Gather information about ASG lifecycle hooks (#233) * Gather information about ASG lifecycle hooks for community.aws.ec2_asg_info * add a changelog fragment to the change * use fail_json_aws Co-authored-by: Saleh Abbas Co-authored-by: Mark Chappell --- .../233-info-about-asg-lifecycle-hooks.yaml | 2 + plugins/modules/ec2_asg_info.py | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 changelogs/fragments/233-info-about-asg-lifecycle-hooks.yaml diff --git a/changelogs/fragments/233-info-about-asg-lifecycle-hooks.yaml b/changelogs/fragments/233-info-about-asg-lifecycle-hooks.yaml new file mode 100644 index 00000000000..ae6d23f8c84 --- /dev/null +++ b/changelogs/fragments/233-info-about-asg-lifecycle-hooks.yaml @@ -0,0 +1,2 @@ +minor_changes: + - ec2_asg_info - gather information about asg lifecycle hooks (https://github.com/ansible-collections/community.aws/pull/233). diff --git a/plugins/modules/ec2_asg_info.py b/plugins/modules/ec2_asg_info.py index 819bf6e5ab3..3c809e069be 100644 --- a/plugins/modules/ec2_asg_info.py +++ b/plugins/modules/ec2_asg_info.py @@ -143,6 +143,28 @@ returned: success type: str sample: "public-webapp-production-1" +lifecycle_hooks: + description: List of lifecycle hooks for the ASG. + returned: success + type: list + sample: [ + { + "AutoScalingGroupName": "public-webapp-production-1", + "DefaultResult": "ABANDON", + "GlobalTimeout": 172800, + "HeartbeatTimeout": 3600, + "LifecycleHookName": "instance-launch", + "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING" + }, + { + "AutoScalingGroupName": "public-webapp-production-1", + "DefaultResult": "ABANDON", + "GlobalTimeout": 172800, + "HeartbeatTimeout": 3600, + "LifecycleHookName": "instance-terminate", + "LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING" + } + ] load_balancer_names: description: List of load balancers names attached to the ASG. returned: success @@ -289,6 +311,25 @@ def find_asgs(conn, module, name=None, tags=None): ], "launch_config_name": "public-webapp-production-1", "launch_configuration_name": "public-webapp-production-1", + "lifecycle_hooks": + [ + { + "AutoScalingGroupName": "public-webapp-production-1", + "DefaultResult": "ABANDON", + "GlobalTimeout": 172800, + "HeartbeatTimeout": 3600, + "LifecycleHookName": "instance-launch", + "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING" + }, + { + "AutoScalingGroupName": "public-webapp-production-1", + "DefaultResult": "ABANDON", + "GlobalTimeout": 172800, + "HeartbeatTimeout": 3600, + "LifecycleHookName": "instance-terminate", + "LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING" + } + ], "load_balancer_names": ["public-webapp-production-lb"], "max_size": 4, "min_size": 2, @@ -381,6 +422,12 @@ def find_asgs(conn, module, name=None, tags=None): module.fail_json_aws(e, msg="Failed to describe Target Groups") else: asg['target_group_names'] = [] + # get asg lifecycle hooks if any + try: + asg_lifecyclehooks = conn.describe_lifecycle_hooks(AutoScalingGroupName=asg['auto_scaling_group_name']) + asg['lifecycle_hooks'] = asg_lifecyclehooks['LifecycleHooks'] + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg="Failed to fetch information about ASG lifecycle hooks") matched_asgs.append(asg) return matched_asgs