mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #5735/fc2b1aac backport][stable-6] terraform: bugfix: init command when default workspace doesn't exists (#5777)
terraform: bugfix: init command when default workspace doesn't exists (#5735)
* feat: init when default workspace doesn't exists
* doc: add changelogs fragment and docs update
* fix: changelog formating fix
(cherry picked from commit fc2b1aac4a
)
Co-authored-by: Teodor Janez Podobnik <48418580+dorkamotorka@users.noreply.github.com>
This commit is contained in:
parent
b7697fe3de
commit
d95a821d5b
2 changed files with 10 additions and 4 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
bugfixes:
|
||||||
|
- terraform - fix ``current`` workspace never getting appended to the ``all`` key in the ``workspace_ctf`` object (https://github.com/ansible-collections/community.general/pull/5735).
|
||||||
|
- terraform - fix ``terraform init`` failure when there are multiple workspaces on the remote backend and when ``default`` workspace is missing by setting ``TF_WORKSPACE`` environmental variable to the value of ``workspace`` when used (https://github.com/ansible-collections/community.general/pull/5735).
|
|
@ -48,7 +48,9 @@ options:
|
||||||
version_added: 3.0.0
|
version_added: 3.0.0
|
||||||
workspace:
|
workspace:
|
||||||
description:
|
description:
|
||||||
- The terraform workspace to work with.
|
- The terraform workspace to work with. This sets the C(TF_WORKSPACE) environmental variable
|
||||||
|
that is used to override workspace selection. For more information about workspaces
|
||||||
|
have a look at U(https://developer.hashicorp.com/terraform/language/state/workspaces).
|
||||||
type: str
|
type: str
|
||||||
default: default
|
default: default
|
||||||
purge_workspace:
|
purge_workspace:
|
||||||
|
@ -310,7 +312,7 @@ def _state_args(state_file):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def init_plugins(bin_path, project_path, backend_config, backend_config_files, init_reconfigure, provider_upgrade, plugin_paths):
|
def init_plugins(bin_path, project_path, backend_config, backend_config_files, init_reconfigure, provider_upgrade, plugin_paths, workspace):
|
||||||
command = [bin_path, 'init', '-input=false', '-no-color']
|
command = [bin_path, 'init', '-input=false', '-no-color']
|
||||||
if backend_config:
|
if backend_config:
|
||||||
for key, val in backend_config.items():
|
for key, val in backend_config.items():
|
||||||
|
@ -328,7 +330,7 @@ def init_plugins(bin_path, project_path, backend_config, backend_config_files, i
|
||||||
if plugin_paths:
|
if plugin_paths:
|
||||||
for plugin_path in plugin_paths:
|
for plugin_path in plugin_paths:
|
||||||
command.extend(['-plugin-dir', plugin_path])
|
command.extend(['-plugin-dir', plugin_path])
|
||||||
rc, out, err = module.run_command(command, check_rc=True, cwd=project_path)
|
rc, out, err = module.run_command(command, check_rc=True, cwd=project_path, environ_update={"TF_WORKSPACE": workspace})
|
||||||
|
|
||||||
|
|
||||||
def get_workspace_context(bin_path, project_path):
|
def get_workspace_context(bin_path, project_path):
|
||||||
|
@ -343,6 +345,7 @@ def get_workspace_context(bin_path, project_path):
|
||||||
continue
|
continue
|
||||||
elif stripped_item.startswith('* '):
|
elif stripped_item.startswith('* '):
|
||||||
workspace_ctx["current"] = stripped_item.replace('* ', '')
|
workspace_ctx["current"] = stripped_item.replace('* ', '')
|
||||||
|
workspace_ctx["all"].append(stripped_item.replace('* ', ''))
|
||||||
else:
|
else:
|
||||||
workspace_ctx["all"].append(stripped_item)
|
workspace_ctx["all"].append(stripped_item)
|
||||||
return workspace_ctx
|
return workspace_ctx
|
||||||
|
@ -485,7 +488,7 @@ def main():
|
||||||
|
|
||||||
if force_init:
|
if force_init:
|
||||||
if overwrite_init or not os.path.isfile(os.path.join(project_path, ".terraform", "terraform.tfstate")):
|
if overwrite_init or not os.path.isfile(os.path.join(project_path, ".terraform", "terraform.tfstate")):
|
||||||
init_plugins(command[0], project_path, backend_config, backend_config_files, init_reconfigure, provider_upgrade, plugin_paths)
|
init_plugins(command[0], project_path, backend_config, backend_config_files, init_reconfigure, provider_upgrade, plugin_paths, workspace)
|
||||||
|
|
||||||
workspace_ctx = get_workspace_context(command[0], project_path)
|
workspace_ctx = get_workspace_context(command[0], project_path)
|
||||||
if workspace_ctx["current"] != workspace:
|
if workspace_ctx["current"] != workspace:
|
||||||
|
|
Loading…
Reference in a new issue