From 26431377dcaf1b07a89773843aded77dc902fe9a Mon Sep 17 00:00:00 2001 From: halo Date: Fri, 12 Sep 2025 15:53:07 +0800 Subject: [PATCH 1/4] Add additional parameters to support connections to lower versions of SQL Server --- plugins/modules/mssql_script.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/modules/mssql_script.py b/plugins/modules/mssql_script.py index 37bd0853d04..f4680c12285 100644 --- a/plugins/modules/mssql_script.py +++ b/plugins/modules/mssql_script.py @@ -291,6 +291,8 @@ def run_module(): output=dict(default='default', choices=['dict', 'default']), params=dict(type='dict'), transaction=dict(type='bool', default=False), + tds_version=dict(type='str', required=False), + encryption=dict(type='str', required=False) ) result = dict( @@ -315,6 +317,9 @@ def run_module(): sql_params = module.params['params'] # Added param to set the transactional mode (true/false) transaction = module.params['transaction'] + # When connecting to lower versions such as SQL Server 2008, you can try setting tds_version to 7.0 and encryption to off. + tds_version = module.params['tds_version'] + encryption = module.params['encryption'] login_querystring = login_host if login_port != 1433: @@ -326,7 +331,7 @@ def run_module(): try: conn = pymssql.connect( - user=login_user, password=login_password, host=login_querystring, database=db) + user=login_user, password=login_password, host=login_querystring, database=db, encryption=encryption, tds_version=tds_version) cursor = conn.cursor() except Exception as e: if "Unknown database" in str(e): From fafd6ee739d4b425c3f370685a409064074ecd9d Mon Sep 17 00:00:00 2001 From: halo Date: Tue, 16 Sep 2025 10:31:05 +0800 Subject: [PATCH 2/4] mssql_script: use kwargs to pass encryption/tds_version only when not None, update changelog --- ... tds_version and encryption parameters.yml | 2 ++ plugins/modules/mssql_script.py | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/10816-mssql_script_add tds_version and encryption parameters.yml diff --git a/changelogs/fragments/10816-mssql_script_add tds_version and encryption parameters.yml b/changelogs/fragments/10816-mssql_script_add tds_version and encryption parameters.yml new file mode 100644 index 00000000000..b482bc65c90 --- /dev/null +++ b/changelogs/fragments/10816-mssql_script_add tds_version and encryption parameters.yml @@ -0,0 +1,2 @@ +minor_changes: + - mssql_script - adds ``tds_version`` and ``encryption`` parameters for pymssql (https://github.com/ansible-collections/community.general/pull/10816). \ No newline at end of file diff --git a/plugins/modules/mssql_script.py b/plugins/modules/mssql_script.py index f4680c12285..bcd83b34e96 100644 --- a/plugins/modules/mssql_script.py +++ b/plugins/modules/mssql_script.py @@ -62,6 +62,14 @@ type: bool default: false version_added: 8.4.0 + encryption: + description: + - Specify whether to use encryption for the connection to the server, Please refer to the pymssql documentation for detailed information. + type: str + tds_version: + description: + - Specify the TDS protocol version to use when connecting to the server, Please refer to the pymssql documentation for detailed information. + type: str output: description: - With V(default) each row is returned as a list of values. See RV(query_results). @@ -330,8 +338,17 @@ def run_module(): 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, encryption=encryption, tds_version=tds_version) + kwargs = { + "user": login_user, + "password": login_password, + "host": login_querystring, + "database": db, + } + if encryption is not None: + kwargs["encryption"] = encryption + if tds_version is not None: + kwargs["tds_version"] = tds_version + conn = pymssql.connect(**kwargs) cursor = conn.cursor() except Exception as e: if "Unknown database" in str(e): From 0266b7511f454a2e30416e34255908df0920d005 Mon Sep 17 00:00:00 2001 From: halo Date: Wed, 17 Sep 2025 15:50:03 +0800 Subject: [PATCH 3/4] Update mssql_script.py documentation --- plugins/modules/mssql_script.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/modules/mssql_script.py b/plugins/modules/mssql_script.py index bcd83b34e96..011da1d2a44 100644 --- a/plugins/modules/mssql_script.py +++ b/plugins/modules/mssql_script.py @@ -64,12 +64,16 @@ version_added: 8.4.0 encryption: description: - - Specify whether to use encryption for the connection to the server, Please refer to the pymssql documentation for detailed information. + - Specify whether to use encryption for the connection to the server. + Please refer to the pymssql documentation for detailed information. type: str + version_added: 11.4.0 tds_version: description: - - Specify the TDS protocol version to use when connecting to the server, Please refer to the pymssql documentation for detailed information. + - Specify the TDS protocol version to use when connecting to the server. + Please refer to the pymssql documentation for detailed information. type: str + version_added: 11.4.0 output: description: - With V(default) each row is returned as a list of values. See RV(query_results). From c19bff1556787468ce78e7614777cdb5440d8e07 Mon Sep 17 00:00:00 2001 From: halo Date: Thu, 18 Sep 2025 15:16:24 +0800 Subject: [PATCH 4/4] Delete unnecessary parameters and add examples --- plugins/modules/mssql_script.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/plugins/modules/mssql_script.py b/plugins/modules/mssql_script.py index 011da1d2a44..bbd8130a390 100644 --- a/plugins/modules/mssql_script.py +++ b/plugins/modules/mssql_script.py @@ -66,12 +66,14 @@ description: - Specify whether to use encryption for the connection to the server. Please refer to the pymssql documentation for detailed information. + - The available values are "off" and "on" type: str version_added: 11.4.0 tds_version: description: - Specify the TDS protocol version to use when connecting to the server. Please refer to the pymssql documentation for detailed information. + - The available values are "7.0", "7.1", "7.2", "7.3", "7.4", "8.0" etc. type: str version_added: 11.4.0 output: @@ -174,6 +176,18 @@ - result_batches_dict.query_results_dict[0] | length == 2 # two selects in first batch - result_batches_dict.query_results_dict[0][0] | length == 1 # one row in first select - result_batches_dict.query_results_dict[0][0][0]['b0s0'] == 'Batch 0 - Select 0' # column 'b0s0' of first row + +- name: Get lower version DB information + community.general.mssql_script: + login_user: "{{ mssql_login_user }}" + login_password: "{{ mssql_login_password }}" + login_host: "{{ mssql_host }}" + login_port: "{{ mssql_port }}" + db: master + tds_version: "7.0" + encryption: off + script: | + SELECT @@version """ RETURN = r""" @@ -303,8 +317,8 @@ def run_module(): output=dict(default='default', choices=['dict', 'default']), params=dict(type='dict'), transaction=dict(type='bool', default=False), - tds_version=dict(type='str', required=False), - encryption=dict(type='str', required=False) + tds_version=dict(type='str'), + encryption=dict(type='str') ) result = dict( @@ -342,17 +356,17 @@ def run_module(): msg="when supplying login_user argument, login_password must also be provided") try: - kwargs = { + connection_args = { "user": login_user, "password": login_password, "host": login_querystring, "database": db, } if encryption is not None: - kwargs["encryption"] = encryption + connection_args["encryption"] = encryption if tds_version is not None: - kwargs["tds_version"] = tds_version - conn = pymssql.connect(**kwargs) + connection_args["tds_version"] = tds_version + conn = pymssql.connect(**connection_args) cursor = conn.cursor() except Exception as e: if "Unknown database" in str(e):