mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-04-08 11:40:33 -07:00
mysql modules: fix failing when \!include_dir is in .my.cnf
This commit is contained in:
parent
8c79011dbd
commit
4855ac4d24
2 changed files with 29 additions and 9 deletions
|
@ -16,6 +16,7 @@ __metaclass__ = type
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from ansible.module_utils.six.moves import configparser
|
from ansible.module_utils.six.moves import configparser
|
||||||
|
from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pymysql as mysql_driver
|
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):
|
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 !include_dir parameter.
|
||||||
|
cp = configparser.ConfigParser(comment_prefixes=('#', ';', '!'))
|
||||||
cp.read(cnf)
|
cp.read(cnf)
|
||||||
return cp
|
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):
|
if config_file and os.path.exists(config_file):
|
||||||
config['read_default_file'] = 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 config_overrides_defaults:
|
||||||
if cp and cp.has_section('client') and config_overrides_defaults:
|
|
||||||
try:
|
try:
|
||||||
module.params['login_host'] = cp.get('client', 'host', fallback=module.params['login_host'])
|
cp = parse_from_mysql_config_file(config_file)
|
||||||
module.params['login_port'] = cp.getint('client', 'port', fallback=module.params['login_port'])
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if "got an unexpected keyword argument 'fallback'" in e.message:
|
module.fail_json(msg="Failed to parse %s: %s" % (config_file, to_native(e)))
|
||||||
module.fail_json(msg='To use config_overrides_defaults, '
|
|
||||||
'it needs Python 3.5+ as the default interpreter on a target host')
|
# 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:
|
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'] = {}
|
config['ssl'] = {}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
config_file: "/root/.my1.cnf"
|
config_file: "/root/.my1.cnf"
|
||||||
fake_port: 9999
|
fake_port: 9999
|
||||||
fake_host: "blahblah.local"
|
fake_host: "blahblah.local"
|
||||||
|
include_dir: "/root/.my.cnf.d"
|
||||||
|
|
||||||
- name: Create custom config file
|
- name: Create custom config file
|
||||||
shell: 'echo "[client]" > {{ config_file }}'
|
shell: 'echo "[client]" > {{ config_file }}'
|
||||||
|
@ -10,6 +11,15 @@
|
||||||
- name: Add fake port to config file
|
- name: Add fake port to config file
|
||||||
shell: 'echo "port = {{ fake_port }}" >> {{ config_file }}'
|
shell: 'echo "port = {{ fake_port }}" >> {{ config_file }}'
|
||||||
|
|
||||||
|
- name: Create include_dir
|
||||||
|
file:
|
||||||
|
path: '{{ include_dir }}'
|
||||||
|
state: directory
|
||||||
|
mode: '0777'
|
||||||
|
|
||||||
|
- name: Add include_dir
|
||||||
|
shell: 'echo "!include_dir {{ include_dir }}" >> {{ config_file }}'
|
||||||
|
|
||||||
- name: Create database using fake port to connect to, must fail
|
- name: Create database using fake port to connect to, must fail
|
||||||
mysql_db:
|
mysql_db:
|
||||||
login_user: '{{ mysql_user }}'
|
login_user: '{{ mysql_user }}'
|
||||||
|
|
Loading…
Add table
Reference in a new issue