From 3c3698d79ce5f56cefd24efe98cc49e6330f4c29 Mon Sep 17 00:00:00 2001 From: Squirrel Date: Wed, 12 Apr 2023 15:45:13 +0800 Subject: [PATCH] Add version_description to ec2_launch_template (#1763) Add version_description to ec2_launch_template SUMMARY Add version_description to ec2_launch_template module, which allows user update the VersionDescription of a launch template. Fix #1762 ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_launch_template ADDITIONAL INFORMATION Reviewed-by: Markus Bergholz --- ...aunch-template-add-version-description.yml | 2 ++ plugins/modules/ec2_launch_template.py | 30 +++++++++++++------ .../ec2_launch_template/tasks/versions.yml | 15 ++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/1763-ec2-launch-template-add-version-description.yml diff --git a/changelogs/fragments/1763-ec2-launch-template-add-version-description.yml b/changelogs/fragments/1763-ec2-launch-template-add-version-description.yml new file mode 100644 index 00000000000..000c5994f42 --- /dev/null +++ b/changelogs/fragments/1763-ec2-launch-template-add-version-description.yml @@ -0,0 +1,2 @@ +minor_changes: + - ec2_launch_template - Add parameter ``version_description`` (https://github.com/ansible-collections/community.aws/pull/1763). diff --git a/plugins/modules/ec2_launch_template.py b/plugins/modules/ec2_launch_template.py index 17f345a2f7e..b807d3aa09f 100644 --- a/plugins/modules/ec2_launch_template.py +++ b/plugins/modules/ec2_launch_template.py @@ -38,6 +38,12 @@ - Which version should be the default when users spin up new instances based on this template? By default, the latest version will be made the default. type: str default: latest + version_description: + version_added: 5.5.0 + description: + - The description of a launch template version. + default: "" + type: str state: description: - Whether the launch template should exist or not. @@ -576,8 +582,10 @@ def create_or_update(module, template_options): template, template_versions = existing_templates(module) out['changed'] = True elif template and template_versions: - most_recent = sorted(template_versions, key=lambda x: x['VersionNumber'])[-1] - if lt_data == most_recent['LaunchTemplateData']: + most_recent = sorted(template_versions, key=lambda x: x["VersionNumber"])[-1] + if lt_data == most_recent["LaunchTemplateData"] and module.params["version_description"] == most_recent.get( + "VersionDescription", "" + ): out['changed'] = False return out try: @@ -586,6 +594,7 @@ def create_or_update(module, template_options): LaunchTemplateId=template['LaunchTemplateId'], LaunchTemplateData=lt_data, ClientToken=uuid4().hex, + VersionDescription=str(module.params["version_description"]), aws_retry=True, ) elif module.params.get('source_version') == 'latest': @@ -593,7 +602,8 @@ def create_or_update(module, template_options): LaunchTemplateId=template['LaunchTemplateId'], LaunchTemplateData=lt_data, ClientToken=uuid4().hex, - SourceVersion=str(most_recent['VersionNumber']), + SourceVersion=str(most_recent["VersionNumber"]), + VersionDescription=str(module.params["version_description"]), aws_retry=True, ) else: @@ -609,7 +619,8 @@ def create_or_update(module, template_options): LaunchTemplateId=template['LaunchTemplateId'], LaunchTemplateData=lt_data, ClientToken=uuid4().hex, - SourceVersion=str(source_version['VersionNumber']), + SourceVersion=str(source_version["VersionNumber"]), + VersionDescription=str(module.params["version_description"]), aws_retry=True, ) @@ -782,11 +793,12 @@ def main(): ) arg_spec = dict( - state=dict(choices=['present', 'absent'], default='present'), - template_name=dict(aliases=['name']), - template_id=dict(aliases=['id']), - default_version=dict(default='latest'), - source_version=dict(default='latest') + state=dict(choices=["present", "absent"], default="present"), + template_name=dict(aliases=["name"]), + template_id=dict(aliases=["id"]), + default_version=dict(default="latest"), + source_version=dict(default="latest"), + version_description=dict(default=""), ) 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 e666b64e4b2..a9e40cd0843 100644 --- a/tests/integration/targets/ec2_launch_template/tasks/versions.yml +++ b/tests/integration/targets/ec2_launch_template/tasks/versions.yml @@ -69,6 +69,21 @@ - lt.latest_version == 4 - lt.latest_template.launch_template_data.instance_type == "c4.large" + - name: update simple instance template + ec2_launch_template: + name: "{{ resource_prefix }}-simple" + version_description: "Fix something." + register: lt + + - name: instance with cpu_options created with the right options + assert: + that: + - lt is success + - lt is changed + - lt.default_version == 5 + - lt.latest_version == 5 + - lt.latest_template.version_description == "Fix something." + always: - name: delete the template ec2_launch_template: