2013-07-30 16:59:32 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Ansible module to manage mysql variables
( c ) 2013 , Balazs Pocze < banyek @gawker.com >
Certain parts are taken from Mark Theunissen ' s mysqldb module
This file is part of Ansible
Ansible is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Ansible is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Ansible . If not , see < http : / / www . gnu . org / licenses / > .
"""
DOCUMENTATION = '''
- - -
module : mysql_variables
short_description : Manage MySQL global variables
description :
- Query / Set MySQL variables
2013-09-06 19:31:43 +02:00
version_added : 1.3
2015-06-15 21:53:30 +02:00
author : " Balazs Pocze (@banyek) "
2013-07-30 16:59:32 +02:00
options :
variable :
description :
- Variable name to operate
required : True
value :
description :
- If set , then sets variable value to this
required : False
2015-11-11 05:13:48 +01:00
extends_documentation_fragment : mysql
2013-07-30 16:59:32 +02:00
'''
2013-08-08 11:28:26 +02:00
EXAMPLES = '''
2014-05-06 13:25:11 +02:00
# Check for sync_binlog setting
- mysql_variables : variable = sync_binlog
2013-08-08 11:28:26 +02:00
# Set read_only variable to 1
- mysql_variables : variable = read_only value = 1
'''
2013-07-30 16:59:32 +02:00
import warnings
2015-03-26 15:12:18 +01:00
from re import match
2013-07-30 16:59:32 +02:00
try :
import MySQLdb
except ImportError :
mysqldb_found = False
else :
mysqldb_found = True
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
def typedvalue ( value ) :
"""
Convert value to number whenever possible , return same value
otherwise .
>> > typedvalue ( ' 3 ' )
3
>> > typedvalue ( ' 3.0 ' )
3.0
>> > typedvalue ( ' foobar ' )
' foobar '
"""
try :
return int ( value )
except ValueError :
pass
try :
return float ( value )
except ValueError :
pass
return value
2013-07-30 16:59:32 +02:00
def getvariable ( cursor , mysqlvar ) :
2015-03-26 15:12:18 +01:00
cursor . execute ( " SHOW VARIABLES WHERE Variable_name = %s " , ( mysqlvar , ) )
2013-07-30 16:59:32 +02:00
mysqlvar_val = cursor . fetchall ( )
2015-03-26 15:12:18 +01:00
if len ( mysqlvar_val ) is 1 :
return mysqlvar_val [ 0 ] [ 1 ]
else :
return None
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
2013-07-30 16:59:32 +02:00
def setvariable ( cursor , mysqlvar , value ) :
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
""" Set a global mysql variable to a given value
The DB driver will handle quoting of the given value based on its
type , thus numeric strings like ' 3.0 ' or ' 8 ' are illegal , they
should be passed as numeric literals .
"""
2015-03-26 15:12:18 +01:00
query = " SET GLOBAL %s = " % mysql_quote_identifier ( mysqlvar , ' vars ' )
2013-07-30 16:59:32 +02:00
try :
2015-03-26 15:12:18 +01:00
cursor . execute ( query + " %s " , ( value , ) )
2013-07-30 16:59:32 +02:00
cursor . fetchall ( )
result = True
2016-05-18 16:07:21 +02:00
except Exception :
e = get_exception ( )
2013-07-30 16:59:32 +02:00
result = str ( e )
return result
def main ( ) :
module = AnsibleModule (
argument_spec = dict (
login_user = dict ( default = None ) ,
2016-05-02 18:38:07 +02:00
login_password = dict ( default = None , no_log = True ) ,
login_host = dict ( default = " localhost " ) ,
login_port = dict ( default = 3306 , type = ' int ' ) ,
2013-07-30 16:59:32 +02:00
login_unix_socket = dict ( default = None ) ,
variable = dict ( default = None ) ,
2015-11-11 05:13:48 +01:00
value = dict ( default = None ) ,
ssl_cert = dict ( default = None ) ,
ssl_key = dict ( default = None ) ,
ssl_ca = dict ( default = None ) ,
2016-03-10 22:06:39 +01:00
connect_timeout = dict ( default = 30 , type = ' int ' ) ,
2016-05-02 18:38:07 +02:00
config_file = dict ( default = " ~/.my.cnf " , type = " path " )
2013-07-30 16:59:32 +02:00
)
)
user = module . params [ " login_user " ]
password = module . params [ " login_password " ]
2015-11-11 05:13:48 +01:00
ssl_cert = module . params [ " ssl_cert " ]
ssl_key = module . params [ " ssl_key " ]
ssl_ca = module . params [ " ssl_ca " ]
2016-03-10 22:06:39 +01:00
connect_timeout = module . params [ ' connect_timeout ' ]
2015-11-11 05:13:48 +01:00
config_file = module . params [ ' config_file ' ]
db = ' mysql '
2013-07-30 16:59:32 +02:00
mysqlvar = module . params [ " variable " ]
value = module . params [ " value " ]
2015-03-26 15:12:18 +01:00
if mysqlvar is None :
module . fail_json ( msg = " Cannot run without variable to operate with " )
if match ( ' ^[0-9a-z_]+$ ' , mysqlvar ) is None :
2016-02-16 22:58:44 +01:00
module . fail_json ( msg = " invalid variable name \" %s \" " % mysqlvar )
2013-07-30 16:59:32 +02:00
if not mysqldb_found :
module . fail_json ( msg = " the python mysqldb module is required " )
else :
warnings . filterwarnings ( ' error ' , category = MySQLdb . Warning )
try :
2016-03-10 22:06:39 +01:00
cursor = mysql_connect ( module , user , password , config_file , ssl_cert , ssl_key , ssl_ca , db ,
connect_timeout = connect_timeout )
2016-05-18 16:07:21 +02:00
except Exception :
e = get_exception ( )
2015-11-11 05:13:48 +01:00
if os . path . exists ( config_file ) :
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 , e ) )
else :
module . fail_json ( msg = " unable to find %s . Exception message: %s " % ( config_file , e ) )
2013-10-17 19:13:58 +02:00
mysqlvar_val = getvariable ( cursor , mysqlvar )
2015-03-26 15:12:18 +01:00
if mysqlvar_val is None :
module . fail_json ( msg = " Variable not available \" %s \" " % mysqlvar , changed = False )
2013-10-17 19:13:58 +02:00
if value is None :
2013-07-30 16:59:32 +02:00
module . exit_json ( msg = mysqlvar_val )
else :
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
# Type values before using them
value_wanted = typedvalue ( value )
2015-03-26 15:12:18 +01:00
value_actual = typedvalue ( mysqlvar_val )
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
if value_wanted == value_actual :
2013-10-17 19:13:58 +02:00
module . exit_json ( msg = " Variable already set to requested value " , changed = False )
2014-11-25 10:46:09 +01:00
try :
result = setvariable ( cursor , mysqlvar , value_wanted )
2016-05-18 16:07:21 +02:00
except SQLParseError :
e = get_exception ( )
2014-11-25 10:46:09 +01:00
result = str ( e )
2013-07-30 16:59:32 +02:00
if result is True :
Add support for string values
The SET GLOBAL statement requires properly quoting of values. For example, the
following correct queries will fail if quotes are toggled:
mysql> SET GLOBAL innodb_lru_scan_depth = 2000;
mysql> SET GLOBAL master_info_repository = "TABLE";
`mysql_variable` module doesn't quote the value argument, therefore
string values will fail.
# this task will pass, 2000 is passed without quotes
- name: set a numeric value
mysql_variable: variable=innodb_lru_scan_depth value=2000
# this task will fail, TABLE is passed without quotes
- name: set a string value
mysql_variable: variable=master_info_repository value=TABLE
With this patch prepared statements are used. Proper quoting will be
done automatically based on the type of the variables thus an attempt
to convert to int, then to float is done in first place.
Booleans values, ie: ON, OFF, are not specially handled because they
can be quoted. For example, the following queries are correct and
equivalent, they all set _innodb_file_per_table_ to logical _True_:
mysql> SET GLOBAL innodb_file_per_table = "ON";
mysql> SET GLOBAL innodb_file_per_table = ON;
mysql> SET GLOBAL innodb_file_per_table = 1;
Tested in mysql 5.5 and 5.6.
2013-10-18 12:35:40 +02:00
module . exit_json ( msg = " Variable change succeeded prev_value= %s " % value_actual , changed = True )
2013-07-30 16:59:32 +02:00
else :
module . fail_json ( msg = result , changed = False )
2013-12-02 21:13:49 +01:00
# import module snippets
2013-12-02 21:11:23 +01:00
from ansible . module_utils . basic import *
2014-11-25 10:46:09 +01:00
from ansible . module_utils . database import *
2015-11-11 05:13:48 +01:00
from ansible . module_utils . mysql import *
2016-05-02 18:38:07 +02:00
if __name__ == ' __main__ ' :
2016-05-18 16:07:21 +02:00
main ( )