mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-02 18:36:19 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -265,6 +265,7 @@ query_results_dict:
|
|||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
import traceback
|
||||
import json
|
||||
|
||||
PYMSSQL_IMP_ERR = None
|
||||
try:
|
||||
import pymssql
|
||||
|
|
@ -281,87 +282,83 @@ def clean_output(o):
|
|||
|
||||
def run_module():
|
||||
module_args = dict(
|
||||
name=dict(aliases=['db'], default=''),
|
||||
name=dict(aliases=["db"], default=""),
|
||||
login_user=dict(),
|
||||
login_password=dict(no_log=True),
|
||||
login_host=dict(required=True),
|
||||
login_port=dict(type='int', default=1433),
|
||||
login_port=dict(type="int", default=1433),
|
||||
script=dict(required=True),
|
||||
output=dict(default='default', choices=['dict', 'default']),
|
||||
params=dict(type='dict'),
|
||||
transaction=dict(type='bool', default=False),
|
||||
output=dict(default="default", choices=["dict", "default"]),
|
||||
params=dict(type="dict"),
|
||||
transaction=dict(type="bool", default=False),
|
||||
)
|
||||
|
||||
result = dict(
|
||||
changed=False,
|
||||
)
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec=module_args,
|
||||
supports_check_mode=True
|
||||
)
|
||||
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
|
||||
if not MSSQL_FOUND:
|
||||
module.fail_json(msg=missing_required_lib(
|
||||
'pymssql'), exception=PYMSSQL_IMP_ERR)
|
||||
module.fail_json(msg=missing_required_lib("pymssql"), exception=PYMSSQL_IMP_ERR)
|
||||
|
||||
db = module.params['name']
|
||||
login_user = module.params['login_user']
|
||||
login_password = module.params['login_password']
|
||||
login_host = module.params['login_host']
|
||||
login_port = module.params['login_port']
|
||||
script = module.params['script']
|
||||
output = module.params['output']
|
||||
sql_params = module.params['params']
|
||||
db = module.params["name"]
|
||||
login_user = module.params["login_user"]
|
||||
login_password = module.params["login_password"]
|
||||
login_host = module.params["login_host"]
|
||||
login_port = module.params["login_port"]
|
||||
script = module.params["script"]
|
||||
output = module.params["output"]
|
||||
sql_params = module.params["params"]
|
||||
# Added param to set the transactional mode (true/false)
|
||||
transaction = module.params['transaction']
|
||||
transaction = module.params["transaction"]
|
||||
|
||||
login_querystring = login_host
|
||||
if login_port != 1433:
|
||||
login_querystring = f"{login_host}:{login_port}"
|
||||
|
||||
if login_user is not None and login_password is None:
|
||||
module.fail_json(
|
||||
msg="when supplying login_user argument, login_password must also be provided")
|
||||
module.fail_json(msg="when supplying login_user argument, login_password must also be provided")
|
||||
|
||||
try:
|
||||
conn = pymssql.connect(
|
||||
user=login_user, password=login_password, host=login_querystring, database=db)
|
||||
conn = pymssql.connect(user=login_user, password=login_password, host=login_querystring, database=db)
|
||||
cursor = conn.cursor()
|
||||
except Exception as e:
|
||||
if "Unknown database" in str(e):
|
||||
errno, errstr = e.args
|
||||
module.fail_json(msg=f"ERROR: {errno} {errstr}")
|
||||
else:
|
||||
module.fail_json(msg="unable to connect, check login_user and login_password are correct, or alternatively check your "
|
||||
"@sysconfdir@/freetds.conf / ${HOME}/.freetds.conf")
|
||||
module.fail_json(
|
||||
msg="unable to connect, check login_user and login_password are correct, or alternatively check your "
|
||||
"@sysconfdir@/freetds.conf / ${HOME}/.freetds.conf"
|
||||
)
|
||||
|
||||
# If transactional mode is requested, start a transaction
|
||||
conn.autocommit(not transaction)
|
||||
|
||||
query_results_key = 'query_results'
|
||||
if output == 'dict':
|
||||
query_results_key = "query_results"
|
||||
if output == "dict":
|
||||
cursor = conn.cursor(as_dict=True)
|
||||
query_results_key = 'query_results_dict'
|
||||
query_results_key = "query_results_dict"
|
||||
|
||||
# Process the script into batches
|
||||
queries = []
|
||||
current_batch = []
|
||||
for statement in script.splitlines(True):
|
||||
# Ignore the Byte Order Mark, if found
|
||||
if statement.strip() == '\uFEFF':
|
||||
if statement.strip() == "\ufeff":
|
||||
continue
|
||||
|
||||
# Assume each 'GO' is on its own line but may have leading/trailing whitespace
|
||||
# and be of mixed-case
|
||||
if statement.strip().upper() != 'GO':
|
||||
if statement.strip().upper() != "GO":
|
||||
current_batch.append(statement)
|
||||
else:
|
||||
queries.append(''.join(current_batch))
|
||||
queries.append("".join(current_batch))
|
||||
current_batch = []
|
||||
if len(current_batch) > 0:
|
||||
queries.append(''.join(current_batch))
|
||||
queries.append("".join(current_batch))
|
||||
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
if module.check_mode:
|
||||
module.exit_json(**result)
|
||||
|
||||
|
|
@ -380,15 +377,15 @@ def run_module():
|
|||
# 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'
|
||||
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}'
|
||||
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
|
||||
|
|
@ -406,5 +403,5 @@ def main():
|
|||
run_module()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue