From 279bbc980befe043745e47480857358e307a2215 Mon Sep 17 00:00:00 2001 From: luiseterc Date: Sat, 2 Jul 2022 00:04:38 +0200 Subject: [PATCH] Add source_version param to ec2_launch_template module (#239) Add source_version param to ec2_launch_template module SUMMARY Add support for Boto3.create_launch_template_version's source_version parameter. Accepted values: int (specific version) : Creates a new launch template using this version as the base, hence keeping all its parameters string ( 'latest') : Uses the latest found in list template_versions ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_launch_template.py ADDITIONAL INFORMATION Sanity tests passed for this module. Reviewed-by: Mark Chappell Reviewed-by: Markus Bergholz Reviewed-by: Joseph Torcasso --- .../239-launch_template-source-version.yml | 2 + plugins/modules/ec2_launch_template.py | 47 ++++++++++++++++--- .../ec2_launch_template/tasks/versions.yml | 18 +++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/239-launch_template-source-version.yml diff --git a/changelogs/fragments/239-launch_template-source-version.yml b/changelogs/fragments/239-launch_template-source-version.yml new file mode 100644 index 00000000000..3a7e291eddd --- /dev/null +++ b/changelogs/fragments/239-launch_template-source-version.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_launch_template - Adds support for specifying the ``source_version`` upon which template updates are based (https://github.com/ansible-collections/community.aws/pull/239). diff --git a/plugins/modules/ec2_launch_template.py b/plugins/modules/ec2_launch_template.py index 4f2d05e1630..9e7bcd7cda2 100644 --- a/plugins/modules/ec2_launch_template.py +++ b/plugins/modules/ec2_launch_template.py @@ -313,6 +313,14 @@ For any VPC other than Default, you must use I(security_group_ids). type: list elements: str + source_version: + description: > + The version number of the launch template version on which to base the new version. + The new version inherits the same launch parameters as the source version, except for parameters that you explicity specify. + Snapshots applied to the block device mapping are ignored when creating a new version unless they are explicitly included. + type: str + default: latest + version_added: 4.1.0 tags: type: dict description: @@ -569,12 +577,38 @@ def create_or_update(module, template_options): out['changed'] = False return out try: - resp = ec2.create_launch_template_version( - LaunchTemplateId=template['LaunchTemplateId'], - LaunchTemplateData=lt_data, - ClientToken=uuid4().hex, - aws_retry=True, - ) + if module.params.get('source_version') in (None, ''): + resp = ec2.create_launch_template_version( + LaunchTemplateId=template['LaunchTemplateId'], + LaunchTemplateData=lt_data, + ClientToken=uuid4().hex, + aws_retry=True, + ) + elif module.params.get('source_version') == 'latest': + resp = ec2.create_launch_template_version( + LaunchTemplateId=template['LaunchTemplateId'], + LaunchTemplateData=lt_data, + ClientToken=uuid4().hex, + SourceVersion=str(most_recent['VersionNumber']), + aws_retry=True, + ) + else: + try: + int(module.params.get('source_version')) + except ValueError: + module.fail_json(msg='source_version param was not a valid integer, got "{0}"'.format(module.params.get('source_version'))) + # get source template version + source_version = next((v for v in template_versions if v['VersionNumber'] == int(module.params.get('source_version'))), None) + if source_version is None: + module.fail_json(msg='source_version does not exist, got "{0}"'.format(module.params.get('source_version'))) + resp = ec2.create_launch_template_version( + LaunchTemplateId=template['LaunchTemplateId'], + LaunchTemplateData=lt_data, + ClientToken=uuid4().hex, + SourceVersion=str(source_version['VersionNumber']), + aws_retry=True, + ) + if module.params.get('default_version') in (None, ''): # no need to do anything, leave the existing version as default pass @@ -748,6 +782,7 @@ def main(): template_name=dict(aliases=['name']), template_id=dict(aliases=['id']), default_version=dict(default='latest'), + source_version=dict(default='latest') ) arg_spec.update(template_options) diff --git a/tests/integration/targets/ec2_launch_template/tasks/versions.yml b/tests/integration/targets/ec2_launch_template/tasks/versions.yml index b2e94c5c3c5..e666b64e4b2 100644 --- a/tests/integration/targets/ec2_launch_template/tasks/versions.yml +++ b/tests/integration/targets/ec2_launch_template/tasks/versions.yml @@ -51,6 +51,24 @@ - lt.default_version == 3 - lt.latest_version == 3 + - name: create new template version based on an old version + ec2_launch_template: + name: "{{ resource_prefix }}-simple" + cpu_options: + core_count: 1 + threads_per_core: 1 + source_version: 1 + register: lt + + - name: instance with cpu_options created with the right options + assert: + that: + - lt is success + - lt is changed + - lt.default_version == 4 + - lt.latest_version == 4 + - lt.latest_template.launch_template_data.instance_type == "c4.large" + always: - name: delete the template ec2_launch_template: