From b24249d39a066df4cd8a19465d72a5d306e87ef0 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Thu, 19 Jan 2023 16:36:06 +0100 Subject: [PATCH] Add connector information to the returned values I need to know what python library was used. I had a container with both mysqlclient and pymysql installed and tests used a different connector that what is advertised by the title of integration tests. We need to prevent that otherwise our tests are worth nothing. --- plugins/module_utils/mysql.py | 5 +++++ plugins/modules/mysql_info.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/plugins/module_utils/mysql.py b/plugins/module_utils/mysql.py index d256599..146d880 100644 --- a/plugins/module_utils/mysql.py +++ b/plugins/module_utils/mysql.py @@ -34,6 +34,11 @@ mysql_driver_fail_msg = ('A MySQL module is required: for Python 2.7 either PyMy 'Consider setting ansible_python_interpreter to use ' 'the intended Python version.') +mysql_driver_info = { + "name": mysql_driver.__name__, + "version": mysql_driver.__version__ +} + def parse_from_mysql_config_file(cnf): # Default values of comment_prefix is '#' and ';'. diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 1daa9b9..0a89aaa 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -206,6 +206,12 @@ slave_hosts: type: dict sample: - { "2": { "Host": "", "Master_id": 1, "Port": 3306 } } +connector: + description: The python connector used by the plugins + returned: always + type: dict + sample: + - { "connector: { "name": "pymysql", "version": "1.0.2" } } ''' from decimal import Decimal @@ -216,6 +222,7 @@ from ansible_collections.community.mysql.plugins.module_utils.mysql import ( mysql_common_argument_spec, mysql_driver, mysql_driver_fail_msg, + mysql_driver_info, ) from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -564,15 +571,19 @@ def main(): check_hostname=check_hostname, connect_timeout=connect_timeout, cursor_class='DictCursor') except Exception as e: - module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. " - "Exception message: %s" % (config_file, to_native(e))) + module.fail_json(msg=f"unable to connect to database using \ + {mysql_driver_info.name} {mysql_driver_info.version}, \ + check login_user and login_password are correct or {config_file} has \ + the credentials. Exception message: {to_native(e)}%s") ############################### # Create object and do main job mysql = MySQL_Info(module, cursor) - module.exit_json(changed=False, **mysql.get_info(filter_, exclude_fields, return_empty_dbs)) + module.exit_json(changed=False, + **mysql.get_info(filter_, exclude_fields, return_empty_dbs), + connector=mysql_driver_info) if __name__ == '__main__':