#!/usr/bin/python # Copyright (c) 2021, Kris Budde 0: queries.append("".join(current_batch)) result["changed"] = True if module.check_mode: module.exit_json(**result) query_results = [] for query in queries: # Catch and exit on any bad query errors try: cursor.execute(query, sql_params) qry_result = [] rows = cursor.fetchall() while rows: qry_result.append(rows) rows = cursor.fetchall() query_results.append(qry_result) except Exception as e: # We know we executed the statement so this error just means we have no resultset # which is ok (eg UPDATE/INSERT) if ( type(e).__name__ == "OperationalError" and str(e) == "Statement not executed or executed statement has no resultset" ): query_results.append([]) else: # Rollback transaction before failing the module in case of error if transaction: conn.rollback() error_msg = f"{type(e).__name__}: {e}" module.fail_json(msg="query failed", query=query, error=error_msg, **result) # Commit transaction before exiting the module in case of no error if transaction: conn.commit() # ensure that the result is json serializable qry_results = json.loads(json.dumps(query_results, default=clean_output)) result[query_results_key] = qry_results module.exit_json(**result) def main(): run_module() if __name__ == "__main__": main()