From 4b02d7129b01eddf2c1fd9e08d334986de454c9a Mon Sep 17 00:00:00 2001 From: eshulman Date: Mon, 7 Aug 2023 16:19:02 +0300 Subject: [PATCH] allow resources exclusion if required - allow removing resources from plan to exclude them from delete --- main.yaml | 9 +++++++++ plugin.spec | 6 ++++++ roles/exclude_resources/tasks/exclude_resources.yaml | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 roles/exclude_resources/tasks/exclude_resources.yaml diff --git a/main.yaml b/main.yaml index 24898f2..0c16329 100644 --- a/main.yaml +++ b/main.yaml @@ -93,6 +93,14 @@ name: terraform_environment tasks_from: prepare + - name: exclude resources if required + import_role: + name: exclude_resources + tasks_from: exclude_resources + when: + - excluded_resources is defined + - terraform_plan_state == 'absent' + - name: Execute Terraform Plan import_role: name: terraform_environment @@ -133,3 +141,4 @@ {{ msg.split('STRING_TO_SPLIT') }} {%- endif -%} failed_when: terraform_plan_execution is failed + diff --git a/plugin.spec b/plugin.spec index 4954495..3b45aeb 100644 --- a/plugin.spec +++ b/plugin.spec @@ -89,3 +89,9 @@ subparsers: help: | List of files containing terraform backend configuration. ansible_variable: 'terraform_backend_config_files' + exclude-resources: + type: Value + help: | + A string that specifies resources to exclude from deletion. + ansible_variable: 'excluded_resources' + diff --git a/roles/exclude_resources/tasks/exclude_resources.yaml b/roles/exclude_resources/tasks/exclude_resources.yaml new file mode 100644 index 0000000..f5eda52 --- /dev/null +++ b/roles/exclude_resources/tasks/exclude_resources.yaml @@ -0,0 +1,5 @@ +--- + +- name: remove resources from plan to exclude them from delete + command: terraform state rm "{{ excluded_resources }}" +