Skip to content

Commit

Permalink
Add version_description to ec2_launch_template (ansible-collections#1763
Browse files Browse the repository at this point in the history
)

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 ansible-collections#1762


ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ec2_launch_template
ADDITIONAL INFORMATION

Reviewed-by: Markus Bergholz <git@osuv.de>
  • Loading branch information
squirrel532 committed Apr 12, 2023
1 parent 3ce5718 commit 3c3698d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_launch_template - Add parameter ``version_description`` (https://github.com/ansible-collections/community.aws/pull/1763).
30 changes: 21 additions & 9 deletions plugins/modules/ec2_launch_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -586,14 +594,16 @@ 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':
resp = ec2.create_launch_template_version(
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:
Expand All @@ -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,
)

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/targets/ec2_launch_template/tasks/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3c3698d

Please sign in to comment.