From 4d5b5f3e5e9aef102e6bf966dab79d65172169fe Mon Sep 17 00:00:00 2001 From: Brian Schonecker Date: Wed, 20 Aug 2025 14:32:58 -0400 Subject: [PATCH] Add excludes parameter. --- changelogs/changelog.yaml | 21 +++++++++++++++++++++ plugins/module_utils/terraform_commands.py | 6 ++++++ plugins/modules/terraform.py | 13 +++++++++++++ 3 files changed, 40 insertions(+) diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index 423af26a..3a67efd1 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -186,3 +186,24 @@ releases: - 20250630-terraform-review-workspace-logic.yaml - release_summary.yml release_date: '2025-07-11' + 4.0.1: + changes: + breaking_changes: + - terraform - The default value `default` for the `workspace` argument has been + removed (https://github.com/ansible-collections/cloud.terraform/pull/200). + bugfixes: + - inventory/terraform_state - Add support for remote/cloud backends (https://github.com/ansible-collections/cloud.terraform/issues/150). + - inventory/terraform_state - use ``terraform pull`` instead of ``terraform + show`` to parse raw state file to avoid provider versioning constraints (https://github.com/ansible-collections/cloud.terraform/issues/151). + minor_changes: + - inventory/terraform_state - Support for custom Terraform providers (https://github.com/ansible-collections/cloud.terraform/pull/146). + - terraform - Update module logic to determine workspace (https://github.com/ansible-collections/cloud.terraform/pull/200). + release_summary: This major release updates the workspace logic for the Terraform + module and adds some bug fixes. + fragments: + - 20240620-inventory-terraform_state-fix-issue-with-terraform-show.yaml + - 20240628-inventory-terraform_state-custom-providers.yaml + - 20240731-terraform_state-support-remote-cloud-backend.yml + - 20250630-terraform-review-workspace-logic.yaml + - release_summary.yml + release_date: '2025-07-11' diff --git a/plugins/module_utils/terraform_commands.py b/plugins/module_utils/terraform_commands.py index cf59da95..2daf8d2b 100644 --- a/plugins/module_utils/terraform_commands.py +++ b/plugins/module_utils/terraform_commands.py @@ -58,6 +58,7 @@ def apply_plan( lock: bool, lock_timeout: Optional[int], targets: List[str], + excludes: List[str], needs_application: bool, ) -> Tuple[str, str, str]: command = ["apply", "-no-color", "-input=false"] @@ -75,6 +76,8 @@ def apply_plan( for t in targets: command.extend(["-target", t]) + for e in excludes: + command.extend(["-exclude", e]) command.append(plan_file_path) @@ -129,6 +132,7 @@ def plan( self, target_plan_file_path: str, targets: List[str], + excludes: List[str], destroy: bool, state_args: List[str], variables_args: List[str], @@ -144,6 +148,8 @@ def plan( ] for t in targets: command.extend(["-target", t]) + for e in excludes: + command.extend(["-exclude", e]) if destroy: command.append("-destroy") command.extend(state_args) diff --git a/plugins/modules/terraform.py b/plugins/modules/terraform.py index 7307fd58..833868de 100644 --- a/plugins/modules/terraform.py +++ b/plugins/modules/terraform.py @@ -113,6 +113,14 @@ type: bool default: false version_added: 1.0.0 + excludes: + description: + - A list of specific resources to exclude in this plan/application. The + resources selected here will also auto-exclude any dependencies. + type: list + default: [] + elements: str + version_added: 1.0.0 targets: description: - A list of specific resources to target in this plan/application. The @@ -462,6 +470,7 @@ def main() -> None: plan_file=dict(type="path"), state_file=dict(type="path"), targets=dict(type="list", elements="str", default=[]), + excludes=dict(type='list', elements='str', default=[]), lock=dict(type="bool", default=True), lock_timeout=dict(type="int"), force_init=dict(type="bool", default=False), @@ -475,6 +484,8 @@ def main() -> None: ), required_if=[("state", "planned", ["plan_file"])], supports_check_mode=True, + mutually_exclusive=[ ['targets', 'excludes'] ], + ) project_path = module.params.get("project_path") @@ -591,6 +602,7 @@ def main() -> None: plan_result_changed, plan_result_any_destroyed, plan_stdout, plan_stderr = terraform.plan( target_plan_file_path=plan_file_to_apply, targets=module.params.get("targets"), + excludes=module.params.get("excludes"), destroy=state == "absent", state_args=get_state_args(state_file), variables_args=variables_args, @@ -625,6 +637,7 @@ def main() -> None: lock=module.params.get("lock", False), lock_timeout=module.params.get("lock_timeout"), targets=module.params.get("targets") or [], + excludes=module.params.get("excludes") or [], needs_application=plan_file_needs_application, ) except TerraformError as e: