From b38d0143373b85aae9dc701990cfdabd260f8b5d Mon Sep 17 00:00:00 2001 From: mandar242 Date: Fri, 1 Apr 2022 16:58:20 -0700 Subject: [PATCH 1/4] WIP: add integration tests ec2_asg_lifecycle_hook --- plugins/modules/ec2_asg_lifecycle_hook.py | 54 ++++--- .../targets/ec2_asg_lifecycle_hook/aliases | 0 .../targets/ec2_asg_lifecycle_hook/inventory | 6 + .../targets/ec2_asg_lifecycle_hook/main.yml | 40 +++++ .../ec2_asg_lifecycle_hook/defaults/main.yml | 5 + .../tasks/create_update_delete.yml | 144 ++++++++++++++++++ .../tasks/env_cleanup.yml | 123 +++++++++++++++ .../tasks/env_setup.yml | 65 ++++++++ .../ec2_asg_lifecycle_hook/tasks/main.yml | 40 +++++ .../targets/ec2_asg_lifecycle_hook/runme.sh | 12 ++ 10 files changed, 468 insertions(+), 21 deletions(-) create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/aliases create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/inventory create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/main.yml create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_cleanup.yml create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml create mode 100644 tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/main.yml create mode 100755 tests/integration/targets/ec2_asg_lifecycle_hook/runme.sh diff --git a/plugins/modules/ec2_asg_lifecycle_hook.py b/plugins/modules/ec2_asg_lifecycle_hook.py index fbdc4a3150d..9e3e185531b 100644 --- a/plugins/modules/ec2_asg_lifecycle_hook.py +++ b/plugins/modules/ec2_asg_lifecycle_hook.py @@ -100,16 +100,17 @@ ''' -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule try: import botocore except ImportError: pass # handled by AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + def create_lifecycle_hook(connection, module): - changed = False lch_name = module.params.get('lifecycle_hook_name') asg_name = module.params.get('autoscaling_group_name') @@ -120,6 +121,9 @@ def create_lifecycle_hook(connection, module): heartbeat_timeout = module.params.get('heartbeat_timeout') default_result = module.params.get('default_result') + return_object = {} + return_object['changed'] = False + lch_params = { 'LifecycleHookName': lch_name, 'AutoScalingGroupName': asg_name, @@ -150,23 +154,30 @@ def create_lifecycle_hook(connection, module): module.fail_json_aws(e, msg="Failed to get Lifecycle Hook") if not existing_hook: - changed = True - else: - # GlobalTimeout is not configurable, but exists in response. - # Removing it helps to compare both dicts in order to understand - # what changes were done. - del(existing_hook[0]['GlobalTimeout']) - added, removed, modified, same = dict_compare(lch_params, existing_hook[0]) - if added or removed or modified: - changed = True - - if changed: try: + return_object['changed'] = True connection.put_lifecycle_hook(**lch_params) + return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks( + AutoScalingGroupName=asg_name, LifecycleHookNames=[lch_name])['LifecycleHooks'] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to create LifecycleHook") - return(changed) + else: + added, removed, modified, same = dict_compare(lch_params, existing_hook[0]) + if modified: + # GlobalTimeout is not configurable, but exists in response. + # Removing it helps to compare both dicts in order to understand + # what changes were done. + del(existing_hook[0]['GlobalTimeout']) + try: + return_object['changed'] = True + connection.put_lifecycle_hook(**lch_params) + return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks( + AutoScalingGroupName=asg_name, LifecycleHookNames=[lch_name])['LifecycleHooks'] + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg="Failed to create LifecycleHook") + + module.exit_json(**camel_dict_to_snake_dict(return_object)) def dict_compare(d1, d2): @@ -186,11 +197,13 @@ def dict_compare(d1, d2): def delete_lifecycle_hook(connection, module): - changed = False lch_name = module.params.get('lifecycle_hook_name') asg_name = module.params.get('autoscaling_group_name') + return_object = {} + return_object['changed'] = False + try: all_hooks = connection.describe_lifecycle_hooks( AutoScalingGroupName=asg_name @@ -207,13 +220,14 @@ def delete_lifecycle_hook(connection, module): try: connection.delete_lifecycle_hook(**lch_params) - changed = True + return_object['changed'] = True + return_object['lifecycle_hook_removed'] = {'LifecycleHookName': lch_name, 'AutoScalingGroupName': asg_name} except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to delete LifecycleHook") else: pass - return(changed) + module.exit_json(**camel_dict_to_snake_dict(return_object)) def main(): @@ -238,11 +252,9 @@ def main(): changed = False if state == 'present': - changed = create_lifecycle_hook(connection, module) + create_lifecycle_hook(connection, module) elif state == 'absent': - changed = delete_lifecycle_hook(connection, module) - - module.exit_json(changed=changed) + delete_lifecycle_hook(connection, module) if __name__ == '__main__': diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/aliases b/tests/integration/targets/ec2_asg_lifecycle_hook/aliases new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/inventory b/tests/integration/targets/ec2_asg_lifecycle_hook/inventory new file mode 100644 index 00000000000..2081f49e60f --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/inventory @@ -0,0 +1,6 @@ +[tests] +create_update_delete + +[all:vars] +ansible_connection=local +ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml new file mode 100644 index 00000000000..5ee0592b2ca --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml @@ -0,0 +1,40 @@ +--- +# Beware: most of our tests here are run in parallel. +# To add new tests you'll need to add a new host to the inventory and a matching +# '{{ inventory_hostname }}'.yml file in roles/ec2_asg/tasks/ + + +# Prepare the VPC and figure out which AMI to use +- hosts: all + gather_facts: no + tasks: + - module_defaults: + group/aws: + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token | default(omit) }}" + region: "{{ aws_region }}" + vars: + # We can't just use "run_once" because the facts don't propagate when + # running an 'include' that was run_once + setup_run_once: yes + block: + - include_role: + name: 'ec2_asg_lifecycle_hook' + tasks_from: env_setup.yml + rescue: + - include_role: + name: 'ec2_asg_lifecycle_hook' + tasks_from: env_cleanup.yml + run_once: yes + - fail: + msg: 'Environment preparation failed' + run_once: yes + +# VPC should get cleaned up once all hosts have run +- hosts: all + gather_facts: no + strategy: free + serial: 6 + roles: + - ec2_asg_lifecycle_hook diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml new file mode 100644 index 00000000000..be66f56f2d3 --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml @@ -0,0 +1,5 @@ +--- +# defaults file for ec2_asg +# Amazon Linux 2 AMI 2019.06.12 (HVM), GP2 Volume Type +ec2_ami_name: 'amzn2-ami-hvm-2.0.20190612-x86_64-gp2' +load_balancer_name: "{{ tiny_prefix }}-lb" diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml new file mode 100644 index 00000000000..e50566f04d8 --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml @@ -0,0 +1,144 @@ +--- + +- name: Test create/update/delete asg_lifecycle_hooks + + block: + + #---------------------------------------------------------------------- + - name: create a launch configuration + ec2_lc: + name: "{{ resource_prefix }}-lc" + image_id: "{{ ec2_ami_image }}" + region: "{{ aws_region }}" + instance_type: t2.micro + assign_public_ip: yes + register: create_lc + + - name: ensure that lc is created + assert: + that: + - create_lc is changed + - create_lc.failed is false + + #---------------------------------------------------------------------- + - name: create a AutoScalingGroup + ec2_asg: + name: "{{ resource_prefix }}-asg" + launch_config_name: "{{ resource_prefix }}-lc" + health_check_period: 60 + health_check_type: ELB + replace_all_instances: yes + min_size: 1 + max_size: 1 + desired_capacity: 1 + region: "{{ aws_region }}" + register: create_asg + + - name: ensure that AutoScalingGroup is created + assert: + that: + - create_asg is changed + - create_asg.failed is false + - '"autoscaling:CreateAutoScalingGroup" in create_asg.resource_actions' + + #---------------------------------------------------------------------- + + - name: Create lifecycle hook + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + transition: autoscaling:EC2_INSTANCE_LAUNCHING + heartbeat_timeout: 7000 + default_result: ABANDON + state: present + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_info" in output' + - output.lifecycle_hook_info[0].heartbeat_timeout == 7000 + + - name: Create lifecycle hook - Idempotency + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + transition: autoscaling:EC2_INSTANCE_LAUNCHING + heartbeat_timeout: 7000 + default_result: ABANDON + state: present + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_info" not in output' + + - name: Update lifecycle hook + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + transition: autoscaling:EC2_INSTANCE_LAUNCHING + heartbeat_timeout: 6000 + default_result: ABANDON + state: present + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_info" in output' + - output.lifecycle_hook_info[0].heartbeat_timeout == 6000 + + - name: Update lifecycle hook - Idempotency + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + transition: autoscaling:EC2_INSTANCE_LAUNCHING + heartbeat_timeout: 6000 + default_result: ABANDON + state: present + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_info" not in output' + + always: + + - name: Delete lifecycle hook + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + state: absent + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_removed" in output' + + - name: Delete lifecycle hook - Idempotency + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + state: absent + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_removed" not in output' diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_cleanup.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_cleanup.yml new file mode 100644 index 00000000000..3b4ee869b42 --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_cleanup.yml @@ -0,0 +1,123 @@ +- name: kill asg + ec2_asg: + name: "{{ resource_prefix }}-asg" + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +# Remove the testing dependencies +- name: remove target group + elb_target_group: + name: "{{ item }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + loop: + - "{{ tg1_name }}" + - "{{ tg2_name }}" + +- name: remove the load balancer + ec2_elb_lb: + name: "{{ load_balancer_name }}" + state: absent + security_group_ids: + - "{{ sg.group_id }}" + subnets: "{{ testing_subnet.subnet.id }}" + wait: true + connection_draining_timeout: 60 + listeners: + - protocol: http + load_balancer_port: 80 + instance_port: 80 + health_check: + ping_protocol: tcp + ping_port: 80 + ping_path: "/" + response_timeout: 5 + interval: 10 + unhealthy_threshold: 4 + healthy_threshold: 2 + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +- name: remove launch configs + ec2_lc: + name: "{{ item }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + loop: + - "{{ resource_prefix }}-lc" + +- name: delete launch template + ec2_launch_template: + name: "{{ resource_prefix }}-lt" + state: absent + register: del_lt + retries: 10 + until: del_lt is not failed + ignore_errors: true + +- name: remove the security group + ec2_group: + name: "{{ resource_prefix }}-sg" + description: a security group for ansible tests + vpc_id: "{{ testing_vpc.vpc.id }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +- name: remove routing rules + ec2_vpc_route_table: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + tags: + created: "{{ resource_prefix }}-route" + routes: + - dest: 0.0.0.0/0 + gateway_id: "{{ igw.gateway_id }}" + subnets: + - "{{ testing_subnet.subnet.id }}" + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +- name: remove internet gateway + ec2_vpc_igw: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +- name: remove the subnet + ec2_vpc_subnet: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.55.77.0/24 + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 + +- name: remove the VPC + ec2_vpc_net: + name: "{{ resource_prefix }}-vpc" + cidr_block: 10.55.77.0/24 + state: absent + register: removed + until: removed is not failed + ignore_errors: true + retries: 10 diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml new file mode 100644 index 00000000000..aee843ab43a --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml @@ -0,0 +1,65 @@ +- name: Run ec2_asg integration tests. + + block: + + # ============================================================ + + - name: Find AMI to use + ec2_ami_info: + owners: 'amazon' + filters: + name: '{{ ec2_ami_name }}' + register: ec2_amis + - set_fact: + ec2_ami_image: '{{ ec2_amis.images[0].image_id }}' + + # Set up the testing dependencies: VPC, subnet, security group, and two launch configurations + - name: Create VPC for use in testing + ec2_vpc_net: + name: "{{ resource_prefix }}-vpc" + cidr_block: 10.55.77.0/24 + tenancy: default + register: testing_vpc + + - name: Create internet gateway for use in testing + ec2_vpc_igw: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: present + register: igw + + - name: Create subnet for use in testing + ec2_vpc_subnet: + state: present + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.55.77.0/24 + az: "{{ aws_region }}a" + resource_tags: + Name: "{{ resource_prefix }}-subnet" + register: testing_subnet + + - name: create routing rules + ec2_vpc_route_table: + vpc_id: "{{ testing_vpc.vpc.id }}" + tags: + created: "{{ resource_prefix }}-route" + routes: + - dest: 0.0.0.0/0 + gateway_id: "{{ igw.gateway_id }}" + subnets: + - "{{ testing_subnet.subnet.id }}" + + - name: create a security group with the vpc created in the ec2_setup + ec2_group: + name: "{{ resource_prefix }}-sg" + description: a security group for ansible tests + vpc_id: "{{ testing_vpc.vpc.id }}" + rules: + - proto: tcp + from_port: 22 + to_port: 22 + cidr_ip: 0.0.0.0/0 + - proto: tcp + from_port: 80 + to_port: 80 + cidr_ip: 0.0.0.0/0 + register: sg diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/main.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/main.yml new file mode 100644 index 00000000000..16442c7fa7c --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/main.yml @@ -0,0 +1,40 @@ +--- +# Beware: most of our tests here are run in parallel. +# To add new tests you'll need to add a new host to the inventory and a matching +# '{{ inventory_hostname }}'.yml file in roles/ec2_asg_lifecycle_hook/tasks/ + +- name: "Wrap up all tests and setup AWS credentials" + module_defaults: + group/aws: + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token | default(omit) }}" + region: "{{ aws_region }}" + aws_config: + retries: + # Unfortunately AWSRetry doesn't support paginators and boto3's paginators + # don't support any configuration of the delay between retries. + max_attempts: 20 + collections: + - community.aws + block: + - debug: + msg: "{{ inventory_hostname }} start: {{ lookup('pipe','date') }}" + - include_tasks: '{{ inventory_hostname }}.yml' + - debug: + msg: "{{ inventory_hostname }} finish: {{ lookup('pipe','date') }}" + + always: + - set_fact: + _role_complete: True + - vars: + completed_hosts: '{{ ansible_play_hosts_all | map("extract", hostvars, "_role_complete") | list | select("defined") | list | length }}' + hosts_in_play: '{{ ansible_play_hosts_all | length }}' + debug: + msg: "{{ completed_hosts }} of {{ hosts_in_play }} complete" + - include_tasks: env_cleanup.yml + vars: + completed_hosts: '{{ ansible_play_hosts_all | map("extract", hostvars, "_role_complete") | list | select("defined") | list | length }}' + hosts_in_play: '{{ ansible_play_hosts_all | length }}' + when: + - completed_hosts == hosts_in_play diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/runme.sh b/tests/integration/targets/ec2_asg_lifecycle_hook/runme.sh new file mode 100755 index 00000000000..aa324772bbe --- /dev/null +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/runme.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# +# Beware: most of our tests here are run in parallel. +# To add new tests you'll need to add a new host to the inventory and a matching +# '{{ inventory_hostname }}'.yml file in roles/ec2_instance/tasks/ + + +set -eux + +export ANSIBLE_ROLES_PATH=../ + +ansible-playbook main.yml -i inventory "$@" From aeba0efdf084ef811afa4edeaab79b33c8022284 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Tue, 5 Apr 2022 21:27:15 -0700 Subject: [PATCH 2/4] Integration test aws creds fix --- tests/integration/targets/ec2_asg_lifecycle_hook/aliases | 1 + tests/integration/targets/ec2_asg_lifecycle_hook/main.yml | 2 +- .../roles/ec2_asg_lifecycle_hook/defaults/main.yml | 2 +- .../ec2_asg_lifecycle_hook/tasks/create_update_delete.yml | 8 +++----- .../roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/aliases b/tests/integration/targets/ec2_asg_lifecycle_hook/aliases index e69de29bb2d..4ef4b2067d0 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/aliases +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/aliases @@ -0,0 +1 @@ +cloud/aws diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml index 5ee0592b2ca..5b7c3375303 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/main.yml @@ -1,7 +1,7 @@ --- # Beware: most of our tests here are run in parallel. # To add new tests you'll need to add a new host to the inventory and a matching -# '{{ inventory_hostname }}'.yml file in roles/ec2_asg/tasks/ +# '{{ inventory_hostname }}'.yml file in roles/ec2_asg_lifecycle_hook/tasks/ # Prepare the VPC and figure out which AMI to use diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml index be66f56f2d3..d24591f8833 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/defaults/main.yml @@ -1,5 +1,5 @@ --- -# defaults file for ec2_asg +# defaults file for ec2_asg_lifecycle_hook # Amazon Linux 2 AMI 2019.06.12 (HVM), GP2 Volume Type ec2_ami_name: 'amzn2-ami-hvm-2.0.20190612-x86_64-gp2' load_balancer_name: "{{ tiny_prefix }}-lb" diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml index e50566f04d8..ee38fddefcb 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml @@ -1,10 +1,8 @@ --- - -- name: Test create/update/delete asg_lifecycle_hooks +- name: Test create/update/delete AutoScalingGroups Lifecycle Hooks with ec2_asg_lifecycle_hook block: - - #---------------------------------------------------------------------- + #---------------------------------------------------------------------- - name: create a launch configuration ec2_lc: name: "{{ resource_prefix }}-lc" @@ -141,4 +139,4 @@ that: - output is not changed - output is not failed - - '"lifecycle_hook_removed" not in output' + - '"lifecycle_hook_removed" not in output' \ No newline at end of file diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml index aee843ab43a..436330f0a8f 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/env_setup.yml @@ -1,4 +1,4 @@ -- name: Run ec2_asg integration tests. +- name: Run ec2_asg_lifecycle_hook integration tests. block: From 0f8ad042ebbddb45de32d641a6f6eeb470ccd2a4 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Fri, 8 Apr 2022 14:24:37 -0700 Subject: [PATCH 3/4] Add changelogs fragment, newline at EOF --- .../1048-ec2_asg_lifecycle_hook-add-integration-tests.yml | 2 ++ .../ec2_asg_lifecycle_hook/tasks/create_update_delete.yml | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml diff --git a/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml b/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml new file mode 100644 index 00000000000..2d8d66d71fd --- /dev/null +++ b/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_asg_lifecycle_hook - add integration tests (https://github.com/ansible-collections/community.aws/pull/1048). diff --git a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml index ee38fddefcb..7f627c35fb9 100644 --- a/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml +++ b/tests/integration/targets/ec2_asg_lifecycle_hook/roles/ec2_asg_lifecycle_hook/tasks/create_update_delete.yml @@ -111,8 +111,6 @@ - output is not failed - '"lifecycle_hook_info" not in output' - always: - - name: Delete lifecycle hook community.aws.ec2_asg_lifecycle_hook: region: "{{ aws_region }}" @@ -139,4 +137,4 @@ that: - output is not changed - output is not failed - - '"lifecycle_hook_removed" not in output' \ No newline at end of file + - '"lifecycle_hook_removed" not in output' From a57bb5771bdc1d0a47fdcf9943191bc32689d4e1 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Mon, 11 Apr 2022 17:02:35 -0700 Subject: [PATCH 4/4] Add return block, modify changelogs fragment --- ...g_lifecycle_hook-add-integration-tests.yml | 1 + plugins/modules/ec2_asg_lifecycle_hook.py | 36 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml b/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml index 2d8d66d71fd..a2b5c0be4ef 100644 --- a/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml +++ b/changelogs/fragments/1048-ec2_asg_lifecycle_hook-add-integration-tests.yml @@ -1,2 +1,3 @@ minor_changes: - ec2_asg_lifecycle_hook - add integration tests (https://github.com/ansible-collections/community.aws/pull/1048). +- ec2_asg_lifecycle_hook - module now returns info about Life Cycle Hook (https://github.com/ansible-collections/community.aws/pull/1048). diff --git a/plugins/modules/ec2_asg_lifecycle_hook.py b/plugins/modules/ec2_asg_lifecycle_hook.py index 9e3e185531b..713a147872f 100644 --- a/plugins/modules/ec2_asg_lifecycle_hook.py +++ b/plugins/modules/ec2_asg_lifecycle_hook.py @@ -97,7 +97,37 @@ ''' RETURN = ''' - +--- +auto_scaling_group_name: + description: The unique name of the auto scaling group + returned: success + type: str + sample: "myasg" +default_result: + description: Defines the action the Auto Scaling group should take when the lifecycle hook timeout elapses or if an unexpected failure occurs + returned: success + type: str + sample: CONTINUE +global_timeout: + description: The maximum time, in seconds, that an instance can remain in a Pending:Wait or Terminating:Wait state + returned: success + type: int + sample: 172800 +heartbeat_timeout: + description: The maximum time, in seconds, that can elapse before the lifecycle hook times out + returned: success + type: int + sample: 3600 +lifecycle_hook_name: + description: The name of the lifecycle hook + returned: success + type: str + sample: "mylifecyclehook" +lifecycle_transition: + description: The instance state to which lifecycle hook should be attached + returned: success + type: str + sample: "autoscaling:EC2_INSTANCE_LAUNCHING" ''' @@ -165,10 +195,6 @@ def create_lifecycle_hook(connection, module): else: added, removed, modified, same = dict_compare(lch_params, existing_hook[0]) if modified: - # GlobalTimeout is not configurable, but exists in response. - # Removing it helps to compare both dicts in order to understand - # what changes were done. - del(existing_hook[0]['GlobalTimeout']) try: return_object['changed'] = True connection.put_lifecycle_hook(**lch_params)