Skip to content

Commit

Permalink
ec2_instance: Fix launch template condition, handle launch template -…
Browse files Browse the repository at this point in the history
… default value for instance_type (#587)

ec2_instance: Fix launch template condition, handle launch template - default value for instance_type

SUMMARY

The launch_template option in ec2_instance has a broken condition.
Also the launch_template option defaults the instance_type to t2.micro if not specified and ignores the instance_type specified in the launch_template as said in the issue #451.

Fixes

#451
#462

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

ec2_instance
ADDITIONAL INFORMATION

The change does not break existing functionality as tested by the integration test run locally.
Related to the condition fix in community.aws: ansible-collections/community.aws#111


Ran the following playbook to verify the change.
# create a launch template called "test-launch-template" 
    - name: create test launch template
      community.aws.ec2_launch_template:
        template_name: test-launch-template
        image_id: ami-002068ed284xxxxxx
        instance_type: t3a.small
        network_interfaces:
          - associate_public_ip_address: no
            delete_on_termination: yes
            device_index: 0
            groups:
              - sg-xxxxxxxxxxxxxxxxxx
            subnet_id: subnet-xxxxxxxxxxxxxxxxxx
        region: us-east-2
        block_device_mappings:
          - device_name: /dev/sdb
            ebs:
              volume_size: 5
              volume_type: gp3
              delete_on_termination: true
              encrypted: yes
          - device_name: /dev/sdc
            ebs:
              volume_size: 2
              volume_type: gp2
              delete_on_termination: true
              encrypted: no
        tags:
          ssome: tags

# launch a ec2 instance using launch template created earlier - launches t3a.small instance as expected
    - name: test launch template usage
      ec2_instance:
        wait: yes
        name: "test-instance-mk-t3a.small"
        launch_template:
          name: test-launch-template
        vpc_subnet_id: subnet-xxxxxxxxxxxxxxxxxx

# launch ec2 instance using launch template created earlier - override instance type to be launch to t3.xlarge
    - name: test launch template usage - override instance type
      ec2_instance:
        wait: yes
        name: "test-instance-mk-t3.xlarge"
        instance_type: t3.xlarge
        launch_template:
          name: test-launch-template
        vpc_subnet_id: subnet-xxxxxxxxxxxxxxxxxx

Reviewed-by: Jill R <None>
Reviewed-by: Markus Bergholz <git@osuv.de>
Reviewed-by: Emanuele Leopardi <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
  • Loading branch information
mandar242 committed Jan 25, 2022
1 parent 1175a4b commit 9a5a1eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bugfixes:
- ec2_instance - Add a condition to handle default ```instance_type``` value
for fix breaking on instance creation with launch template (https://github.com/ansible-collections/amazon.aws/pull/587).
deprecated_features:
- ec2_instance - The default value for ```instance_type``` has been deprecated,
in the future release you must set an instance_type or a launch_template".
17 changes: 13 additions & 4 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
description:
- Instance type to use for the instance, see U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html)
Only required when instance is not already present.
default: t2.micro
- If not specified, t2.micro will be used.
type: str
count:
description:
Expand Down Expand Up @@ -1232,7 +1232,7 @@ def build_top_level_options(params):

if params.get('launch_template') is not None:
spec['LaunchTemplate'] = {}
if not params.get('launch_template').get('id') or params.get('launch_template').get('name'):
if not params.get('launch_template').get('id') and not params.get('launch_template').get('name'):
module.fail_json(msg="Could not create instance with launch template. Either launch_template.name or launch_template.id parameters are required")

if params.get('launch_template').get('id') is not None:
Expand Down Expand Up @@ -1305,7 +1305,12 @@ def build_run_instance_spec(params):
spec['MaxCount'] = module.params.get('count')
spec['MinCount'] = module.params.get('count')

spec['InstanceType'] = params['instance_type']
if not module.params.get('launch_template'):
spec['InstanceType'] = params['instance_type'] if module.params.get('instance_type') else 't2.micro'

if module.params.get('launch_template') and module.params.get('instance_type'):
spec['InstanceType'] = params['instance_type']

return spec


Expand Down Expand Up @@ -1921,7 +1926,7 @@ def main():
exact_count=dict(type='int'),
image=dict(type='dict'),
image_id=dict(type='str'),
instance_type=dict(default='t2.micro', type='str'),
instance_type=dict(type='str'),
user_data=dict(type='str'),
tower_callback=dict(type='dict'),
ebs_optimized=dict(type='bool'),
Expand Down Expand Up @@ -1967,6 +1972,10 @@ def main():
],
supports_check_mode=True
)

if not module.params.get('instance_type') and not module.params.get('launch_template'):
module.deprecate("Default value instance_type has been deprecated, in the future you must set an instance_type or a launch_template",
date='2023-01-01', collection_name='amazon.aws')
result = dict()

if module.params.get('network'):
Expand Down
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ plugins/modules/ec2_vpc_dhcp_option.py pylint:ansible-deprecated-no-version # W
plugins/modules/ec2_vpc_igw_info.py pylint:ansible-deprecated-no-version # We use dates for deprecations, Ansible 2.9 only supports this for compatability
plugins/modules/ec2_vpc_endpoint.py pylint:ansible-deprecated-no-version
plugins/modules/ec2_vpc_endpoint_info.py pylint:ansible-deprecated-no-version
plugins/modules/ec2_instance.py pylint:ansible-deprecated-no-version # We use dates for deprecations, Ansible 2.9 only supports this for compatability

0 comments on commit 9a5a1eb

Please sign in to comment.