Don't return module error when mysql_connect fails (#64560) (#64585)

* Don't return module error when mysql_connect fails (#64560)

mysql_user expects an Exception when using check_implicit_admin.

* Adds integration tests for mysql_user check_implicit_admin (#64560)
This commit is contained in:
Jürgen Hötzel 2019-12-06 06:52:35 +01:00 committed by Abhijit Menon-Sen
parent f21e72d55a
commit 47aea84924
5 changed files with 41 additions and 8 deletions

View file

@ -78,12 +78,8 @@ 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))
if cursor_class == 'DictCursor':
return db_connection.cursor(**{_mysql_cursor_param: mysql_driver.cursors.DictCursor})
else:

View file

@ -479,9 +479,13 @@ def main():
if mysql_driver is None:
module.fail_json(msg=mysql_driver_fail_msg)
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

View file

@ -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

View file

@ -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" }

View file

@ -211,3 +211,7 @@
- import_tasks: issue-29511.yaml
tags:
- issue-29511
- import_tasks: issue-64560.yaml
tags:
- issue-64560