Skip to content

Commit d07eabd

Browse files
committed
add tagging on resource_type
1 parent 9602782 commit d07eabd

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

plugins/modules/ec2_launch_template.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,32 @@
340340
- A set of key-value pairs to be applied to resources when this Launch Template is used.
341341
- "Tag key constraints: Tag keys are case-sensitive and accept a maximum of 127 Unicode characters. May not begin with I(aws:)"
342342
- "Tag value constraints: Tag values are case-sensitive and accept a maximum of 255 Unicode characters."
343+
- This field is deprecated and will be removed in a release after 2026-12-01, use O(tag_specifications) instead.
343344
aliases: ['resource_tags']
345+
tag_specifications:
346+
description:
347+
- The tags to apply to the resources when this Launch template is used.
348+
type: list
349+
elements: dict
350+
version_added: 8.1.0
351+
suboptions:
352+
resource_type:
353+
description:
354+
- The type of resource to tag.
355+
- If the instance does not include the resource type that you specify, the instance launch fails.
356+
type: str
357+
default: instance
358+
choices:
359+
- instance
360+
- volume
361+
- network-interface
362+
- spot-instances-request
363+
tags:
364+
description:
365+
- A set of key-value pairs to be applied to the resource type.
366+
- "Tag key constraints: Tag keys are case-sensitive and accept a maximum of 127 Unicode characters. May not begin with I(aws:)"
367+
- "Tag value constraints: Tag values are case-sensitive and accept a maximum of 255 Unicode characters."
368+
type: dict
344369
user_data:
345370
description: >
346371
The Base64-encoded user data to make available to the instance. For more information, see the Linux
@@ -394,6 +419,7 @@
394419
- If the O(template_tags) parameter is not set then tags will not be modified.
395420
type: dict
396421
required: false
422+
version_added: 8.1.0
397423
purge_template_tags:
398424
description:
399425
- If O(purge_template_tags=true) and O(template_tags) is set, existing tags will be purged
@@ -407,6 +433,7 @@
407433
type: bool
408434
default: true
409435
required: false
436+
version_added: 8.1.0
410437
extends_documentation_fragment:
411438
- amazon.aws.common.modules
412439
- amazon.aws.region.modules
@@ -450,6 +477,35 @@
450477
- 1
451478
default_version: 2
452479
state: absent
480+
481+
- name: Create an ec2 launch template with specific tags
482+
community.aws.ec2_launch_template:
483+
name: "my_template"
484+
image_id: "ami-04b762b4289fba92b"
485+
instance_type: t2.micro
486+
disable_api_termination: true
487+
template_tags:
488+
Some: tag
489+
Another: tag
490+
491+
- name: Create an ec2 launch template with different tag for volume and instance
492+
community.aws.ec2_launch_template:
493+
name: "my_template"
494+
image_id: "ami-04b762b4289fba92b"
495+
instance_type: t2.micro
496+
block_device_mappings:
497+
- device_name: /dev/sdb
498+
ebs:
499+
volume_size: 20
500+
delete_on_termination: true
501+
volume_type: standard
502+
tag_specifications:
503+
- resource_type: instance
504+
tags:
505+
OsType: Linux
506+
- resource_type: volume
507+
tags:
508+
foo: bar
453509
"""
454510

455511
RETURN = r"""
@@ -759,9 +815,16 @@ def find_existing(client, module: AnsibleAWSModule) -> Tuple[Optional[Dict[str,
759815
def params_to_launch_data(
760816
template_params: Dict[str, Any], iam_instance_profile_arn: Optional[str] = None
761817
) -> Dict[str, Any]:
818+
if template_params.get("tag_specifications"):
819+
template_params["tag_specifications"] = [
820+
{"resource_type": ts["resource_type"], "tags": ansible_dict_to_boto3_tag_list(ts["tags"])}
821+
for ts in template_params["tag_specifications"]
822+
]
762823
if template_params.get("tags"):
824+
if "tag_specifications" not in template_params:
825+
template_params["tag_specifications"] = []
763826
tag_list = ansible_dict_to_boto3_tag_list(template_params.get("tags"))
764-
template_params["tag_specifications"] = [
827+
template_params["tag_specifications"] += [
765828
{"resource_type": r_type, "tags": tag_list} for r_type in ("instance", "volume", "network-interface")
766829
]
767830
del template_params["tags"]
@@ -1162,6 +1225,18 @@ def main():
11621225
security_groups=dict(type="list", elements="str"),
11631226
tags=dict(type="dict", aliases=["resource_tags"]),
11641227
user_data=dict(),
1228+
tag_specifications=dict(
1229+
type="list",
1230+
elements="dict",
1231+
options=dict(
1232+
resource_type=dict(
1233+
type="str",
1234+
default="instance",
1235+
choices=["instance", "volume", "network-interface", "spot-instances-request"],
1236+
),
1237+
tags=dict(type="dict"),
1238+
),
1239+
),
11651240
)
11661241

11671242
argument_spec = dict(
@@ -1187,6 +1262,13 @@ def main():
11871262
supports_check_mode=True,
11881263
)
11891264

1265+
if module.params.get("tags"):
1266+
module.deprecate(
1267+
"The tags parameter has been deprecated, please use tag_specifications instead.",
1268+
date="2026-12-01",
1269+
collection_name="community.aws",
1270+
)
1271+
11901272
state = module.params.get("state")
11911273
client = module.client("ec2")
11921274
launch_template, launch_template_versions = find_existing(client, module)

0 commit comments

Comments
 (0)