From 946430e1fbd54d6abfa3105304bd651272a544c3 Mon Sep 17 00:00:00 2001 From: egnirra <37709886+egnirra@users.noreply.github.com> Date: Tue, 23 Nov 2021 05:31:35 +0100 Subject: [PATCH] terraform: ensuring command options are applied during build_plan (#3726) * Fixes parameters missing in planned state * Added new line at end of file * Added changelog fragment for pr 3726 * Added changes mentioned by felixfontein * Removed blank space for pep8 validation * Update changelogs/fragments/3726-terraform-missing-parameters-planned-fix.yml Co-authored-by: Felix Fontein * Update plugins/modules/cloud/misc/terraform.py extend needs to be a list Co-authored-by: Felix Fontein Co-authored-by: Thomas Arringe Co-authored-by: Thomas Arringe Co-authored-by: Felix Fontein --- ...rraform-missing-parameters-planned-fix.yml | 4 ++++ plugins/modules/cloud/misc/terraform.py | 20 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/3726-terraform-missing-parameters-planned-fix.yml diff --git a/changelogs/fragments/3726-terraform-missing-parameters-planned-fix.yml b/changelogs/fragments/3726-terraform-missing-parameters-planned-fix.yml new file mode 100644 index 0000000000..4615aad85d --- /dev/null +++ b/changelogs/fragments/3726-terraform-missing-parameters-planned-fix.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - terraform - fix command options being ignored during planned/plan in function ``build_plan`` such as ``lock`` or ``lock_timeout`` + (https://github.com/ansible-collections/community.general/issues/3707, https://github.com/ansible-collections/community.general/pull/3726). \ No newline at end of file diff --git a/plugins/modules/cloud/misc/terraform.py b/plugins/modules/cloud/misc/terraform.py index 5e3b952c0c..8c7bd8c70b 100644 --- a/plugins/modules/cloud/misc/terraform.py +++ b/plugins/modules/cloud/misc/terraform.py @@ -319,11 +319,25 @@ def remove_workspace(bin_path, project_path, workspace): _workspace_cmd(bin_path, project_path, 'delete', workspace) -def build_plan(command, project_path, variables_args, state_file, targets, state, plan_path=None): +def build_plan(command, project_path, variables_args, state_file, targets, state, apply_args, plan_path=None): if plan_path is None: f, plan_path = tempfile.mkstemp(suffix='.tfplan') - plan_command = [command[0], 'plan', '-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path] + local_command = command.copy() + + plan_command = [command[0], 'plan'] + + if state == "planned": + for c in local_command[1:]: + plan_command.append(c) + + if state == "present": + for a in apply_args: + local_command.remove(a) + for c in local_command[1:]: + plan_command.append(c) + + plan_command.extend(['-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path]) for t in targets: plan_command.extend(['-target', t]) @@ -461,7 +475,7 @@ def main(): module.fail_json(msg='Could not find plan_file "{0}", check the path and try again.'.format(plan_file)) else: plan_file, needs_application, out, err, command = build_plan(command, project_path, variables_args, state_file, - module.params.get('targets'), state, plan_file) + module.params.get('targets'), state, APPLY_ARGS, plan_file) if state == 'present' and check_destroy and '- destroy' in out: module.fail_json(msg="Aborting command because it would destroy some resources. " "Consider switching the 'check_destroy' to false to suppress this error")