mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-04-05 02:00:31 -07:00
mysql modules: fix failing when \!include_dir is in config file (#47)
* mysql modules: fix failing when \!include_dir is in .my.cnf * Add changelog fragment * fix CI * fix CI * Fix CI * Fix CI * Fix comment
This commit is contained in:
parent
8c79011dbd
commit
82ad06e072
3 changed files with 44 additions and 9 deletions
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- "mysql modules - fix crash when ``!includedir`` option is in config file (https://github.com/ansible-collections/community.mysql/issues/46)."
|
|
@ -16,6 +16,7 @@ __metaclass__ = type
|
|||
import os
|
||||
|
||||
from ansible.module_utils.six.moves import configparser
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
try:
|
||||
import pymysql as mysql_driver
|
||||
|
@ -32,7 +33,10 @@ mysql_driver_fail_msg = 'The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python
|
|||
|
||||
|
||||
def parse_from_mysql_config_file(cnf):
|
||||
cp = configparser.ConfigParser()
|
||||
# Default values of comment_prefix is '#' and ';'.
|
||||
# '!' added to prevent a parsing error
|
||||
# when a config file contains !includedir parameter.
|
||||
cp = configparser.ConfigParser(comment_prefixes=('#', ';', '!'))
|
||||
cp.read(cnf)
|
||||
return cp
|
||||
|
||||
|
@ -44,16 +48,22 @@ def mysql_connect(module, login_user=None, login_password=None, config_file='',
|
|||
|
||||
if config_file and os.path.exists(config_file):
|
||||
config['read_default_file'] = config_file
|
||||
cp = parse_from_mysql_config_file(config_file)
|
||||
# Override some commond defaults with values from config file if needed
|
||||
if cp and cp.has_section('client') and config_overrides_defaults:
|
||||
|
||||
if config_overrides_defaults:
|
||||
try:
|
||||
module.params['login_host'] = cp.get('client', 'host', fallback=module.params['login_host'])
|
||||
module.params['login_port'] = cp.getint('client', 'port', fallback=module.params['login_port'])
|
||||
cp = parse_from_mysql_config_file(config_file)
|
||||
except Exception as e:
|
||||
if "got an unexpected keyword argument 'fallback'" in e.message:
|
||||
module.fail_json(msg='To use config_overrides_defaults, '
|
||||
'it needs Python 3.5+ as the default interpreter on a target host')
|
||||
module.fail_json(msg="Failed to parse %s: %s" % (config_file, to_native(e)))
|
||||
|
||||
# Override some commond defaults with values from config file if needed
|
||||
if cp and cp.has_section('client'):
|
||||
try:
|
||||
module.params['login_host'] = cp.get('client', 'host', fallback=module.params['login_host'])
|
||||
module.params['login_port'] = cp.getint('client', 'port', fallback=module.params['login_port'])
|
||||
except Exception as e:
|
||||
if "got an unexpected keyword argument 'fallback'" in e.message:
|
||||
module.fail_json(msg='To use config_overrides_defaults, '
|
||||
'it needs Python 3.5+ as the default interpreter on a target host')
|
||||
|
||||
if ssl_ca is not None or ssl_key is not None or ssl_cert is not None or check_hostname is not None:
|
||||
config['ssl'] = {}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
config_file: "/root/.my1.cnf"
|
||||
fake_port: 9999
|
||||
fake_host: "blahblah.local"
|
||||
include_dir: "/root/mycnf.d"
|
||||
|
||||
- name: Create custom config file
|
||||
shell: 'echo "[client]" > {{ config_file }}'
|
||||
|
@ -10,6 +11,28 @@
|
|||
- name: Add fake port to config file
|
||||
shell: 'echo "port = {{ fake_port }}" >> {{ config_file }}'
|
||||
|
||||
- name: Get pymysql version
|
||||
shell: pip show pymysql | awk '/Version/ {print $2}'
|
||||
register: pymysql_version
|
||||
|
||||
- name: Add blank line
|
||||
shell: 'echo "" >> {{ config_file }}'
|
||||
when: (pymysql_version.stdout | default('1000', true)) is version('0.9.3', '>=')
|
||||
|
||||
- name: Create include_dir
|
||||
file:
|
||||
path: '{{ include_dir }}'
|
||||
state: directory
|
||||
mode: '0777'
|
||||
when: (pymysql_version.stdout | default('1000', true)) is version('0.9.3', '>=')
|
||||
|
||||
- name: Add include_dir
|
||||
lineinfile:
|
||||
path: '{{ config_file }}'
|
||||
line: '!includedir {{ include_dir }}'
|
||||
insertafter: EOF
|
||||
when: (pymysql_version.stdout | default('1000', true)) is version('0.9.3', '>=')
|
||||
|
||||
- name: Create database using fake port to connect to, must fail
|
||||
mysql_db:
|
||||
login_user: '{{ mysql_user }}'
|
||||
|
|
Loading…
Add table
Reference in a new issue