From 7f7b2f76a663be0fc6487d7ebac09ab51e6f0168 Mon Sep 17 00:00:00 2001 From: betanummeric <40263343+betanummeric@users.noreply.github.com> Date: Thu, 18 May 2023 09:28:34 +0200 Subject: [PATCH] fix connection arguments mysql driver compatability (#551) * only use the "database" connection argument with driver versions where "db" is deprecated/removed * connection arguments: fix KeyError * connection arguments: fix KeyError * connection arguments: use 'passwd' instead of 'password' with older drivers * add changelog fragment * refactoring: use "get_connector_name" in "mysql_connect" --------- Co-authored-by: Felix Hamme --- ...ection_arguments_driver_compatability.yaml | 2 ++ plugins/module_utils/mysql.py | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/551-fix_connection_arguments_driver_compatability.yaml diff --git a/changelogs/fragments/551-fix_connection_arguments_driver_compatability.yaml b/changelogs/fragments/551-fix_connection_arguments_driver_compatability.yaml new file mode 100644 index 00000000..be18f560 --- /dev/null +++ b/changelogs/fragments/551-fix_connection_arguments_driver_compatability.yaml @@ -0,0 +1,2 @@ +bugfixes: + - mysql module utils - use the connection arguments ``db`` instead of ``database`` and ``passwd`` instead of ``password`` when running with older mysql drivers (MySQLdb < 2.1.0 or PyMySQL < 1.0.0) (https://github.com/ansible-collections/community.mysql/pull/551). diff --git a/plugins/module_utils/mysql.py b/plugins/module_utils/mysql.py index 6aeebe54..713aba89 100644 --- a/plugins/module_utils/mysql.py +++ b/plugins/module_utils/mysql.py @@ -134,18 +134,34 @@ def mysql_connect(module, login_user=None, login_password=None, config_file='', if connect_timeout is not None: config['connect_timeout'] = connect_timeout if check_hostname is not None: - if mysql_driver.__name__ == "pymysql": + if get_connector_name(mysql_driver) == 'pymysql': version_tuple = (n for n in mysql_driver.__version__.split('.') if n != 'None') if reduce(lambda x, y: int(x) * 100 + int(y), version_tuple) >= 711: config['ssl']['check_hostname'] = check_hostname else: module.fail_json(msg='To use check_hostname, pymysql >= 0.7.11 is required on the target host') - if _mysql_cursor_param == 'cursor': + if get_connector_name(mysql_driver) == 'pymysql': # In case of PyMySQL driver: + if mysql_driver.version_info[0] < 1: + # for PyMySQL < 1.0.0, use 'db' instead of 'database' and 'passwd' instead of 'password' + if 'database' in config: + config['db'] = config['database'] + del config['database'] + if 'password' in config: + config['passwd'] = config['password'] + del config['password'] db_connection = mysql_driver.connect(autocommit=autocommit, **config) else: # In case of MySQLdb driver + if mysql_driver.version_info[0] < 2 and mysql_driver.version_info[1] < 1: + # for MySQLdb < 2.1.0, use 'db' instead of 'database' and 'passwd' instead of 'password' + if 'database' in config: + config['db'] = config['database'] + del config['database'] + if 'password' in config: + config['passwd'] = config['password'] + del config['password'] db_connection = mysql_driver.connect(**config) if autocommit: db_connection.autocommit(True)