diff --git a/changelogs/fragments/1632-using_check_rc_in_terraform.yml b/changelogs/fragments/1632-using_check_rc_in_terraform.yml new file mode 100644 index 0000000000..481becb763 --- /dev/null +++ b/changelogs/fragments/1632-using_check_rc_in_terraform.yml @@ -0,0 +1,2 @@ +bugfixes: +- terraform - improve result code checking when executing terraform commands (https://github.com/ansible-collections/community.general/pull/1632). diff --git a/plugins/modules/cloud/misc/terraform.py b/plugins/modules/cloud/misc/terraform.py index 9e8834821c..680bab9aed 100644 --- a/plugins/modules/cloud/misc/terraform.py +++ b/plugins/modules/cloud/misc/terraform.py @@ -194,9 +194,7 @@ def preflight_validation(bin_path, project_path, variables_args=None, plan_file= if not os.path.isdir(project_path): module.fail_json(msg="Path for Terraform project '{0}' doesn't exist on this host - check the path and try again please.".format(project_path)) - rc, out, err = module.run_command([bin_path, 'validate'] + variables_args, cwd=project_path, use_unsafe_shell=True) - if rc != 0: - module.fail_json(msg="Failed to validate Terraform configuration files:\r\n{0}".format(err)) + rc, out, err = module.run_command([bin_path, 'validate'] + variables_args, check_rc=True, cwd=project_path, use_unsafe_shell=True) def _state_args(state_file): @@ -220,9 +218,7 @@ def init_plugins(bin_path, project_path, backend_config, backend_config_files, i 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)) + rc, out, err = module.run_command(command, check_rc=True, cwd=project_path) def get_workspace_context(bin_path, project_path): @@ -244,9 +240,7 @@ def get_workspace_context(bin_path, project_path): def _workspace_cmd(bin_path, project_path, action, workspace): command = [bin_path, 'workspace', action, workspace, '-no-color'] - rc, out, err = module.run_command(command, cwd=project_path) - if rc != 0: - module.fail_json(msg="Failed to {0} workspace:\r\n{1}".format(action, err)) + rc, out, err = module.run_command(command, check_rc=True, cwd=project_path) return rc, out, err @@ -388,15 +382,10 @@ def main(): command.append(plan_file) if needs_application and not module.check_mode and not state == 'planned': - rc, out, err = module.run_command(command, cwd=project_path) + rc, out, err = module.run_command(command, check_rc=True, cwd=project_path) # checks out to decide if changes were made during execution if ' 0 added, 0 changed' not in out and not state == "absent" or ' 0 destroyed' not in out: changed = True - if rc != 0: - module.fail_json( - msg="Failure when executing Terraform command. Exited {0}.\nstdout: {1}\nstderr: {2}".format(rc, out, err), - command=' '.join(command) - ) outputs_command = [command[0], 'output', '-no-color', '-json'] + _state_args(state_file) rc, outputs_text, outputs_err = module.run_command(outputs_command, cwd=project_path)