From b7d9feb7dc71e0b1e305ae8ad4d04b3038e31b80 Mon Sep 17 00:00:00 2001 From: Andrea Tartaglia Date: Fri, 9 Nov 2018 12:11:51 +0000 Subject: [PATCH] ec2_instance: ebs_optimized is not sub-option of 'network' (#48341) * ebs_optimized is not suboption of 'network' * Add Shaps as ec2_instance maintainer * Added workaround-backward compatible check for ebs_optimized * Added ebs_optimized test * CI fixes, dynamic select of ENA-enabled AMI --- .github/BOTMETA.yml | 1 + .../modules/cloud/amazon/ec2_instance.py | 10 ++++-- .../roles/ec2_instance/defaults/main.yml | 16 +++++++++ .../ec2_instance/tasks/ebs_optimized.yml | 34 +++++++++++++++++++ .../roles/ec2_instance/tasks/main.yml | 1 + .../ec2_instance/playbooks/version_fail.yml | 6 ++-- 6 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml index aa4a623fa4a..98e53ea893e 100644 --- a/.github/BOTMETA.yml +++ b/.github/BOTMETA.yml @@ -53,6 +53,7 @@ files: $modules/cloud/amazon/ec2_asg.py: $team_ansible s-hertel ryansb $modules/cloud/amazon/ec2_group.py: $team_ansible $modules/cloud/amazon/ec2_group_facts.py: willthames + $modules/cloud/amazon/ec2_instance.py: Shaps $modules/cloud/amazon/ec2_instance_facts.py: willthames $modules/cloud/amazon/ec2_key.py: $team_ansible $modules/cloud/amazon/ec2_lc.py: $team_ansible diff --git a/lib/ansible/modules/cloud/amazon/ec2_instance.py b/lib/ansible/modules/cloud/amazon/ec2_instance.py index db6230b4023..790a7dbcfed 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_instance.py +++ b/lib/ansible/modules/cloud/amazon/ec2_instance.py @@ -1111,7 +1111,10 @@ def build_top_level_options(params): spec['CreditSpecification'] = {'CpuCredits': params.get('cpu_credit_specification')} if params.get('tenancy') is not None: spec['Placement'] = {'Tenancy': params.get('tenancy')} - if (params.get('network') or {}).get('ebs_optimized') is not None: + if params.get('ebs_optimized') is not None: + spec['EbsOptimized'] = params.get('ebs_optimized') + elif (params.get('network') or {}).get('ebs_optimized') is not None: + # Backward compatibility for workaround described in https://github.com/ansible/ansible/issues/48159 spec['EbsOptimized'] = params['network'].get('ebs_optimized') if params.get('instance_initiated_shutdown_behavior'): spec['InstanceInitiatedShutdownBehavior'] = params.get('instance_initiated_shutdown_behavior') @@ -1597,6 +1600,9 @@ def main(): ) if module.params.get('network'): + if 'ebs_optimized' in module.params['network']: + module.deprecate("network.ebs_optimized is deprecated." + "Use the top level ebs_optimized parameter instead", 2.9) if module.params.get('network').get('interfaces'): if module.params.get('security_group'): module.fail_json(msg="Parameter network.interfaces can't be used with security_group") @@ -1646,7 +1652,7 @@ def main(): module.params['filters'] = filters if module.params.get('cpu_options') and not module.botocore_at_least('1.10.16'): - module.fail_json(msg="cpu_options is only supported with botocore >= 1.10.16") + module.fail_json(msg="cpu_options is only supported with botocore >= 1.10.16") existing_matches = find_instances(ec2, filters=module.params.get('filters')) changed = False diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml index 01b770e485c..4b6773a1d8f 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml +++ b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml @@ -18,3 +18,19 @@ ec2_ami_image: us-east-2: ami-9cbf9bf9 us-west-1: ami-7c280d1c us-west-2: ami-0c2aba6c +# We need to use ENA enabled AMIs to get EBS optimized instances. +ec2_ebs_optimized_ami_image: + ap-northeast-1: ami-00f9d04b3b3092052 + ap-northeast-2: ami-0c764df09c35858b8 + ap-south-1: ami-00796998f258969fd + ap-southeast-1: ami-085fd1bd447be68e8 + ap-southeast-2: ami-0b8dea0e70b969adc + ca-central-1: ami-05cac140c6a1fb960 + eu-central-1: ami-02ea8f348fa28c108 + eu-west-1: ami-0a5e707736615003c + eu-west-2: ami-017b0e29fac27906b + sa-east-1: ami-0160a8b6087883cb6 + us-east-1: ami-013be31976ca2c322 + us-east-2: ami-0350c5670171b5391 + us-west-1: ami-01beb64058d271bc4 + us-west-2: ami-061e7ebbc234015fe diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml new file mode 100644 index 00000000000..6cf6b8503c2 --- /dev/null +++ b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml @@ -0,0 +1,34 @@ +- name: set connection information for all tasks + set_fact: + aws_connection_info: &aws_connection_info + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token }}" + region: "{{ aws_region }}" + no_log: true + +- name: Make EBS optimized instance in the testing subnet of the test VPC + ec2_instance: + name: "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc" + image_id: "{{ ec2_ebs_optimized_ami_image[aws_region] }}" + tags: + TestId: "{{ resource_prefix }}" + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + ebs_optimized: true + instance_type: t3.nano + <<: *aws_connection_info + register: ebs_opt_in_vpc + +- name: Get ec2 instance facts + ec2_instance_facts: + filters: + "tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc" + "instance-state-name": "running" + <<: *aws_connection_info + register: ebs_opt_instance_fact + +- name: Assert instance is ebs_optimized + assert: + that: + - "{{ ebs_opt_instance_fact.instances.0.ebs_optimized }}" diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml index 4cae24f62a7..9f05b0976d8 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml +++ b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml @@ -97,6 +97,7 @@ - include_tasks: default_vpc_tests.yml - include_tasks: iam_instance_role.yml - include_tasks: checkmode_tests.yml + - include_tasks: ebs_optimized.yml # ============================================================ diff --git a/test/integration/targets/ec2_instance/playbooks/version_fail.yml b/test/integration/targets/ec2_instance/playbooks/version_fail.yml index b4ecfca31f3..517164d1971 100644 --- a/test/integration/targets/ec2_instance/playbooks/version_fail.yml +++ b/test/integration/targets/ec2_instance/playbooks/version_fail.yml @@ -13,7 +13,7 @@ security_token: "{{ security_token }}" region: "{{ aws_region }}" no_log: True - + - name: Include vars file in roles/ec2_instance/defaults/main.yml include_vars: file: 'roles/ec2_instance/defaults/main.yml' @@ -31,8 +31,8 @@ register: ec2_instance_cpu_options_creation ignore_errors: yes - - name: check that graceful error message is returned when creation with cpu_options and old botocore + - name: check that graceful error message is returned when creation with cpu_options and old botocore assert: that: - ec2_instance_cpu_options_creation.failed - - 'ec2_instance_cpu_options_creation.msg == "cpu_options is only supported with botocore >= 1.10.16"' + - 'ec2_instance_cpu_options_creation.msg == "cpu_options is only supported with botocore >= 1.10.16"'