Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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).
44 changes: 42 additions & 2 deletions plugins/modules/mssql_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@
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.
- The available values are "off" and "on"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The available values are "off" and "on"
- The available values are V("off") and V("on").

(I left the quotes to avoid conversion to booleans.)

If no other values are allowed, how about adding choices to limit the input to these two values?

type: str
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is intrinsically a boolean value, why not:

Suggested change
type: str
type: bool

?
Maybe it should have a default value, regardless of the type?

version_added: 11.4.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now:

Suggested change
version_added: 11.4.0
version_added: 12.0.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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The available values are "7.0", "7.1", "7.2", "7.3", "7.4", "8.0" etc.
- The available values are V(7.0), V(7.1), V(7.2), V(7.3), V(7.4), V(8.0), and so on.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The param is type=str, the doc should recommend wrapping quotes, so that the values are not converted to float.

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).
Expand Down Expand Up @@ -162,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

Copy link
Collaborator

@russoz russoz Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tripping the sanity test (trailing spaces in the line):

Suggested change

- 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the param type=str, this should have quotes around the value. If you change it to type=bool, then

Suggested change
encryption: off
encryption: false

script: |
SELECT @@version
"""

RETURN = r"""
Expand Down Expand Up @@ -291,6 +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'),
encryption=dict(type='str')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making life better for the next person:

Suggested change
encryption=dict(type='str')
encryption=dict(type='str'),

)

result = dict(
Expand All @@ -315,6 +343,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:
Expand All @@ -325,8 +356,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)
connection_args = {
"user": login_user,
"password": login_password,
"host": login_querystring,
"database": db,
}
if encryption is not None:
connection_args["encryption"] = encryption
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you make the parameter a boolean, then this should go:

Suggested change
connection_args["encryption"] = encryption
connection_args["encryption"] = "on" if encryption else "off"

if tds_version is not None:
connection_args["tds_version"] = tds_version
conn = pymssql.connect(**connection_args)
cursor = conn.cursor()
except Exception as e:
if "Unknown database" in str(e):
Expand Down
Loading