diff --git a/tfworker/commands/terraform.py b/tfworker/commands/terraform.py index ebbd222..4d5f268 100644 --- a/tfworker/commands/terraform.py +++ b/tfworker/commands/terraform.py @@ -415,6 +415,7 @@ def _run( self._terraform_bin, debug=debug, b64_encode=self._b64_encode, + extra_vars=definition.template_vars, ) except HookError as e: click.secho( @@ -487,6 +488,7 @@ def _run( self._terraform_bin, debug=debug, b64_encode=self._b64_encode, + extra_vars=definition.template_vars, ) except HookError as e: click.secho( @@ -504,6 +506,7 @@ def hook_exec( terraform_path, debug=False, b64_encode=False, + extra_vars={}, ): """ hook_exec executes a hook script. @@ -559,9 +562,7 @@ def hook_exec( if state_value is not None: if b64_encode: - state_value = base64.b64encode( - state_value.encode("utf-8") - ).decode() + state_value = base64.b64encode(state_value.encode("utf-8")) local_env[f"TF_REMOTE_{state}_{item}".upper()] = state_value # populate environment with terraform variables @@ -578,7 +579,7 @@ def hook_exec( tf_var[1] = tf_var[1].replace(k, v) if b64_encode: - tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8")).decode() + tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8")) local_env[f"TF_VAR_{tf_var[0].upper()}"] = tf_var[1] else: @@ -587,6 +588,11 @@ def hook_exec( fg="red", ) + for k, v in extra_vars.items(): + if b64_encode: + v = base64.b64encode(v.encode("utf-8")) + local_env[f"TF_EXTRA_{k.upper()}"] = v + # execute the hook (exit_code, stdout, stderr) = pipe_exec( f"{hook_script} {phase} {command}", diff --git a/tfworker/definitions.py b/tfworker/definitions.py index e278520..aaa7760 100644 --- a/tfworker/definitions.py +++ b/tfworker/definitions.py @@ -115,6 +115,10 @@ def provider_names(self): def plan_file(self): return self._plan_file + @property + def template_vars(self): + return self._template_vars + @plan_file.setter def plan_file(self, value: Path): if type(value) not in [PosixPath, WindowsPath, Path]: