1
0
Fork 0
mirror of https://github.com/ansible-collections/community.mysql.git synced 2026-02-04 07:11:49 +00:00

Add session_vars to mysql_query. (#729)

This commit adds a `session_vars` dict to the `mysql_query` plugin,
similar to that done in #489. While this could also be done by using a
list of queries, having a dictionary allows for a cleaner query,
reusability (via merge key), and a more consistent experience when using
different plugins (like `mysql_user`, which supports `session_vars`).
This commit is contained in:
Richard Burnison 2025-09-12 03:05:30 -04:00 committed by GitHub
parent 1f9b1a29dd
commit 2e0c44f616
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 0 deletions

View file

@ -51,6 +51,12 @@ options:
- Where passed queries run in a single transaction (C(yes)) or commit them one-by-one (C(no)).
type: bool
default: false
session_vars:
description:
- "Dictionary of session variables in form of C(variable: value) to set at the beginning of module execution."
- Cannot be used to set global variables, use the M(community.mysql.mysql_variables) module instead.
type: dict
version_added: '3.16.0'
attributes:
check_mode:
support: none
@ -136,6 +142,7 @@ from ansible_collections.community.mysql.plugins.module_utils.mysql import (
mysql_common_argument_spec,
mysql_driver,
mysql_driver_fail_msg,
set_session_vars,
)
from ansible.module_utils._text import to_native
@ -176,6 +183,7 @@ def main():
positional_args=dict(type='list', elements='raw'),
named_args=dict(type='dict'),
single_transaction=dict(type='bool', default=False),
session_vars=dict(type='dict'),
)
module = AnsibleModule(
@ -195,6 +203,7 @@ def main():
check_hostname = module.params['check_hostname']
config_file = module.params['config_file']
query = module.params["query"]
session_vars = module.params["session_vars"]
if not isinstance(query, (str, list)):
module.fail_json(msg="the query option value must be a string or list, passed %s" % type(query))
@ -238,6 +247,9 @@ def main():
max_keyword_len = len(max(DML_QUERY_KEYWORDS + DDL_QUERY_KEYWORDS, key=len))
if session_vars:
set_session_vars(module, cursor, session_vars)
# Execute query:
query_result = []
executed_queries = []