From 21f35bcbd788f9b813e885ad762073dfc656df0f Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Thu, 18 Aug 2022 18:53:59 -0400 Subject: [PATCH 01/13] Add hibernation_option to ec2_instance module Signed-off-by: GomathiselviS --- plugins/modules/ec2_instance.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 2125ab5a42a..095b4bf4dc0 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -244,6 +244,10 @@ - Whether to enable termination protection. - This module will not terminate an instance with termination protection active, it must be turned off first. type: bool + hibernation_option: + description: + - Indicates whether an instance is enabled for hibernation.. + type: bool cpu_credit_specification: description: - For T series instances, choose whether to allow increased charges to buy CPU credits if the default pool is depleted. @@ -1010,6 +1014,8 @@ def build_volume_spec(params): volumes = params.get('volumes') or [] for volume in volumes: if 'ebs' in volume: + if 'encrypted' in volume['ebs']: + volume['ebs']['encrypted'] = volume['ebs']['encrypted'] for int_value in ['volume_size', 'iops']: if int_value in volume['ebs']: volume['ebs'][int_value] = int(volume['ebs'][int_value]) @@ -1289,6 +1295,8 @@ def build_top_level_options(params): spec['InstanceInitiatedShutdownBehavior'] = params.get('instance_initiated_shutdown_behavior') if params.get('termination_protection') is not None: spec['DisableApiTermination'] = params.get('termination_protection') + if params.get('hibernation_option') is not None: + spec['HibernationOptions'] = {'Configured': True} if params.get('cpu_options') is not None: spec['CpuOptions'] = {} spec['CpuOptions']['ThreadsPerCore'] = params.get('cpu_options').get('threads_per_core') @@ -2020,6 +2028,7 @@ def main(): placement_group=dict(type='str'), instance_initiated_shutdown_behavior=dict(type='str', choices=['stop', 'terminate']), termination_protection=dict(type='bool'), + hibernation_option=dict(type=bool), detailed_monitoring=dict(type='bool'), instance_ids=dict(default=[], type='list', elements='str'), network=dict(default=None, type='dict'), From 67253ac257838a3bc93c9f1033cd9573de356c24 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Fri, 19 Aug 2022 16:08:40 -0400 Subject: [PATCH 02/13] Added changelog and integration tests --- .../972-ec2_instance-stop-hibernate.yml | 2 ++ plugins/modules/ec2_instance.py | 20 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/972-ec2_instance-stop-hibernate.yml diff --git a/changelogs/fragments/972-ec2_instance-stop-hibernate.yml b/changelogs/fragments/972-ec2_instance-stop-hibernate.yml new file mode 100644 index 00000000000..92ef8fd77e9 --- /dev/null +++ b/changelogs/fragments/972-ec2_instance-stop-hibernate.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_instance - Add hibernation_options and volumes->ebs->encrypted keys to support stop-hibernate instance (https://github.com/ansible-collections/amazon.aws/pull/972). diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 095b4bf4dc0..4cc047ee305 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -244,10 +244,13 @@ - Whether to enable termination protection. - This module will not terminate an instance with termination protection active, it must be turned off first. type: bool - hibernation_option: + hibernation_options: description: - - Indicates whether an instance is enabled for hibernation.. + - Indicates whether an instance is enabled for hibernation. + Refer U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html) + for Hibernation prerequisits. type: bool + default: False cpu_credit_specification: description: - For T series instances, choose whether to allow increased charges to buy CPU credits if the default pool is depleted. @@ -1295,8 +1298,15 @@ def build_top_level_options(params): spec['InstanceInitiatedShutdownBehavior'] = params.get('instance_initiated_shutdown_behavior') if params.get('termination_protection') is not None: spec['DisableApiTermination'] = params.get('termination_protection') - if params.get('hibernation_option') is not None: - spec['HibernationOptions'] = {'Configured': True} + if params.get('hibernation_options') is not None and params.get('volumes'): + for vol in params['volumes']: + if vol.get('ebs') and vol['ebs'].get('encrypted'): + spec['HibernationOptions'] = {'Configured': True} + else: + module.fail_json( + msg="Hibernation prerequisites not satisfied. Refer {0}".format( + "Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html") + ) if params.get('cpu_options') is not None: spec['CpuOptions'] = {} spec['CpuOptions']['ThreadsPerCore'] = params.get('cpu_options').get('threads_per_core') @@ -2028,7 +2038,7 @@ def main(): placement_group=dict(type='str'), instance_initiated_shutdown_behavior=dict(type='str', choices=['stop', 'terminate']), termination_protection=dict(type='bool'), - hibernation_option=dict(type=bool), + hibernation_options=dict(type=bool, default=False), detailed_monitoring=dict(type='bool'), instance_ids=dict(default=[], type='list', elements='str'), network=dict(default=None, type='dict'), From e0a63a46752a5e6584cd30915acf2bea29c84860 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Mon, 22 Aug 2022 10:14:56 -0400 Subject: [PATCH 03/13] Sanity fixes --- plugins/modules/ec2_instance.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 4cc047ee305..3d6e7be8cb9 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1304,8 +1304,8 @@ def build_top_level_options(params): spec['HibernationOptions'] = {'Configured': True} else: module.fail_json( - msg="Hibernation prerequisites not satisfied. Refer {0}".format( - "Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html") + msg="Hibernation prerequisites not satisfied. Refer {0}".format( + "Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html") ) if params.get('cpu_options') is not None: spec['CpuOptions'] = {} @@ -2038,7 +2038,7 @@ def main(): placement_group=dict(type='str'), instance_initiated_shutdown_behavior=dict(type='str', choices=['stop', 'terminate']), termination_protection=dict(type='bool'), - hibernation_options=dict(type=bool, default=False), + hibernation_options=dict(type='bool', default=False), detailed_monitoring=dict(type='bool'), instance_ids=dict(default=[], type='list', elements='str'), network=dict(default=None, type='dict'), From 1c83dd719e4d63c495c6df2a0d6243e2c1844d37 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Mon, 22 Aug 2022 10:22:46 -0400 Subject: [PATCH 04/13] Added version_added --- plugins/modules/ec2_instance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 3d6e7be8cb9..f91bc528764 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -251,6 +251,7 @@ for Hibernation prerequisits. type: bool default: False + version_added: 5.0.0 cpu_credit_specification: description: - For T series instances, choose whether to allow increased charges to buy CPU credits if the default pool is depleted. From 312e580e3a1d3ac97502034fbc7312f529db079b Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 23 Aug 2022 09:58:14 -0400 Subject: [PATCH 05/13] Use ec2_instance_info to gather info in tests --- plugins/modules/ec2_instance.py | 2 +- .../tasks/hibernation_options_config.yml | 149 ++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index f91bc528764..1ee38b7d665 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1306,7 +1306,7 @@ def build_top_level_options(params): else: module.fail_json( msg="Hibernation prerequisites not satisfied. Refer {0}".format( - "Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html") + "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html") ) if params.get('cpu_options') is not None: spec['CpuOptions'] = {} diff --git a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml new file mode 100644 index 00000000000..8bc6b01f797 --- /dev/null +++ b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml @@ -0,0 +1,149 @@ +- block: + - name: Create instance with hibernation config (check mode) + ec2_instance: + name: "{{ resource_prefix }}-stop-hibernation" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + hibernation_options: true + instance_type: "{{ ec2_instance_type }}" + volumes: + - device_name: /dev/sda1 + ebs: + delete_on_termination: true + encrypted: true + state: running + wait: yes + check_mode: yes + register: create_instance_check_mode_results + + - name: Check the returned value for the earlier task + assert: + that: + - create_instance_check_mode_results is changed + - create_instance_check_mode_results.spec.HibernationOptions.Configured == True + + - name: Create instance with hibernation config + ec2_instance: + name: "{{ resource_prefix }}-stop-hibernation" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + hibernation_options: true + instance_type: "{{ ec2_instance_type }}" + volumes: + - device_name: /dev/sda1 + ebs: + delete_on_termination: true + encrypted: true + state: running + wait: yes + register: create_instance_results + + - set_fact: + instance_id: '{{ create_instance_results.instances[0].instance_id }}' + + - name: Check return values of the create instance task + assert: + that: + - "{{ create_instance_results.instances | length }} > 0" + - "'{{ create_instance_results.instances.0.state.name }}' == 'running'" + - "'{{ create_instance_results.spec.HibernationOptions.Configured }}'" + + - name: Gather information about the instance to get the hibernation status + ec2_instance_info: + filters: + "tag:Name": "{{ resource_prefix }}-stop-hibernation" + register: instance_hibernation_status + + - name: Assert hibernation options is true + assert: + that: + - instance_hibernation_status.instances[0].hibernation_options.configured == true + + - name: Create instance with hibernation option (check mode) (idempotent) + ec2_instance: + name: "{{ resource_prefix }}-stop-hibernation" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + hibernation_options: true + instance_type: "{{ ec2_instance_type }}" + volumes: + - device_name: /dev/sda1 + ebs: + delete_on_termination: true + encrypted: true + state: running + wait: yes + check_mode: yes + register: create_instance_check_mode_results + + - name: Check the returned value for the earlier task + assert: + that: + - create_instance_check_mode_results is not changed + + - name: Create instance with hibernation options configured (idempotent) + ec2_instance: + name: "{{ resource_prefix }}-stop-hibernation" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + hibernation_options: true + instance_type: "{{ ec2_instance_type }}" + volumes: + - device_name: /dev/sda1 + ebs: + delete_on_termination: true + encrypted: true + state: running + wait: yes + register: create_instance_results + + - name: Check return values of the create instance task + assert: + that: + - "{{ not create_instance_results.changed }}" + - "{{ create_instance_results.instances | length }} > 0" + + - name: Create instance with hibernation options configured with unencrypted volume + ec2_instance: + name: "{{ resource_prefix }}-stop-hibernation-error" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + hibernation_options: true + instance_type: "{{ ec2_instance_type }}" + volumes: + - device_name: /dev/sda1 + ebs: + delete_on_termination: true + register: create_instance_results + failed_when: "'Hibernation prerequisites not satisfied' not in create_instance_results.msg" + + - name: Terminate the instance + ec2_instance: + filters: + tag:TestId: "{{ resource_prefix }}" + state: absent + + always: + + - name: Terminate instance + ec2_instance: + filters: + tag:TestId: "{{ resource_prefix }}" + state: absent + wait: false + ignore_errors: yes From 9644dffd8b0e508ad35935e0c191988ca1ff223a Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 23 Aug 2022 15:23:46 -0400 Subject: [PATCH 06/13] Make ec2_instance_hibernation a separate target for test --- .../targets/ec2_instance/inventory | 20 +++++ .../ec2_instance_hibernation_options/aliases | 2 + .../inventory | 6 ++ .../ec2_instance_hibernation_options/main.yml | 40 ++++++++++ .../meta/main.yml | 6 ++ .../defaults/main.yml | 21 +++++ .../files/assume-role-policy.json | 13 +++ .../meta/main.yml | 7 ++ .../tasks/env_cleanup.yml | 79 +++++++++++++++++++ .../tasks/env_setup.yml | 64 +++++++++++++++ .../tasks/hibernation_options_config.yml | 0 .../tasks/main.yml | 56 +++++++++++++ .../ec2_instance_hibernation_options/runme.sh | 9 +++ 13 files changed, 323 insertions(+) create mode 100644 tests/integration/targets/ec2_instance/inventory create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/aliases create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/inventory create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/main.yml create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml rename tests/integration/targets/{ec2_instance/roles/ec2_instance => ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options}/tasks/hibernation_options_config.yml (100%) create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml create mode 100755 tests/integration/targets/ec2_instance_hibernation_options/runme.sh diff --git a/tests/integration/targets/ec2_instance/inventory b/tests/integration/targets/ec2_instance/inventory new file mode 100644 index 00000000000..e063f5745c0 --- /dev/null +++ b/tests/integration/targets/ec2_instance/inventory @@ -0,0 +1,20 @@ +[tests] +instance_minimal +instance_multiple +checkmode_tests +termination_protection +ebs_optimized +block_devices +cpu_options +metadata_options +default_vpc_tests +external_resource_attach +instance_no_wait +iam_instance_role +tags_and_vpc_settings +security_group +state_config_updates + +[all:vars] +ansible_connection=local +ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/aliases b/tests/integration/targets/ec2_instance_hibernation_options/aliases new file mode 100644 index 00000000000..6a794c03bc1 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/aliases @@ -0,0 +1,2 @@ +cloud/aws +ec2_instance_info diff --git a/tests/integration/targets/ec2_instance_hibernation_options/inventory b/tests/integration/targets/ec2_instance_hibernation_options/inventory new file mode 100644 index 00000000000..8187631b284 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/inventory @@ -0,0 +1,6 @@ +[tests] +hibernation_options_config + +[all:vars] +ansible_connection=local +ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/main.yml new file mode 100644 index 00000000000..359b4bcacf4 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/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_instance/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_instance_hibernation_options' + tasks_from: env_setup.yml + rescue: + - include_role: + name: 'ec2_instance_hibernation_options' + 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: 7 + roles: + - ec2_instance_hibernation_options diff --git a/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml new file mode 100644 index 00000000000..b907b87853a --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml @@ -0,0 +1,6 @@ +# this just makes sure they're in the right place +dependencies: +- role: setup_ec2_facts +- role: setup_botocore_pip + vars: + boto3_version: "1.20.30" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml new file mode 100644 index 00000000000..e137e28de89 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml @@ -0,0 +1,21 @@ +--- +# defaults file for ec2_instance +ec2_instance_owner: 'integration-run-{{ resource_prefix }}' +ec2_instance_type: 't3.micro' +ec2_instance_tag_TestId: '{{ resource_prefix }}-{{ inventory_hostname }}' + +vpc_name: '{{ resource_prefix }}-vpc' +vpc_seed: '{{ resource_prefix }}' +vpc_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.0.0/16' + +subnet_a_az: '{{ ec2_availability_zone_names[0] }}' +subnet_a_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.32.0/24' +subnet_a_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.32.' +subnet_b_az: '{{ ec2_availability_zone_names[1] }}' +subnet_b_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.33.0/24' +subnet_b_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.33.' + +first_iam_role: "ansible-test-sts-{{ resource_prefix | hash('md5') }}-test-policy" +second_iam_role: "ansible-test-sts-{{ resource_prefix | hash('md5') }}-test-policy-2" +# Zuul resource prefixes are very long, and IAM roles can only be 64 characters +unique_id: "{{ resource_prefix | hash('md5') }}" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json new file mode 100644 index 00000000000..72413abdd38 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json @@ -0,0 +1,13 @@ +{ + "Version": "2008-10-17", + "Statement": [ + { + "Sid": "", + "Effect": "Allow", + "Principal": { + "Service": "ec2.amazonaws.com" + }, + "Action": "sts:AssumeRole" + } + ] +} diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml new file mode 100644 index 00000000000..ee6b1d5a3b0 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml @@ -0,0 +1,7 @@ +dependencies: +- role: setup_ec2_facts +- role: setup_botocore_pip + vars: + boto3_version: "1.20.30" +collections: +- amazon.aws diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml new file mode 100644 index 00000000000..bdd69fecc44 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml @@ -0,0 +1,79 @@ +- name: "remove Instances" + ec2_instance: + state: absent + filters: + vpc-id: "{{ testing_vpc.vpc.id }}" + wait: yes + ignore_errors: yes + retries: 10 + +- name: "remove ENIs" + ec2_eni_info: + filters: + vpc-id: "{{ testing_vpc.vpc.id }}" + register: enis + +- name: "delete all ENIs" + ec2_eni: + state: absent + eni_id: "{{ item.id }}" + register: removed + until: removed is not failed + with_items: "{{ enis.network_interfaces }}" + ignore_errors: yes + retries: 10 + +- name: "remove the security group" + ec2_group: + state: absent + name: "{{ resource_prefix }}-sg" + description: a security group for ansible tests + vpc_id: "{{ testing_vpc.vpc.id }}" + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: "remove the second security group" + ec2_group: + name: "{{ resource_prefix }}-sg-2" + description: a security group for ansible tests + vpc_id: "{{ testing_vpc.vpc.id }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: "remove subnet A" + ec2_vpc_subnet: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: "{{ subnet_a_cidr }}" + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: "remove subnet B" + ec2_vpc_subnet: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: "{{ subnet_b_cidr }}" + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: "remove the VPC" + ec2_vpc_net: + state: absent + name: "{{ vpc_name }}" + cidr_block: "{{ vpc_cidr }}" + tags: + Name: Ansible Testing VPC + tenancy: default + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml new file mode 100644 index 00000000000..4ec8bc49c06 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml @@ -0,0 +1,64 @@ +- run_once: '{{ setup_run_once | default("no") | bool }}' + block: + - name: "Create VPC for use in testing" + ec2_vpc_net: + state: present + name: "{{ vpc_name }}" + cidr_block: "{{ vpc_cidr }}" + tags: + Name: Ansible ec2_instance Testing VPC + tenancy: default + register: testing_vpc + + - name: "Create default subnet in zone A" + ec2_vpc_subnet: + state: present + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: "{{ subnet_a_cidr }}" + az: "{{ subnet_a_az }}" + resource_tags: + Name: "{{ resource_prefix }}-subnet-a" + register: testing_subnet_a + + - name: "Create secondary subnet in zone B" + ec2_vpc_subnet: + state: present + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: "{{ subnet_b_cidr }}" + az: "{{ subnet_b_az }}" + resource_tags: + Name: "{{ resource_prefix }}-subnet-b" + register: testing_subnet_b + + - name: "create a security group with the vpc" + ec2_group: + state: present + 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 + + - name: "create secondary security group with the vpc" + ec2_group: + name: "{{ resource_prefix }}-sg-2" + description: a secondary 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: sg2 diff --git a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml similarity index 100% rename from tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/hibernation_options_config.yml rename to tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml new file mode 100644 index 00000000000..65645d2fa08 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml @@ -0,0 +1,56 @@ +--- +# 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/ +# +# Please make sure you tag your instances with +# tags: +# "tag:TestId": "{{ ec2_instance_tag_TestId }}" +# And delete them based off that tag at the end of your specific set of tests +# +# ############################################################################### +# +# A Note about ec2 environment variable name preference: +# - EC2_URL -> AWS_URL +# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY +# - EC2_SECRET_KEY -> AWS_SECRET_ACCESS_KEY -> AWX_SECRET_KEY +# - EC2_REGION -> AWS_REGION +# + +- 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: + - amazon.aws + - 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: + - aws_cleanup + - completed_hosts == hosts_in_play diff --git a/tests/integration/targets/ec2_instance_hibernation_options/runme.sh b/tests/integration/targets/ec2_instance_hibernation_options/runme.sh new file mode 100755 index 00000000000..328e65b90df --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/runme.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# + + +set -eux + +export ANSIBLE_ROLES_PATH=../ + +ansible-playbook main.yml -i inventory "$@" From 36be68d5785dabdaf456f7c56596ce5284318d01 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Wed, 24 Aug 2022 08:15:43 -0400 Subject: [PATCH 07/13] Add ec2_instance to alias --- .../integration/targets/ec2_instance_hibernation_options/aliases | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/targets/ec2_instance_hibernation_options/aliases b/tests/integration/targets/ec2_instance_hibernation_options/aliases index 6a794c03bc1..9993c7be99a 100644 --- a/tests/integration/targets/ec2_instance_hibernation_options/aliases +++ b/tests/integration/targets/ec2_instance_hibernation_options/aliases @@ -1,2 +1,3 @@ cloud/aws ec2_instance_info +ec2_instance \ No newline at end of file From d8aa38dac090c7a4107ea08ec5406c4e3ec3b4cf Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 30 Aug 2022 13:55:35 -0400 Subject: [PATCH 08/13] Handle bool value --- plugins/modules/ec2_instance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 1ee38b7d665..0e42a2947f1 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1299,7 +1299,7 @@ def build_top_level_options(params): spec['InstanceInitiatedShutdownBehavior'] = params.get('instance_initiated_shutdown_behavior') if params.get('termination_protection') is not None: spec['DisableApiTermination'] = params.get('termination_protection') - if params.get('hibernation_options') is not None and params.get('volumes'): + if params.get('hibernation_options') and params.get('volumes'): for vol in params['volumes']: if vol.get('ebs') and vol['ebs'].get('encrypted'): spec['HibernationOptions'] = {'Configured': True} From bd764ffd6353ee0d4ed6595c9007ef8d23603f8b Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Wed, 7 Sep 2022 08:58:58 -0400 Subject: [PATCH 09/13] Removed unwanted lines --- plugins/modules/ec2_instance.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 0e42a2947f1..0999f5696b3 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1018,8 +1018,6 @@ def build_volume_spec(params): volumes = params.get('volumes') or [] for volume in volumes: if 'ebs' in volume: - if 'encrypted' in volume['ebs']: - volume['ebs']['encrypted'] = volume['ebs']['encrypted'] for int_value in ['volume_size', 'iops']: if int_value in volume['ebs']: volume['ebs'][int_value] = int(volume['ebs'][int_value]) From ff22f416780ca7d55346ffe9bc32a56e859c35cd Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 13 Sep 2022 11:43:34 -0400 Subject: [PATCH 10/13] Removed ec2_instance integration target --- .../targets/ec2_instance/inventory | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 tests/integration/targets/ec2_instance/inventory diff --git a/tests/integration/targets/ec2_instance/inventory b/tests/integration/targets/ec2_instance/inventory deleted file mode 100644 index e063f5745c0..00000000000 --- a/tests/integration/targets/ec2_instance/inventory +++ /dev/null @@ -1,20 +0,0 @@ -[tests] -instance_minimal -instance_multiple -checkmode_tests -termination_protection -ebs_optimized -block_devices -cpu_options -metadata_options -default_vpc_tests -external_resource_attach -instance_no_wait -iam_instance_role -tags_and_vpc_settings -security_group -state_config_updates - -[all:vars] -ansible_connection=local -ansible_python_interpreter="{{ ansible_playbook_python }}" From 1809fd0bb461d39cc73ade092fad5922a70d66bd Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 13 Sep 2022 11:55:12 -0400 Subject: [PATCH 11/13] use tiny_prefic in iam_role variable --- .../roles/ec2_instance_hibernation_options/defaults/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml index e137e28de89..48b36df4456 100644 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml +++ b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml @@ -15,7 +15,7 @@ subnet_b_az: '{{ ec2_availability_zone_names[1] }}' subnet_b_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.33.0/24' subnet_b_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.33.' -first_iam_role: "ansible-test-sts-{{ resource_prefix | hash('md5') }}-test-policy" -second_iam_role: "ansible-test-sts-{{ resource_prefix | hash('md5') }}-test-policy-2" +first_iam_role: "ansible-test-sts-{{ tiny_prefix }}-test-policy" +second_iam_role: "ansible-test-sts-{{ tiny_prefix }}-test-policy-2" # Zuul resource prefixes are very long, and IAM roles can only be 64 characters unique_id: "{{ resource_prefix | hash('md5') }}" From 036a0f7df994b2b04048f78462ce6bb36c1255e0 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Thu, 15 Sep 2022 15:46:58 -0400 Subject: [PATCH 12/13] Fix integration tests --- .../defaults/main.yml | 4 + .../inventory | 6 -- .../ec2_instance_hibernation_options/main.yml | 40 ---------- .../meta/main.yml | 3 + .../defaults/main.yml | 21 ----- .../files/assume-role-policy.json | 13 --- .../meta/main.yml | 7 -- .../tasks/env_cleanup.yml | 79 ------------------- .../tasks/env_setup.yml | 64 --------------- .../tasks/main.yml | 56 ------------- .../ec2_instance_hibernation_options/runme.sh | 9 --- .../main.yml} | 26 +++--- 12 files changed, 18 insertions(+), 310 deletions(-) create mode 100644 tests/integration/targets/ec2_instance_hibernation_options/defaults/main.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/inventory delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/main.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml delete mode 100644 tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml delete mode 100755 tests/integration/targets/ec2_instance_hibernation_options/runme.sh rename tests/integration/targets/ec2_instance_hibernation_options/{roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml => tasks/main.yml} (91%) diff --git a/tests/integration/targets/ec2_instance_hibernation_options/defaults/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/defaults/main.yml new file mode 100644 index 00000000000..1e51a336a63 --- /dev/null +++ b/tests/integration/targets/ec2_instance_hibernation_options/defaults/main.yml @@ -0,0 +1,4 @@ +--- +# defaults file for ec2_instance +ec2_instance_type: 't3.micro' +ec2_instance_tag_TestId: '{{ resource_prefix }}-instance-hibernation-options' diff --git a/tests/integration/targets/ec2_instance_hibernation_options/inventory b/tests/integration/targets/ec2_instance_hibernation_options/inventory deleted file mode 100644 index 8187631b284..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/inventory +++ /dev/null @@ -1,6 +0,0 @@ -[tests] -hibernation_options_config - -[all:vars] -ansible_connection=local -ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/main.yml deleted file mode 100644 index 359b4bcacf4..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/main.yml +++ /dev/null @@ -1,40 +0,0 @@ ---- -# 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/ - - -# 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_instance_hibernation_options' - tasks_from: env_setup.yml - rescue: - - include_role: - name: 'ec2_instance_hibernation_options' - 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: 7 - roles: - - ec2_instance_hibernation_options diff --git a/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml index b907b87853a..80a82ca0bac 100644 --- a/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml +++ b/tests/integration/targets/ec2_instance_hibernation_options/meta/main.yml @@ -1,6 +1,9 @@ # this just makes sure they're in the right place dependencies: - role: setup_ec2_facts +- role: setup_ec2_instance_env + vars: + ec2_instance_test_name: hibernation_options - role: setup_botocore_pip vars: boto3_version: "1.20.30" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml deleted file mode 100644 index 48b36df4456..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/defaults/main.yml +++ /dev/null @@ -1,21 +0,0 @@ ---- -# defaults file for ec2_instance -ec2_instance_owner: 'integration-run-{{ resource_prefix }}' -ec2_instance_type: 't3.micro' -ec2_instance_tag_TestId: '{{ resource_prefix }}-{{ inventory_hostname }}' - -vpc_name: '{{ resource_prefix }}-vpc' -vpc_seed: '{{ resource_prefix }}' -vpc_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.0.0/16' - -subnet_a_az: '{{ ec2_availability_zone_names[0] }}' -subnet_a_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.32.0/24' -subnet_a_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.32.' -subnet_b_az: '{{ ec2_availability_zone_names[1] }}' -subnet_b_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.33.0/24' -subnet_b_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.33.' - -first_iam_role: "ansible-test-sts-{{ tiny_prefix }}-test-policy" -second_iam_role: "ansible-test-sts-{{ tiny_prefix }}-test-policy-2" -# Zuul resource prefixes are very long, and IAM roles can only be 64 characters -unique_id: "{{ resource_prefix | hash('md5') }}" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json deleted file mode 100644 index 72413abdd38..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/files/assume-role-policy.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "Version": "2008-10-17", - "Statement": [ - { - "Sid": "", - "Effect": "Allow", - "Principal": { - "Service": "ec2.amazonaws.com" - }, - "Action": "sts:AssumeRole" - } - ] -} diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml deleted file mode 100644 index ee6b1d5a3b0..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/meta/main.yml +++ /dev/null @@ -1,7 +0,0 @@ -dependencies: -- role: setup_ec2_facts -- role: setup_botocore_pip - vars: - boto3_version: "1.20.30" -collections: -- amazon.aws diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml deleted file mode 100644 index bdd69fecc44..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_cleanup.yml +++ /dev/null @@ -1,79 +0,0 @@ -- name: "remove Instances" - ec2_instance: - state: absent - filters: - vpc-id: "{{ testing_vpc.vpc.id }}" - wait: yes - ignore_errors: yes - retries: 10 - -- name: "remove ENIs" - ec2_eni_info: - filters: - vpc-id: "{{ testing_vpc.vpc.id }}" - register: enis - -- name: "delete all ENIs" - ec2_eni: - state: absent - eni_id: "{{ item.id }}" - register: removed - until: removed is not failed - with_items: "{{ enis.network_interfaces }}" - ignore_errors: yes - retries: 10 - -- name: "remove the security group" - ec2_group: - state: absent - name: "{{ resource_prefix }}-sg" - description: a security group for ansible tests - vpc_id: "{{ testing_vpc.vpc.id }}" - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 - -- name: "remove the second security group" - ec2_group: - name: "{{ resource_prefix }}-sg-2" - description: a security group for ansible tests - vpc_id: "{{ testing_vpc.vpc.id }}" - state: absent - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 - -- name: "remove subnet A" - ec2_vpc_subnet: - state: absent - vpc_id: "{{ testing_vpc.vpc.id }}" - cidr: "{{ subnet_a_cidr }}" - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 - -- name: "remove subnet B" - ec2_vpc_subnet: - state: absent - vpc_id: "{{ testing_vpc.vpc.id }}" - cidr: "{{ subnet_b_cidr }}" - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 - -- name: "remove the VPC" - ec2_vpc_net: - state: absent - name: "{{ vpc_name }}" - cidr_block: "{{ vpc_cidr }}" - tags: - Name: Ansible Testing VPC - tenancy: default - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml deleted file mode 100644 index 4ec8bc49c06..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/env_setup.yml +++ /dev/null @@ -1,64 +0,0 @@ -- run_once: '{{ setup_run_once | default("no") | bool }}' - block: - - name: "Create VPC for use in testing" - ec2_vpc_net: - state: present - name: "{{ vpc_name }}" - cidr_block: "{{ vpc_cidr }}" - tags: - Name: Ansible ec2_instance Testing VPC - tenancy: default - register: testing_vpc - - - name: "Create default subnet in zone A" - ec2_vpc_subnet: - state: present - vpc_id: "{{ testing_vpc.vpc.id }}" - cidr: "{{ subnet_a_cidr }}" - az: "{{ subnet_a_az }}" - resource_tags: - Name: "{{ resource_prefix }}-subnet-a" - register: testing_subnet_a - - - name: "Create secondary subnet in zone B" - ec2_vpc_subnet: - state: present - vpc_id: "{{ testing_vpc.vpc.id }}" - cidr: "{{ subnet_b_cidr }}" - az: "{{ subnet_b_az }}" - resource_tags: - Name: "{{ resource_prefix }}-subnet-b" - register: testing_subnet_b - - - name: "create a security group with the vpc" - ec2_group: - state: present - 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 - - - name: "create secondary security group with the vpc" - ec2_group: - name: "{{ resource_prefix }}-sg-2" - description: a secondary 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: sg2 diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml deleted file mode 100644 index 65645d2fa08..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/main.yml +++ /dev/null @@ -1,56 +0,0 @@ ---- -# 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/ -# -# Please make sure you tag your instances with -# tags: -# "tag:TestId": "{{ ec2_instance_tag_TestId }}" -# And delete them based off that tag at the end of your specific set of tests -# -# ############################################################################### -# -# A Note about ec2 environment variable name preference: -# - EC2_URL -> AWS_URL -# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY -# - EC2_SECRET_KEY -> AWS_SECRET_ACCESS_KEY -> AWX_SECRET_KEY -# - EC2_REGION -> AWS_REGION -# - -- 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: - - amazon.aws - - 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: - - aws_cleanup - - completed_hosts == hosts_in_play diff --git a/tests/integration/targets/ec2_instance_hibernation_options/runme.sh b/tests/integration/targets/ec2_instance_hibernation_options/runme.sh deleted file mode 100755 index 328e65b90df..00000000000 --- a/tests/integration/targets/ec2_instance_hibernation_options/runme.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -# - - -set -eux - -export ANSIBLE_ROLES_PATH=../ - -ansible-playbook main.yml -i inventory "$@" diff --git a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml b/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml similarity index 91% rename from tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml rename to tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml index 8bc6b01f797..f1a15764f1e 100644 --- a/tests/integration/targets/ec2_instance_hibernation_options/roles/ec2_instance_hibernation_options/tasks/hibernation_options_config.yml +++ b/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml @@ -1,7 +1,13 @@ -- block: - - name: Create instance with hibernation config (check mode) +- 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 }}" + block: + - name: Create instance with termination protection (check mode) ec2_instance: - name: "{{ resource_prefix }}-stop-hibernation" + name: "{{ resource_prefix }}-hibernation-options" image_id: "{{ ec2_ami_id }}" tags: TestId: "{{ resource_prefix }}" @@ -27,7 +33,7 @@ - name: Create instance with hibernation config ec2_instance: - name: "{{ resource_prefix }}-stop-hibernation" + name: "{{ resource_prefix }}-hibernation-options" image_id: "{{ ec2_ami_id }}" tags: TestId: "{{ resource_prefix }}" @@ -136,14 +142,4 @@ ec2_instance: filters: tag:TestId: "{{ resource_prefix }}" - state: absent - - always: - - - name: Terminate instance - ec2_instance: - filters: - tag:TestId: "{{ resource_prefix }}" - state: absent - wait: false - ignore_errors: yes + state: absent \ No newline at end of file From 4a684a5dd724602475cab04638d02fd59b5cace2 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Mon, 19 Sep 2022 10:03:20 -0400 Subject: [PATCH 13/13] Fix resource name error in integration tests --- .../ec2_instance_hibernation_options/tasks/main.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml b/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml index f1a15764f1e..e6aace728ec 100644 --- a/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml +++ b/tests/integration/targets/ec2_instance_hibernation_options/tasks/main.yml @@ -5,7 +5,7 @@ security_token: "{{ security_token | default(omit) }}" region: "{{ aws_region }}" block: - - name: Create instance with termination protection (check mode) + - name: Create instance with hibernation option (check mode) ec2_instance: name: "{{ resource_prefix }}-hibernation-options" image_id: "{{ ec2_ami_id }}" @@ -63,7 +63,7 @@ - name: Gather information about the instance to get the hibernation status ec2_instance_info: filters: - "tag:Name": "{{ resource_prefix }}-stop-hibernation" + "tag:Name": "{{ resource_prefix }}-hibernation-options" register: instance_hibernation_status - name: Assert hibernation options is true @@ -73,7 +73,7 @@ - name: Create instance with hibernation option (check mode) (idempotent) ec2_instance: - name: "{{ resource_prefix }}-stop-hibernation" + name: "{{ resource_prefix }}-hibernation-options" image_id: "{{ ec2_ami_id }}" tags: TestId: "{{ resource_prefix }}" @@ -98,7 +98,7 @@ - name: Create instance with hibernation options configured (idempotent) ec2_instance: - name: "{{ resource_prefix }}-stop-hibernation" + name: "{{ resource_prefix }}-hibernation-options" image_id: "{{ ec2_ami_id }}" tags: TestId: "{{ resource_prefix }}" @@ -123,7 +123,7 @@ - name: Create instance with hibernation options configured with unencrypted volume ec2_instance: - name: "{{ resource_prefix }}-stop-hibernation-error" + name: "{{ resource_prefix }}-hibernation-options-error" image_id: "{{ ec2_ami_id }}" tags: TestId: "{{ resource_prefix }}"