community.general.mssql_script module – Execute SQL scripts on a MSSQL database
Note
This module is part of the community.general collection (version 4.8.3).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.general
.
To use it in a playbook, specify: community.general.mssql_script
.
New in version 4.0.0: of community.general
Requirements
The below requirements are needed on the host that executes this module.
python >= 2.7
pymssql
Parameters
Parameter |
Comments |
---|---|
Host running the database. |
|
The password used to authenticate with. |
|
Port of the MSSQL server. Requires login_host be defined as well. Default: 1433 |
|
The username used to authenticate with. |
|
Database to run script against. Default: “” |
|
With Output format
Choices:
|
|
Parameters passed to the script as SQL parameters. (‘SELECT %(name)s”’ with |
|
The SQL script to be executed. Script can contain multiple SQL statements. Multiple Batches can be separated by Each batch must return at least one result set. |
Notes
Note
Requires the pymssql Python package on the remote host. For Ubuntu, this is as easy as
pip install pymssql
(See ansible.builtin.pip.)
Examples
- name: Check DB connection
community.general.mssql_script:
login_user: "{{ mssql_login_user }}"
login_password: "{{ mssql_login_password }}"
login_host: "{{ mssql_host }}"
login_port: "{{ mssql_port }}"
db: master
script: "SELECT 1"
- name: Query with parameter
community.general.mssql_script:
login_user: "{{ mssql_login_user }}"
login_password: "{{ mssql_login_password }}"
login_host: "{{ mssql_host }}"
login_port: "{{ mssql_port }}"
script: |
SELECT name, state_desc FROM sys.databases WHERE name = %(dbname)s
params:
dbname: msdb
register: result_params
- assert:
that:
- result_params.query_results[0][0][0][0] == 'msdb'
- result_params.query_results[0][0][0][1] == 'ONLINE'
- name: two batches with default output
community.general.mssql_script:
login_user: "{{ mssql_login_user }}"
login_password: "{{ mssql_login_password }}"
login_host: "{{ mssql_host }}"
login_port: "{{ mssql_port }}"
script: |
SELECT 'Batch 0 - Select 0'
SELECT 'Batch 0 - Select 1'
GO
SELECT 'Batch 1 - Select 0'
register: result_batches
- assert:
that:
- result_batches.query_results | length == 2 # two batch results
- result_batches.query_results[0] | length == 2 # two selects in first batch
- result_batches.query_results[0][0] | length == 1 # one row in first select
- result_batches.query_results[0][0][0] | length == 1 # one column in first row
- result_batches.query_results[0][0][0][0] == 'Batch 0 - Select 0' # each row contains a list of values.
- name: two batches with dict output
community.general.mssql_script:
login_user: "{{ mssql_login_user }}"
login_password: "{{ mssql_login_password }}"
login_host: "{{ mssql_host }}"
login_port: "{{ mssql_port }}"
output: dict
script: |
SELECT 'Batch 0 - Select 0' as b0s0
SELECT 'Batch 0 - Select 1' as b0s1
GO
SELECT 'Batch 1 - Select 0' as b1s0
register: result_batches_dict
- assert:
that:
- result_batches_dict.query_results_dict | length == 2 # two batch results
- 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
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
List of batches (queries separated by Returned: success and output=default Sample: [[[[“Batch 0 - Select 0”]], [[“Batch 0 - Select 1”]]], [[[“Batch 1 - Select 0”]]]] |
|
List of result sets of each query. If a query returns no results, the results of this and all the following queries will not be included in the output. Use the Returned: success |
|
List of rows returned by query. Returned: success |
|
List of column values. Any non-standard JSON type is converted to string. Returned: success, if output is default Sample: [“Batch 0 - Select 0”] |
|
List of batches (queries separated by Returned: success and output=dict Sample: [[[[“Batch 0 - Select 0”]], [[“Batch 0 - Select 1”]]], [[[“Batch 1 - Select 0”]]]] |
|
List of result sets of each query. If a query returns no results, the results of this and all the following queries will not be included in the output. Use ‘GO’ keyword to separate queries. Returned: success |
|
List of rows returned by query. Returned: success |
|
Dictionary of column names and values. Any non-standard JSON type is converted to string. Returned: success, if output is dict Sample: {“col_name”: “Batch 0 - Select 0”} |
Authors
Kris Budde (@kbudde)
Collection links
Issue Tracker Repository (Sources) Submit a bug report Request a feature Communication