diff --git a/changelogs/fragments/1060-ec2_asg_lifecycle_hook-add_check_mode_support.yml b/changelogs/fragments/1060-ec2_asg_lifecycle_hook-add_check_mode_support.yml new file mode 100644 index 00000000000..2bc99a142b4 --- /dev/null +++ b/changelogs/fragments/1060-ec2_asg_lifecycle_hook-add_check_mode_support.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_asg_lifecycle_hook - Added check_mode support (https://github.com/ansible-collections/community.aws/pull/1060). diff --git a/plugins/modules/ec2_asg_lifecycle_hook.py b/plugins/modules/ec2_asg_lifecycle_hook.py index 713a147872f..351bba5b84d 100644 --- a/plugins/modules/ec2_asg_lifecycle_hook.py +++ b/plugins/modules/ec2_asg_lifecycle_hook.py @@ -185,6 +185,8 @@ def create_lifecycle_hook(connection, module): if not existing_hook: try: + if module.check_mode: + module.exit_json(changed=True, msg="Would have created AutoScalingGroup Lifecycle Hook if not in check_mode.") return_object['changed'] = True connection.put_lifecycle_hook(**lch_params) return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks( @@ -196,6 +198,8 @@ def create_lifecycle_hook(connection, module): added, removed, modified, same = dict_compare(lch_params, existing_hook[0]) if modified: try: + if module.check_mode: + module.exit_json(changed=True, msg="Would have modified AutoScalingGroup Lifecycle Hook if not in check_mode.") return_object['changed'] = True connection.put_lifecycle_hook(**lch_params) return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks( @@ -245,6 +249,8 @@ def delete_lifecycle_hook(connection, module): } try: + if module.check_mode: + module.exit_json(changed=True, msg="Would have deleted AutoScalingGroup Lifecycle Hook if not in check_mode.") connection.delete_lifecycle_hook(**lch_params) return_object['changed'] = True return_object['lifecycle_hook_removed'] = {'LifecycleHookName': lch_name, 'AutoScalingGroupName': asg_name} @@ -269,8 +275,12 @@ def main(): state=dict(default='present', choices=['present', 'absent']) ) - module = AnsibleAWSModule(argument_spec=argument_spec, - required_if=[['state', 'present', ['transition']]]) + module = AnsibleAWSModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_if=[['state', 'present', ['transition']]], + ) + state = module.params.get('state') connection = module.client('autoscaling') 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 7f627c35fb9..5a4fa7af165 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 @@ -41,6 +41,25 @@ #---------------------------------------------------------------------- + - name: Create lifecycle hook - check_mode + 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 + check_mode: true + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_info" not in output' + - '"Would have created AutoScalingGroup Lifecycle Hook if not in check_mode" in output.msg' + - name: Create lifecycle hook community.aws.ec2_asg_lifecycle_hook: region: "{{ aws_region }}" @@ -76,6 +95,43 @@ - output is not failed - '"lifecycle_hook_info" not in output' + - name: Create lifecycle hook - check_mode (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 + check_mode: true + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_info" not in output' + + - name: Update lifecycle hook - check_mode + 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 + check_mode: true + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_info" not in output' + - '"Would have modified AutoScalingGroup Lifecycle Hook if not in check_mode." in output.msg' + - name: Update lifecycle hook community.aws.ec2_asg_lifecycle_hook: region: "{{ aws_region }}" @@ -111,6 +167,40 @@ - output is not failed - '"lifecycle_hook_info" not in output' + - name: Update lifecycle hook - check_mode (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 + check_mode: true + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_info" not in output' + + - name: Delete lifecycle hook - check_mode + community.aws.ec2_asg_lifecycle_hook: + region: "{{ aws_region }}" + autoscaling_group_name: "{{ resource_prefix }}-asg" + lifecycle_hook_name: "{{ resource_prefix }}-test-hook" + state: absent + check_mode: true + register: output + + - assert: + that: + - output is changed + - output is not failed + - '"lifecycle_hook_removed" not in output' + - '"Would have deleted AutoScalingGroup Lifecycle Hook if not in check_mode." in output.msg' + - name: Delete lifecycle hook community.aws.ec2_asg_lifecycle_hook: region: "{{ aws_region }}" @@ -138,3 +228,18 @@ - output is not changed - output is not failed - '"lifecycle_hook_removed" not in output' + + - name: Delete lifecycle hook - check_mode (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 + check_mode: true + register: output + + - assert: + that: + - output is not changed + - output is not failed + - '"lifecycle_hook_removed" not in output'