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: