Skip to content

Add Script File Path Support with Automatic BOM Removal to mssql_script Module #10203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

zollo
Copy link

@zollo zollo commented Jun 5, 2025

SUMMARY

Fixes #10201

This feature branch updates the mssql_script module with a new parameter allowing users to supply a file path containing their SQL script. To avoid potential execution issues, the file is checked for the presence of any common BOM patterns - if found, they are removed.

Notes:

  • When a BOM is detected and removed, details are added to the module results.
  • This module will never modify the provided script, we are only modifying the file contents as they are loaded into memory.
  • All existing functionality remains unchanged, BOM detection only applies to files using the script_path param.
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

mssql_script

ADDITIONAL INFORMATION

See changelog fragment for more information.

This code contribution is made on behalf of the Omnissa UEM Cloud Services Team (formerly VMware EUC), internal reference Jira AWSA-47744.

@ansibullbot
Copy link
Collaborator

cc @kbudde
click here for bot help

@ansibullbot ansibullbot added feature This issue/PR relates to a feature request module module new_contributor Help guide this first time contributor plugins plugin (any type) labels Jun 5, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Jun 5, 2025
@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Jun 5, 2025
@felixfontein felixfontein added the check-before-release PR will be looked at again shortly before release and merged if possible. label Jun 5, 2025
@russoz
Copy link
Collaborator

russoz commented Jun 7, 2025

Hi @zollo thanks for your contribution.

I must say, I am a bit uneasy about this PR - I mean, the "alternate credentials" part of it. That does not seem to be anything particular to MSSQL, in fact the code literally tries with one set of credentials and if that does not work, it tries to login with the other one.

IMHO, this feature:

  • is adding unnecessary complexity to the module, just to save few lines of YAML for the Ansible user. Do you have any particular use case in mind for this feature? I am finding it hard to visualize a playbook in which we need to access a database and we use credentials that do not work (hence the need for an alternate one), but I might be missing something.
  • opens a dangerous precedent - why not add that feature to mysql modules? to postgresql modules? To any other module that required credentials (not just database ones)?
  • raises another question: why only one set of alternate credentials? Why not two, or three? How many is too many in that case? You may think that one set of alternates is enough, I think 1 is already too much, but we might bump into someone else in the community who thinks they must have two.

I think those questions are very subjective, and as such, we should try and avoid bringing them up in the context of the module. The maintenance of common/shared code based on individual's subjective views is probably going to much more painful than it needs to.

Sorry, I don't think we should add that feature to the module, but I might be wrong in that. It would be nice if more folks could chip in this one. @Andersson007 you are maintaining the PGSQL modules (right? 😁 ), what do you think of this?

That being said, the handling of BOM markers is definitely something we could benefit from.

@Andersson007
Copy link
Contributor

Andersson007 commented Jun 10, 2025

@russoz thanks for pinging! I'm also not sure if we should add these "in-case-of-failure" features (cause a lot of things can fail).
It's solvable on Ansible level by using:

  1. ignore_errors: true + register: result
  2. another task containing the other creds with the when: result is failed directive

my opinion

@zollo zollo changed the title Add Alternate Login Credential & Script File Path Support to mssql_script Module Add Script File Path Support with Automatic BOM Removal to mssql_script Module Jun 13, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Jun 13, 2025
@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Jun 13, 2025
script_source:
description: Source of the executed script.
type: str
returned: always
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't returned if the module fails. Also all new return values need a version_added entry.

Suggested change
returned: always
returned: success
version_added: 11.0.0

choices: ["file", "parameter"]
script_path:
description: Path to the script file that was executed.
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.

Suggested change
type: str
type: path

script_path:
description: Path to the script file that was executed.
type: str
returned: when script_path parameter is used
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same below:

Suggested change
returned: when script_path parameter is used
returned: success and O(script_path) is provided

bom_removed:
description: Whether a Byte Order Mark was detected and removed from the script file.
type: bool
returned: when script_path parameter is used and BOM was found
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not always provide this if script_path is used? It's not very helpful if this is sometimes true and sometimes not there.

description: Type of Byte Order Mark that was detected and removed.
type: str
returned: when script_path parameter is used and BOM was found
sample: "UTF-8"
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about returning none/null (the Jinja/YAML equivalent for Python's None) here if script_path is used, but no BOM was found?

- If the file contains a Byte Order Mark (BOM), it will be automatically detected and removed.
- Mutually exclusive with O(script).
type: path
version_added: 10.8.0
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
version_added: 10.8.0
version_added: 11.0.0

Comment on lines +2 to +3
- mssql_script - add ``script_path`` parameter to allow executing SQL scripts from files as an alternative to inline scripts.
- mssql_script - add automatic Byte Order Mark (BOM) detection and removal for script files to prevent SQL parsing errors.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The second fragment isn't needed, since this is part of the feature mentioned in the first fragment. Also the URLs are missing.

Suggested change
- mssql_script - add ``script_path`` parameter to allow executing SQL scripts from files as an alternative to inline scripts.
- mssql_script - add automatic Byte Order Mark (BOM) detection and removal for script files to prevent SQL parsing errors.
- mssql_script - add ``script_path`` parameter to allow executing SQL scripts from files as an alternative to inline scripts (https://github.com/ansible-collections/community.general/issues/10201, https://github.com/ansible-collections/community.general/pull/10203).

script_path:
description:
- Path to a file containing the SQL script to be executed.
- Script can contain multiple SQL statements. Multiple Batches can be separated by V(GO) command.
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
- Script can contain multiple SQL statements. Multiple Batches can be separated by V(GO) command.
- Script can contain multiple SQL statements. Multiple batches can be separated by V(GO) command.

- Path to a file containing the SQL script to be executed.
- Script can contain multiple SQL statements. Multiple Batches can be separated by V(GO) command.
- Each batch must return at least one result set.
- If the file contains a Byte Order Mark (BOM), it will be automatically detected and removed.
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
- If the file contains a Byte Order Mark (BOM), it will be automatically detected and removed.
- If the file contains a Byte Order Mark (BOM), it will be automatically detected and removed.
The return values RV(bom_removed) and RV(bom_type) will be set accordingly.

script_content = raw_content.decode('utf-8', errors='replace')

# Detect and remove BOM
cleaned_content, bom_removed, bom_type = detect_and_remove_bom(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't the BOM detection/removal happen before any decoding is done? The BOM basically tells you the encoding you have to use to decode the script.

Returns tuple (script_content, bom_info)
"""
if not os.path.exists(script_path):
raise IOError(f"Script file not found: {script_path}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

f-strings are not compatible with Python 2.7, you cannot use them currently in modules.

@zollo
Copy link
Author

zollo commented Jun 14, 2025

Thank you for the great feedback @felixfontein - I will address these ASAP.

@felixfontein felixfontein added the backport-11 Automatically create a backport for the stable-10 branch label Jun 16, 2025
@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Jun 24, 2025
@felixfontein
Copy link
Collaborator

Ping @zollo

needs_info

@ansibullbot ansibullbot added the needs_info This issue requires further information. Please answer any outstanding questions label Jul 13, 2025
@ansibullbot
Copy link
Collaborator

@zollo This pullrequest is waiting for your response. Please respond or the pullrequest will be closed.

click here for bot help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-11 Automatically create a backport for the stable-10 branch check-before-release PR will be looked at again shortly before release and merged if possible. feature This issue/PR relates to a feature request module module needs_info This issue requires further information. Please answer any outstanding questions new_contributor Help guide this first time contributor plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mssql_script module should support alternate login credentials and script file paths
5 participants