diff --git a/changelogs/fragments/587-ec2_instance-default-instance-type-launch-template.yml b/changelogs/fragments/587-ec2_instance-default-instance-type-launch-template.yml new file mode 100644 index 00000000000..93545736656 --- /dev/null +++ b/changelogs/fragments/587-ec2_instance-default-instance-type-launch-template.yml @@ -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". diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 9eb99a3698a..4c61bfc64e0 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -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: @@ -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: @@ -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 @@ -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'), @@ -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'): diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt index d228fed498b..9950c296ab7 100644 --- a/tests/sanity/ignore-2.9.txt +++ b/tests/sanity/ignore-2.9.txt @@ -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