From 924f18535a2ee3c09e46f42cb8130e6bb16f57af Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2020 21:04:08 +0100 Subject: [PATCH] Add support for "reconfigure" option for terraform init (#823) (#1209) * Add support for extra options for terraform init * Add missing var declaration * feat: add changelog fragment * feat: switch from an init_extra_args global option to an init_reconfigure specific option * Update changelogs/fragments/823-terraform_init_reconfigure.yaml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein (cherry picked from commit 282c1d546cf40177cf1a8e6dc016c0e1ead6d5e0) Co-authored-by: Damien Guihal --- .../fragments/823-terraform_init_reconfigure.yaml | 2 ++ plugins/modules/cloud/misc/terraform.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/823-terraform_init_reconfigure.yaml diff --git a/changelogs/fragments/823-terraform_init_reconfigure.yaml b/changelogs/fragments/823-terraform_init_reconfigure.yaml new file mode 100644 index 0000000000..408fe3a5a4 --- /dev/null +++ b/changelogs/fragments/823-terraform_init_reconfigure.yaml @@ -0,0 +1,2 @@ +minor_changes: +- terraform - add ``init_reconfigure`` option, which controls the ``-reconfigure`` flag (backend reconfiguration) (https://github.com/ansible-collections/community.general/pull/823). diff --git a/plugins/modules/cloud/misc/terraform.py b/plugins/modules/cloud/misc/terraform.py index c0fad285b1..79f6ceb198 100644 --- a/plugins/modules/cloud/misc/terraform.py +++ b/plugins/modules/cloud/misc/terraform.py @@ -105,6 +105,12 @@ options: type: list elements: path version_added: '0.2.0' + init_reconfigure: + description: + - Forces backend reconfiguration during init. + default: false + type: bool + version_added: '1.3.0' notes: - To just run a `terraform plan`, use check mode. requirements: [ "terraform" ] @@ -201,7 +207,7 @@ def _state_args(state_file): return [] -def init_plugins(bin_path, project_path, backend_config, backend_config_files): +def init_plugins(bin_path, project_path, backend_config, backend_config_files, init_reconfigure): command = [bin_path, 'init', '-input=false'] if backend_config: for key, val in backend_config.items(): @@ -212,6 +218,8 @@ def init_plugins(bin_path, project_path, backend_config, backend_config_files): if backend_config_files: for f in backend_config_files: command.extend(['-backend-config', f]) + if init_reconfigure: + command.extend('-reconfigure') rc, out, err = module.run_command(command, cwd=project_path) if rc != 0: module.fail_json(msg="Failed to initialize Terraform modules:\r\n{0}".format(err)) @@ -299,6 +307,7 @@ def main(): force_init=dict(type='bool', default=False), backend_config=dict(type='dict', default=None), backend_config_files=dict(type='list', elements='path', default=None), + init_reconfigure=dict(required=False, type='bool', default=False), ), required_if=[('state', 'planned', ['plan_file'])], supports_check_mode=True, @@ -316,6 +325,7 @@ def main(): force_init = module.params.get('force_init') backend_config = module.params.get('backend_config') backend_config_files = module.params.get('backend_config_files') + init_reconfigure = module.params.get('init_reconfigure') if bin_path is not None: command = [bin_path] @@ -323,7 +333,7 @@ def main(): command = [module.get_bin_path('terraform', required=True)] if force_init: - init_plugins(command[0], project_path, backend_config, backend_config_files) + init_plugins(command[0], project_path, backend_config, backend_config_files, init_reconfigure) workspace_ctx = get_workspace_context(command[0], project_path) if workspace_ctx["current"] != workspace: