Skip to content

Commit

Permalink
add support for template_tags and purge_template_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Oct 4, 2024
1 parent c21b9d0 commit c036ff2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
minor_changes:
- 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).
- 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).
- ec2_launch_template - add the ``purge_tags`` option (https://github.com/ansible-collections/community.aws/pull/2164).
- 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).
- 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).
51 changes: 40 additions & 11 deletions plugins/modules/ec2_launch_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,29 @@
- Wether the instance tags are availble (V(enabled)) via metadata endpoint or not (V(disabled)).
choices: [enabled, disabled]
default: 'disabled'
notes:
- Support for O(purge_tags) was added in release 8.1.0.
template_tags:
description:
- A dictionary representing the tags to be applied to the launch template.
- If the O(template_tags) parameter is not set then tags will not be modified.
type: dict
required: false
purge_template_tags:
description:
- If O(purge_template_tags=true) and O(template_tags) is set, existing tags will be purged
from the resource to match exactly what is defined by O(template_tags) parameter.
- If the O(template_tags) parameter is not set then tags will not be modified, even
if O(purge_template_tags=True).
- Tag keys beginning with V(aws:) are reserved by Amazon and can not be
modified. As such they will be ignored for the purposes of the
O(purge_template_tags) parameter. See the Amazon documentation for more information
U(https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions).
type: bool
default: true
required: false
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.boto3
- amazon.aws.tags
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -701,8 +717,9 @@
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import determine_iam_arn_from_name
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import modify_launch_template
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import is_ansible_aws_error_code
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import AnsibleAWSError
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import is_ansible_aws_error_code
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters

Expand Down Expand Up @@ -742,6 +759,12 @@ def find_existing(client, module: AnsibleAWSModule) -> Tuple[Optional[Dict[str,
def params_to_launch_data(
template_params: Dict[str, Any], iam_instance_profile_arn: Optional[str] = None
) -> Dict[str, Any]:
if template_params.get("tags"):
tag_list = ansible_dict_to_boto3_tag_list(template_params.get("tags"))
template_params["tag_specifications"] = [
{"resource_type": r_type, "tags": tag_list} for r_type in ("instance", "volume")
]
del template_params["tags"]
if iam_instance_profile_arn:
template_params["iam_instance_profile"] = {"arn": iam_instance_profile_arn}
for interface in template_params.get("network_interfaces") or []:
Expand Down Expand Up @@ -801,7 +824,7 @@ def validate_version_deletion(

if default_version_to_set and default_version_to_set not in remaining_versions:
module.fail_json(
msg=f"Could not set version '{default_version_to_set}' as default,"
msg=f"Could not set version '{default_version_to_set}' as default, "
"the launch template version was not found for the specified launch template id '{launch_template_id}'."
)
else:
Expand Down Expand Up @@ -967,8 +990,8 @@ def ensure_present(
existing_versions: List[Dict[str, Any]],
) -> None:
template_name = module.params["template_name"]
tags = module.params["tags"]
purge_tags = module.params["purge_tags"]
template_tags = module.params["template_tags"]
purge_template_tags = module.params["purge_template_tags"]
version_description = module.params.get("version_description")
iam_instance_profile = module.params.get("iam_instance_profile")
if iam_instance_profile:
Expand All @@ -988,7 +1011,7 @@ def ensure_present(
client,
launch_template_name=template_name,
launch_template_data=launch_template_data,
tags=tags,
tags=template_tags,
ClientToken=uuid4().hex,
VersionDescription=version_description,
)
Expand Down Expand Up @@ -1020,7 +1043,12 @@ def ensure_present(
)
# Ensure tags
changed |= ensure_ec2_tags(
client, module, launch_template_id, resource_type="launch-template", tags=tags, purge_tags=purge_tags
client,
module,
launch_template_id,
resource_type="launch-template",
tags=template_tags,
purge_tags=purge_template_tags,
)

module.exit_json(changed=changed, **format_module_output(client, module))
Expand Down Expand Up @@ -1132,6 +1160,7 @@ def main():
ram_disk_id=dict(),
security_group_ids=dict(type="list", elements="str"),
security_groups=dict(type="list", elements="str"),
tags=dict(type="dict", aliases=["resource_tags"]),
user_data=dict(),
)

Expand All @@ -1143,8 +1172,8 @@ def main():
source_version=dict(default="latest"),
version_description=dict(default=""),
iam_instance_profile=dict(),
tags=dict(type="dict", aliases=["resource_tags"]),
purge_tags=dict(type="bool", default=True),
template_tags=dict(type="dict"),
purge_template_tags=dict(type="bool", default=True),
versions_to_delete=dict(type="list", elements="int"),
)

Expand Down
52 changes: 52 additions & 0 deletions tests/integration/targets/ec2_launch_template/tasks/limits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
# Add integration tests for issue https://github.com/ansible-collections/community.aws/issues/2131
- name: Test creation/deletion of launch template with more 200 versions
vars:
test_launch_template_name: "{{ resource_prefix }}-limits"
test_total_versions: "{{ range(1, 205) }}"
block:
- name: Create a Launch template with more than 200 versions using
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t2.micro
version_description: "Version number {{ item }}"
source_version: 1
default_version: 1
with_items: "{{ test_total_versions }}"

- name: Retrieve Launch template information
amazon.aws.ec2_launch_template_info:
filters:
launch-template-name: "{{ test_launch_template_name }}"
register: _templates

- name: Ensure the Launch template was created as expected
ansible.builtin.assert:
that:
- _templates.launch_templates | length == 1
- _templates.launch_templates[0].versions | length == test_total_versions | length

- name: Delete the launch template
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
state: absent
register: _delete_templ

- name: Retrieve Launch template information
amazon.aws.ec2_launch_template_info:
filters:
launch-template-name: "{{ test_launch_template_name }}"
register: _templates

- name: Ensure the Launch template does not exist anymore
ansible.builtin.assert:
that:
- _delete_templ is changed
- _templates.launch_templates | length == 0

always:
- name: Delete launch template
community.aws.ec2_launch_template:
state: absent
name: "{{ test_launch_template_name }}"
ignore_errors: true
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
block:
- include_tasks: template_data.yml
- include_tasks: tagging.yml
# - include_tasks: limits.yml
- include_tasks: iam_instance_role.yml
- include_tasks: versions.yml
- include_tasks: deletion.yml
16 changes: 8 additions & 8 deletions tests/integration/targets/ec2_launch_template/tasks/tagging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t2.micro
tags:
template_tags:
ResourcePrefix: "{{ resource_prefix }}"
InstanceType: "t2.micro"
register: _create_with_tags
Expand All @@ -35,7 +35,7 @@
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t2.micro
tags:
template_tags:
ResourcePrefix: "{{ resource_prefix }}"
InstanceType: "t2.micro"
register: _create_with_tags_idempotency
Expand All @@ -62,9 +62,9 @@
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t2.micro
tags:
template_tags:
Phase: integration
purge_tags: false
purge_template_tags: false
register: _add_tag

- name: Retrieve Launch template information
Expand All @@ -91,9 +91,9 @@
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t3.micro
tags:
template_tags:
Team: Ansible
purge_tags: true
purge_template_tags: true
register: _add_tag_and_version

- name: Retrieve Launch template information
Expand Down Expand Up @@ -122,8 +122,8 @@
community.aws.ec2_launch_template:
name: "{{ test_launch_template_name }}"
instance_type: t3.micro
tags: {}
purge_tags: true
template_tags: {}
purge_template_tags: true
register: _purge_tags

- name: Retrieve Launch template information
Expand Down

0 comments on commit c036ff2

Please sign in to comment.