This commit is contained in:
Andrew Klychkov 2021-04-13 11:22:45 +02:00
parent 915d125d70
commit 83b76a4804
3 changed files with 22 additions and 4 deletions

View file

@ -100,6 +100,8 @@ def mysql_connect(module, login_user=None, login_password=None, config_file='',
if charset is not None:
config['charset'] = charset
module.warn('%s' % config)
if _mysql_cursor_param == 'cursor':
# In case of PyMySQL driver:
db_connection = mysql_driver.connect(autocommit=autocommit, **config)
@ -134,7 +136,7 @@ def mysql_common_argument_spec():
client_key=dict(type='path', aliases=['ssl_key']),
ca_cert=dict(type='path', aliases=['ssl_ca']),
check_hostname=dict(type='bool', default=None),
charset=dict(type='str'),
charset=dict(type='str', default=None),
)

View file

@ -1177,13 +1177,15 @@ def main():
if check_implicit_admin:
try:
cursor, db_conn = mysql_connect(module, "root", "", config_file, ssl_cert, ssl_key, ssl_ca, db,
connect_timeout=connect_timeout, check_hostname=check_hostname, charset=charset)
connect_timeout=connect_timeout, check_hostname=check_hostname,
charset=charset)
except Exception:
pass
if not cursor:
cursor, db_conn = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca, db,
connect_timeout=connect_timeout, check_hostname=check_hostname)
connect_timeout=connect_timeout, check_hostname=check_hostname,
charset=charset)
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)))

View file

@ -49,8 +49,21 @@
<<: *mysql_params
name: '{{ user_name_5 }}'
password: '{{ user_password_5 }}'
priv: '*.*:SELECT'
state: present
- name: Create user with default charset again
mysql_user:
<<: *mysql_params
name: '{{ user_name_5 }}'
password: '{{ user_password_5 }}'
state: present
priv: '*.*:SELECT'
register: result
- assert:
that: result is not changed
- name: Try to get connect and get info, must fail
mysql_query:
login_host: 127.0.0.1
@ -79,9 +92,10 @@
name: '{{ user_name_5 }}'
password: '{{ user_password_5 }}'
state: present
priv: '*.*:SELECT'
charset: utf8mb4
- name: Try to get connect and get info, must succeed
- name: Try to connect and get info, must succeed
mysql_query:
login_host: 127.0.0.1
login_port: '{{ mysql_primary_port }}'