diff --git a/lib/ansible/module_utils/mysql.py b/lib/ansible/module_utils/mysql.py index 512e8bafde..2de6dac739 100644 --- a/lib/ansible/module_utils/mysql.py +++ b/lib/ansible/module_utils/mysql.py @@ -78,11 +78,7 @@ def mysql_connect(module, login_user=None, login_password=None, config_file='', if connect_timeout is not None: config['connect_timeout'] = connect_timeout - try: - db_connection = mysql_driver.connect(**config) - - except Exception as e: - module.fail_json(msg="unable to connect to database: %s" % to_native(e)) + db_connection = mysql_driver.connect(**config) if cursor_class == 'DictCursor': return db_connection.cursor(**{_mysql_cursor_param: mysql_driver.cursors.DictCursor}) diff --git a/lib/ansible/modules/database/mysql/mysql_info.py b/lib/ansible/modules/database/mysql/mysql_info.py index eaea7cc5bb..b2a15b9c60 100644 --- a/lib/ansible/modules/database/mysql/mysql_info.py +++ b/lib/ansible/modules/database/mysql/mysql_info.py @@ -479,9 +479,13 @@ def main(): if mysql_driver is None: module.fail_json(msg=mysql_driver_fail_msg) - cursor = mysql_connect(module, login_user, login_password, - config_file, ssl_cert, ssl_key, ssl_ca, db, - connect_timeout=connect_timeout, cursor_class='DictCursor') + try: + cursor = mysql_connect(module, login_user, login_password, + config_file, ssl_cert, ssl_key, ssl_ca, db, + 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))) ############################### # Create object and do main job diff --git a/test/integration/targets/mysql_user/defaults/main.yml b/test/integration/targets/mysql_user/defaults/main.yml index e40be893fb..9d6865a919 100644 --- a/test/integration/targets/mysql_user/defaults/main.yml +++ b/test/integration/targets/mysql_user/defaults/main.yml @@ -7,6 +7,8 @@ user_name_2: 'db_user2' user_password_1: 'gadfFDSdtTU^Sdfuj' user_password_2: 'jkFKUdfhdso78yi&td' +root_password: 'zevuR6oPh7' + db_names: - clientdb - employeedb diff --git a/test/integration/targets/mysql_user/tasks/issue-64560.yaml b/test/integration/targets/mysql_user/tasks/issue-64560.yaml new file mode 100644 index 0000000000..3851d859ed --- /dev/null +++ b/test/integration/targets/mysql_user/tasks/issue-64560.yaml @@ -0,0 +1,27 @@ +--- + +- name: Set root password + mysql_user: + name: root + password: '{{ root_password }}' + login_user: root + login_password: '{{ root_password }}' + check_implicit_admin: yes + login_unix_socket: '{{ mysql_socket }}' + register: result + +- name: assert root password is changed + assert: { that: "result.changed == true" } + +- name: Set root password again + mysql_user: + name: root + password: '{{ root_password }}' + login_user: root + login_password: '{{ root_password }}' + check_implicit_admin: yes + login_unix_socket: '{{ mysql_socket }}' + register: result + +- name: Assert root password is not changed + assert: { that: "result.changed == false" } diff --git a/test/integration/targets/mysql_user/tasks/main.yml b/test/integration/targets/mysql_user/tasks/main.yml index c3d1e7ef12..e399b6db89 100644 --- a/test/integration/targets/mysql_user/tasks/main.yml +++ b/test/integration/targets/mysql_user/tasks/main.yml @@ -211,3 +211,7 @@ - import_tasks: issue-29511.yaml tags: - issue-29511 + +- import_tasks: issue-64560.yaml + tags: + - issue-64560