Skip to content

Add tags to ecs cluster#2347

Open
Shaps wants to merge 6 commits intoansible-collections:mainfrom
Shaps:add-tags-to-ecs-cluster
Open

Add tags to ecs cluster#2347
Shaps wants to merge 6 commits intoansible-collections:mainfrom
Shaps:add-tags-to-ecs-cluster

Conversation

@Shaps
Copy link
Copy Markdown

@Shaps Shaps commented Oct 15, 2025

SUMMARY

Adds 2 new options to the ecs_cluster module:

  • tags Allows adding or updating tags on an ECS Cluster. Tags are not replaced by default
  • purge_tags allows re-setting the tags to the passed values. Overrides the default behaviour of not re-setting the tags.
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

ecs_cluster

ADDITIONAL INFORMATION

Not much additional info to be added, I required the tags to be added on cluster creation.

I have also added some lint fixes in the integration test playbook/tasks

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 15, 2025

Docs Build 📝

Thank you for contribution!✨

The docsite for this PR is available for download as an artifact from this run:
https://github.com/ansible-collections/community.aws/actions/runs/23264951598

You can compare to the docs for the main branch here:
https://ansible-collections.github.io/community.aws/branch/main

File changes:

  • M collections/community/aws/ecs_cluster_module.html
Click to see the diff comparison.

NOTE: only file modifications are shown here. New and deleted files are excluded.
See the file list and check the published docs to see those files.

diff --git a/home/runner/work/community.aws/community.aws/docsbuild/base/collections/community/aws/ecs_cluster_module.html b/home/runner/work/community.aws/community.aws/docsbuild/head/collections/community/aws/ecs_cluster_module.html
index 6f3471d..06cb4f0 100644
--- a/home/runner/work/community.aws/community.aws/docsbuild/base/collections/community/aws/ecs_cluster_module.html
+++ b/home/runner/work/community.aws/community.aws/docsbuild/head/collections/community/aws/ecs_cluster_module.html
@@ -310,6 +310,20 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 </div></td>
 </tr>
 <tr class="row-even"><td><div class="ansible-option-cell">
+<div class="ansibleOptionAnchor" id="parameter-purge_tags"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-purge-tags"><strong>purge_tags</strong></p>
+<a class="ansibleOptionLink" href="#parameter-purge_tags" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-type">boolean</span></p>
+</div></td>
+<td><div class="ansible-option-cell"><p>If <code class="ansible-option-value docutils literal notranslate"><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-purge-tags"><span class="std std-ref"><span class="pre">purge_tags=true</span></span></a></code> and <code class="ansible-option docutils literal notranslate"><strong><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-tags"><span class="std std-ref"><span class="pre">tags</span></span></a></strong></code> is set, existing tags will be purged from the resource to match exactly what is defined by <code class="ansible-option docutils literal notranslate"><strong><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-tags"><span class="std std-ref"><span class="pre">tags</span></span></a></strong></code> parameter.</p>
+<p>If the <code class="ansible-option docutils literal notranslate"><strong><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-tags"><span class="std std-ref"><span class="pre">tags</span></span></a></strong></code> parameter is not set then tags will not be modified, even if <code class="ansible-option-value docutils literal notranslate"><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-purge-tags"><span class="std std-ref"><span class="pre">purge_tags=True</span></span></a></code>.</p>
+<p>Tag keys beginning with <code class="ansible-value docutils literal notranslate"><span class="pre">aws:</span></code> are reserved by Amazon and can not be modified. As such they will be ignored for the purposes of the <code class="ansible-option docutils literal notranslate"><strong><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-purge-tags"><span class="std std-ref"><span class="pre">purge_tags</span></span></a></strong></code> parameter. See the Amazon documentation for more information <a class="reference external" href="https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions">https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions</a>.</p>
+<p class="ansible-option-line"><strong class="ansible-option-choices">Choices:</strong></p>
+<ul class="simple">
+<li><p><code class="ansible-option-choices-entry docutils literal notranslate"><span class="pre">false</span></code></p></li>
+<li><p><code class="ansible-option-default-bold docutils literal notranslate"><strong><span class="pre">true</span></strong></code> <span class="ansible-option-choices-default-mark">← (default)</span></p></li>
+</ul>
+</div></td>
+</tr>
+<tr class="row-odd"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-region"></div>
 <div class="ansibleOptionAnchor" id="parameter-aws_region"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-region"><span id="ansible-collections-community-aws-ecs-cluster-module-parameter-aws-region"></span><strong>region</strong></p>
 <a class="ansibleOptionLink" href="#parameter-region" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-aliases">aliases: aws_region</span></p>
@@ -321,7 +335,7 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 <p>See the Amazon AWS documentation for more information <a class="reference external" href="http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region">http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region</a>.</p>
 </div></td>
 </tr>
-<tr class="row-odd"><td><div class="ansible-option-cell">
+<tr class="row-even"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-repeat"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-repeat"><strong>repeat</strong></p>
 <a class="ansibleOptionLink" href="#parameter-repeat" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-type">integer</span></p>
 </div></td>
@@ -329,7 +343,7 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 <p class="ansible-option-line"><strong class="ansible-option-default-bold">Default:</strong> <code class="ansible-option-default docutils literal notranslate"><span class="pre">10</span></code></p>
 </div></td>
 </tr>
-<tr class="row-even"><td><div class="ansible-option-cell">
+<tr class="row-odd"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-secret_key"></div>
 <div class="ansibleOptionAnchor" id="parameter-aws_secret_access_key"></div>
 <div class="ansibleOptionAnchor" id="parameter-aws_secret_key"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-secret-key"><span id="ansible-collections-community-aws-ecs-cluster-module-parameter-aws-secret-key"></span><span id="ansible-collections-community-aws-ecs-cluster-module-parameter-aws-secret-access-key"></span><strong>secret_key</strong></p>
@@ -343,7 +357,7 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 <p>The <em>aws_secret_access_key</em> alias was added in release 5.1.0 for consistency with the AWS botocore SDK.</p>
 </div></td>
 </tr>
-<tr class="row-odd"><td><div class="ansible-option-cell">
+<tr class="row-even"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-session_token"></div>
 <div class="ansibleOptionAnchor" id="parameter-aws_session_token"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-session-token"><span id="ansible-collections-community-aws-ecs-cluster-module-parameter-aws-session-token"></span><strong>session_token</strong></p>
 <a class="ansibleOptionLink" href="#parameter-session_token" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-aliases">aliases: aws_session_token</span></p>
@@ -355,7 +369,7 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 <p>The <em>session_token</em> and <em>profile</em> options are mutually exclusive.</p>
 </div></td>
 </tr>
-<tr class="row-even"><td><div class="ansible-option-cell">
+<tr class="row-odd"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-state"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-state"><strong>state</strong></p>
 <a class="ansibleOptionLink" href="#parameter-state" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-type">string</span> / <span class="ansible-option-required">required</span></p>
 </div></td>
@@ -368,6 +382,16 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 </ul>
 </div></td>
 </tr>
+<tr class="row-even"><td><div class="ansible-option-cell">
+<div class="ansibleOptionAnchor" id="parameter-tags"></div>
+<div class="ansibleOptionAnchor" id="parameter-resource_tags"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-tags"><span id="ansible-collections-community-aws-ecs-cluster-module-parameter-resource-tags"></span><strong>tags</strong></p>
+<a class="ansibleOptionLink" href="#parameter-tags" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-aliases">aliases: resource_tags</span></p>
+<p class="ansible-option-type-line"><span class="ansible-option-type">dictionary</span></p>
+</div></td>
+<td><div class="ansible-option-cell"><p>A dictionary representing the tags to be applied to the resource.</p>
+<p>If the <code class="ansible-option docutils literal notranslate"><strong><a class="reference internal" href="#ansible-collections-community-aws-ecs-cluster-module-parameter-tags"><span class="std std-ref"><span class="pre">tags</span></span></a></strong></code> parameter is not set then tags will not be modified.</p>
+</div></td>
+</tr>
 <tr class="row-odd"><td><div class="ansible-option-cell">
 <div class="ansibleOptionAnchor" id="parameter-validate_certs"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-parameter-validate-certs"><strong>validate_certs</strong></p>
 <a class="ansibleOptionLink" href="#parameter-validate_certs" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-type">boolean</span></p>
@@ -522,6 +546,15 @@ see <a class="reference internal" href="#ansible-collections-community-aws-ecs-c
 <p class="ansible-option-line ansible-option-sample"><strong class="ansible-option-sample-bold">Sample:</strong> <code class="ansible-option-sample docutils literal notranslate"><span class="pre">&quot;ACTIVE&quot;</span></code></p>
 </div></td>
 </tr>
+<tr class="row-odd"><td><div class="ansible-option-cell">
+<div class="ansibleOptionAnchor" id="return-tags"></div><p class="ansible-option-title" id="ansible-collections-community-aws-ecs-cluster-module-return-tags"><strong>tags</strong></p>
+<a class="ansibleOptionLink" href="#return-tags" title="Permalink to this return value"></a><p class="ansible-option-type-line"><span class="ansible-option-type">dictionary</span></p>
+</div></td>
+<td><div class="ansible-option-cell"><p>Cluster tags.</p>
+<p class="ansible-option-line"><strong class="ansible-option-returned-bold">Returned:</strong> always</p>
+<p class="ansible-option-line ansible-option-sample"><strong class="ansible-option-sample-bold">Sample:</strong> <code class="ansible-option-sample docutils literal notranslate"><span class="pre">{&quot;key&quot;:</span> <span class="pre">&quot;value&quot;}</span></code></p>
+</div></td>
+</tr>
 </tbody>
 </table>
 <section id="authors">

@softwarefactory-project-zuul
Copy link
Copy Markdown
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/f7327b674de64e7db873a89c128d576f

✔️ ansible-galaxy-importer SUCCESS in 4m 16s (non-voting)
✔️ build-ansible-collection SUCCESS in 11m 26s
✔️ ansible-test-splitter SUCCESS in 4m 25s
✔️ integration-community.aws-1 SUCCESS in 13m 40s
Skipped 21 jobs

Comment thread plugins/modules/ecs_cluster.py Outdated
type: bool
default: true
purge_tags:
version_added: 10.1.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to manually document tags and purge_tags here, as they are automatically included when the amazon.aws.tags doc fragment is imported. However, I suggest adding a note for the module to indicate when support for these parameters was introduced (likely version 11.0.0).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I will update the version once we're ok merging this.

Comment thread plugins/modules/ecs_cluster.py Outdated
Comment thread changelogs/fragments/20251015_ecs_cluster_add_tags.yml Outdated
Comment thread changelogs/fragments/20251015_ecs_cluster_add_tags.yml Outdated
@Shaps Shaps force-pushed the add-tags-to-ecs-cluster branch 2 times, most recently from 410e3d4 to 2a3c54d Compare February 3, 2026 09:41
@softwarefactory-project-zuul
Copy link
Copy Markdown
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/59c580896a0c422d884d17506582867b

✔️ ansible-galaxy-importer SUCCESS in 4m 03s (non-voting)
✔️ build-ansible-collection SUCCESS in 11m 18s
✔️ ansible-test-splitter SUCCESS in 4m 25s
✔️ integration-community.aws-1 SUCCESS in 14m 18s
Skipped 21 jobs

@Shaps Shaps force-pushed the add-tags-to-ecs-cluster branch from bd51193 to 60a36f6 Compare February 4, 2026 09:16
@softwarefactory-project-zuul
Copy link
Copy Markdown
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/8e7cf3a5107c4e9792b81533108e2eb5

✔️ ansible-galaxy-importer SUCCESS in 8m 02s (non-voting)
✔️ build-ansible-collection SUCCESS in 11m 10s
✔️ ansible-test-splitter SUCCESS in 4m 19s
✔️ integration-community.aws-1 SUCCESS in 13m 44s
Skipped 21 jobs

@Shaps
Copy link
Copy Markdown
Author

Shaps commented Feb 5, 2026

@alinabuzachis looks like the docs expect purge_tags to be set to True by default. I think I'd personally prefer keeping it as False to avoid unexpected behaviour (wiping tags if not set is the first thing that comes to mind) but I'm happy to take suggestions!

Shaps added 6 commits March 18, 2026 21:12
the `tags` option allows the creation and update of tags on a cluster.
Since tags are not replaced by default, the `purge_tags` option allows
for all the tags to be re-set to the values passed
@Shaps Shaps force-pushed the add-tags-to-ecs-cluster branch from 60a36f6 to aac6452 Compare March 18, 2026 20:14
@softwarefactory-project-zuul
Copy link
Copy Markdown
Contributor

Build succeeded.
https://ansible.softwarefactory-project.io/zuul/buildset/cadebdfd7b584be899e9112ba42db3a0

ansible-galaxy-importer FAILURE in 3m 59s (non-voting)
✔️ build-ansible-collection SUCCESS in 12m 20s
✔️ ansible-test-splitter SUCCESS in 4m 25s
✔️ integration-community.aws-1 SUCCESS in 14m 04s
Skipped 21 jobs

Copy link
Copy Markdown
Collaborator

@s-hertel s-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tested this, just gave a cursory read-through.

),
),
tags=dict(type="dict", required=False, aliases=["resource_tags"]),
purge_tags=dict(required=False, type="bool", default=False),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should default to True. It should only purge existing tags if tags is explicitly set to a dictionary.

requested_cp = existing_cp
requested_cps = existing_cps

if module.params["tags"]:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's an empty dictionary I'd expect it to remove all tags, not skip purging.

module.params["tags"],
)

tags_changed = True if recursive_diff(existing_tags, module.params["tags"]) is not None else False
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use recursive_diff instead the compare_aws_tags helper? There are some general guidelines here https://docs.ansible.com/projects/ansible/latest/collections/amazon/aws/docsite/dev_guidelines.html#dealing-with-tags.


if module.params["tags"]:
if module.params["purge_tags"]:
tags_to_remove = existing_tags
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect tags_to_remove only contains existing tags that are not specified for the task.

target_swap_mb: 0
target_swappiness: 80
ecs_task_containers:
- name: "{{ ecs_task_name }}"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend removing all the stylistic changes to existing tests (indentation, booleans, quotes), unless maintainers ask for such a change. This will make your pull request easier to review and avoids polluting the git blame with unenforced personal preferences that can easily be changed again by the next contributor.

else:
results["cluster"] = existing

results["cluster"] = cluster_mgr.refresh_cluster_state(cluster_name=module.params["name"])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just set the cluster tags to the calculated value. Calling describe_clusters again seems unnecessary.

Comment on lines +344 to +348
cluster_mgr.update_tags(
existing["clusterArn"],
tags_to_remove,
module.params["tags"],
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thought - this modules supports check mode, and this breaks it. You should add a test to make sure check mode detects tag changes but doesn't actually modify tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants