Skip to content

Commit c036ff2

Browse files
committed
add support for template_tags and purge_template_tags
1 parent c21b9d0 commit c036ff2

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
minor_changes:
33
- ec2_launch_template - Refactor module to use shared code from ``amazon.aws.plugins.module_utils.ec2`` and update ``RETURN`` block (https://github.com/ansible-collections/community.aws/pull/2164).
4-
- ec2_launch_template - add the possibility to delete specific versions of a launch template using ``versions_to_delete`` (https://github.com/ansible-collections/community.aws/pull/2164).
5-
- ec2_launch_template - add the ``purge_tags`` option (https://github.com/ansible-collections/community.aws/pull/2164).
4+
- ec2_launch_template - Add the possibility to delete specific versions of a launch template using ``versions_to_delete`` (https://github.com/ansible-collections/community.aws/pull/2164).
5+
- ec2_launch_template - Add support for template tags via ``template_tags`` and ``purge_template_tags`` options (https://github.com/ansible-collections/community.aws/issues/176).

plugins/modules/ec2_launch_template.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,29 @@
388388
- Wether the instance tags are availble (V(enabled)) via metadata endpoint or not (V(disabled)).
389389
choices: [enabled, disabled]
390390
default: 'disabled'
391-
notes:
392-
- Support for O(purge_tags) was added in release 8.1.0.
391+
template_tags:
392+
description:
393+
- A dictionary representing the tags to be applied to the launch template.
394+
- If the O(template_tags) parameter is not set then tags will not be modified.
395+
type: dict
396+
required: false
397+
purge_template_tags:
398+
description:
399+
- If O(purge_template_tags=true) and O(template_tags) is set, existing tags will be purged
400+
from the resource to match exactly what is defined by O(template_tags) parameter.
401+
- If the O(template_tags) parameter is not set then tags will not be modified, even
402+
if O(purge_template_tags=True).
403+
- Tag keys beginning with V(aws:) are reserved by Amazon and can not be
404+
modified. As such they will be ignored for the purposes of the
405+
O(purge_template_tags) parameter. See the Amazon documentation for more information
406+
U(https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions).
407+
type: bool
408+
default: true
409+
required: false
393410
extends_documentation_fragment:
394411
- amazon.aws.common.modules
395412
- amazon.aws.region.modules
396413
- amazon.aws.boto3
397-
- amazon.aws.tags
398414
"""
399415

400416
EXAMPLES = r"""
@@ -701,8 +717,9 @@
701717
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import determine_iam_arn_from_name
702718
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags
703719
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import modify_launch_template
704-
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import is_ansible_aws_error_code
705720
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import AnsibleAWSError
721+
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import is_ansible_aws_error_code
722+
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
706723
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
707724
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters
708725

@@ -742,6 +759,12 @@ def find_existing(client, module: AnsibleAWSModule) -> Tuple[Optional[Dict[str,
742759
def params_to_launch_data(
743760
template_params: Dict[str, Any], iam_instance_profile_arn: Optional[str] = None
744761
) -> Dict[str, Any]:
762+
if template_params.get("tags"):
763+
tag_list = ansible_dict_to_boto3_tag_list(template_params.get("tags"))
764+
template_params["tag_specifications"] = [
765+
{"resource_type": r_type, "tags": tag_list} for r_type in ("instance", "volume")
766+
]
767+
del template_params["tags"]
745768
if iam_instance_profile_arn:
746769
template_params["iam_instance_profile"] = {"arn": iam_instance_profile_arn}
747770
for interface in template_params.get("network_interfaces") or []:
@@ -801,7 +824,7 @@ def validate_version_deletion(
801824

802825
if default_version_to_set and default_version_to_set not in remaining_versions:
803826
module.fail_json(
804-
msg=f"Could not set version '{default_version_to_set}' as default,"
827+
msg=f"Could not set version '{default_version_to_set}' as default, "
805828
"the launch template version was not found for the specified launch template id '{launch_template_id}'."
806829
)
807830
else:
@@ -967,8 +990,8 @@ def ensure_present(
967990
existing_versions: List[Dict[str, Any]],
968991
) -> None:
969992
template_name = module.params["template_name"]
970-
tags = module.params["tags"]
971-
purge_tags = module.params["purge_tags"]
993+
template_tags = module.params["template_tags"]
994+
purge_template_tags = module.params["purge_template_tags"]
972995
version_description = module.params.get("version_description")
973996
iam_instance_profile = module.params.get("iam_instance_profile")
974997
if iam_instance_profile:
@@ -988,7 +1011,7 @@ def ensure_present(
9881011
client,
9891012
launch_template_name=template_name,
9901013
launch_template_data=launch_template_data,
991-
tags=tags,
1014+
tags=template_tags,
9921015
ClientToken=uuid4().hex,
9931016
VersionDescription=version_description,
9941017
)
@@ -1020,7 +1043,12 @@ def ensure_present(
10201043
)
10211044
# Ensure tags
10221045
changed |= ensure_ec2_tags(
1023-
client, module, launch_template_id, resource_type="launch-template", tags=tags, purge_tags=purge_tags
1046+
client,
1047+
module,
1048+
launch_template_id,
1049+
resource_type="launch-template",
1050+
tags=template_tags,
1051+
purge_tags=purge_template_tags,
10241052
)
10251053

10261054
module.exit_json(changed=changed, **format_module_output(client, module))
@@ -1132,6 +1160,7 @@ def main():
11321160
ram_disk_id=dict(),
11331161
security_group_ids=dict(type="list", elements="str"),
11341162
security_groups=dict(type="list", elements="str"),
1163+
tags=dict(type="dict", aliases=["resource_tags"]),
11351164
user_data=dict(),
11361165
)
11371166

@@ -1143,8 +1172,8 @@ def main():
11431172
source_version=dict(default="latest"),
11441173
version_description=dict(default=""),
11451174
iam_instance_profile=dict(),
1146-
tags=dict(type="dict", aliases=["resource_tags"]),
1147-
purge_tags=dict(type="bool", default=True),
1175+
template_tags=dict(type="dict"),
1176+
purge_template_tags=dict(type="bool", default=True),
11481177
versions_to_delete=dict(type="list", elements="int"),
11491178
)
11501179

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# Add integration tests for issue https://github.com/ansible-collections/community.aws/issues/2131
3+
- name: Test creation/deletion of launch template with more 200 versions
4+
vars:
5+
test_launch_template_name: "{{ resource_prefix }}-limits"
6+
test_total_versions: "{{ range(1, 205) }}"
7+
block:
8+
- name: Create a Launch template with more than 200 versions using
9+
community.aws.ec2_launch_template:
10+
name: "{{ test_launch_template_name }}"
11+
instance_type: t2.micro
12+
version_description: "Version number {{ item }}"
13+
source_version: 1
14+
default_version: 1
15+
with_items: "{{ test_total_versions }}"
16+
17+
- name: Retrieve Launch template information
18+
amazon.aws.ec2_launch_template_info:
19+
filters:
20+
launch-template-name: "{{ test_launch_template_name }}"
21+
register: _templates
22+
23+
- name: Ensure the Launch template was created as expected
24+
ansible.builtin.assert:
25+
that:
26+
- _templates.launch_templates | length == 1
27+
- _templates.launch_templates[0].versions | length == test_total_versions | length
28+
29+
- name: Delete the launch template
30+
community.aws.ec2_launch_template:
31+
name: "{{ test_launch_template_name }}"
32+
state: absent
33+
register: _delete_templ
34+
35+
- name: Retrieve Launch template information
36+
amazon.aws.ec2_launch_template_info:
37+
filters:
38+
launch-template-name: "{{ test_launch_template_name }}"
39+
register: _templates
40+
41+
- name: Ensure the Launch template does not exist anymore
42+
ansible.builtin.assert:
43+
that:
44+
- _delete_templ is changed
45+
- _templates.launch_templates | length == 0
46+
47+
always:
48+
- name: Delete launch template
49+
community.aws.ec2_launch_template:
50+
state: absent
51+
name: "{{ test_launch_template_name }}"
52+
ignore_errors: true

tests/integration/targets/ec2_launch_template/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
block:
99
- include_tasks: template_data.yml
1010
- include_tasks: tagging.yml
11+
# - include_tasks: limits.yml
1112
- include_tasks: iam_instance_role.yml
1213
- include_tasks: versions.yml
1314
- include_tasks: deletion.yml

tests/integration/targets/ec2_launch_template/tasks/tagging.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
community.aws.ec2_launch_template:
99
name: "{{ test_launch_template_name }}"
1010
instance_type: t2.micro
11-
tags:
11+
template_tags:
1212
ResourcePrefix: "{{ resource_prefix }}"
1313
InstanceType: "t2.micro"
1414
register: _create_with_tags
@@ -35,7 +35,7 @@
3535
community.aws.ec2_launch_template:
3636
name: "{{ test_launch_template_name }}"
3737
instance_type: t2.micro
38-
tags:
38+
template_tags:
3939
ResourcePrefix: "{{ resource_prefix }}"
4040
InstanceType: "t2.micro"
4141
register: _create_with_tags_idempotency
@@ -62,9 +62,9 @@
6262
community.aws.ec2_launch_template:
6363
name: "{{ test_launch_template_name }}"
6464
instance_type: t2.micro
65-
tags:
65+
template_tags:
6666
Phase: integration
67-
purge_tags: false
67+
purge_template_tags: false
6868
register: _add_tag
6969

7070
- name: Retrieve Launch template information
@@ -91,9 +91,9 @@
9191
community.aws.ec2_launch_template:
9292
name: "{{ test_launch_template_name }}"
9393
instance_type: t3.micro
94-
tags:
94+
template_tags:
9595
Team: Ansible
96-
purge_tags: true
96+
purge_template_tags: true
9797
register: _add_tag_and_version
9898

9999
- name: Retrieve Launch template information
@@ -122,8 +122,8 @@
122122
community.aws.ec2_launch_template:
123123
name: "{{ test_launch_template_name }}"
124124
instance_type: t3.micro
125-
tags: {}
126-
purge_tags: true
125+
template_tags: {}
126+
purge_template_tags: true
127127
register: _purge_tags
128128

129129
- name: Retrieve Launch template information

0 commit comments

Comments
 (0)