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")