| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: proxysql_manage_config |
| 12 |
|
| 13 |
author: "Ben Mildren (@bmildren)" |
| 14 |
short_description: Writes the proxysql configuration settings between layers |
| 15 |
description: |
| 16 |
- The M(community.proxysql.proxysql_global_variables) module writes the proxysql configuration |
| 17 |
settings between layers. Currently this module will always report a |
| 18 |
changed state, so should typically be used with WHEN however this will |
| 19 |
change in a future version when the CHECKSUM table commands are available |
| 20 |
for all tables in proxysql. |
| 21 |
options: |
| 22 |
action: |
| 23 |
description: |
| 24 |
- The supplied I(action) combines with the supplied I(direction) to |
| 25 |
provide the semantics of how we want to move the I(config_settings) |
| 26 |
between the I(config_layers). |
| 27 |
type: str |
| 28 |
choices: [ "LOAD", "SAVE" ] |
| 29 |
required: true |
| 30 |
config_settings: |
| 31 |
description: |
| 32 |
- The I(config_settings) specifies which configuration we're writing. |
| 33 |
type: str |
| 34 |
choices: [ "MYSQL USERS", "MYSQL SERVERS", "MYSQL QUERY RULES", |
| 35 |
"MYSQL VARIABLES", "PGSQL USERS", "PGSQL SERVERS", |
| 36 |
"PGSQL QUERY RULES", "PGSQL VARIABLES", |
| 37 |
"ADMIN VARIABLES", "SCHEDULER" ] |
| 38 |
required: true |
| 39 |
direction: |
| 40 |
description: |
| 41 |
- FROM - denotes we're reading values FROM the supplied I(config_layer) |
| 42 |
and writing to the next layer. |
| 43 |
TO - denotes we're reading from the previous layer and writing TO the |
| 44 |
supplied I(config_layer)." |
| 45 |
type: str |
| 46 |
choices: [ "FROM", "TO" ] |
| 47 |
required: true |
| 48 |
config_layer: |
| 49 |
description: |
| 50 |
- RUNTIME - represents the in-memory data structures of ProxySQL used by |
| 51 |
the threads that are handling the requests. |
| 52 |
MEMORY - (sometimes also referred as main) represents the in-memory |
| 53 |
SQLite3 database. |
| 54 |
DISK - represents the on-disk SQLite3 database. |
| 55 |
CONFIG - is the classical config file. You can only LOAD FROM the |
| 56 |
config file. |
| 57 |
type: str |
| 58 |
choices: [ "MEMORY", "DISK", "RUNTIME", "CONFIG" ] |
| 59 |
required: true |
| 60 |
extends_documentation_fragment: |
| 61 |
- community.proxysql.proxysql.connectivity |
| 62 |
notes: |
| 63 |
- Supports C(check_mode). |
| 64 |
''' |
| 65 |
|
| 66 |
EXAMPLES = ''' |
| 67 |
--- |
| 68 |
# This example saves the mysql users config from memory to disk. It uses |
| 69 |
# supplied credentials to connect to the proxysql admin interface. |
| 70 |
|
| 71 |
- name: Save the mysql users config from memory to disk |
| 72 |
community.proxysql.proxysql_manage_config: |
| 73 |
login_user: 'admin' |
| 74 |
login_password: 'admin' |
| 75 |
action: "SAVE" |
| 76 |
config_settings: "MYSQL USERS" |
| 77 |
direction: "FROM" |
| 78 |
config_layer: "MEMORY" |
| 79 |
|
| 80 |
# This example loads the mysql query rules config from memory to runtime. It |
| 81 |
# uses supplied credentials to connect to the proxysql admin interface. |
| 82 |
|
| 83 |
- name: Load the mysql query rules config from memory to runtime |
| 84 |
community.proxysql.proxysql_manage_config: |
| 85 |
config_file: '~/proxysql.cnf' |
| 86 |
action: "LOAD" |
| 87 |
config_settings: "MYSQL QUERY RULES" |
| 88 |
direction: "TO" |
| 89 |
config_layer: "RUNTIME" |
| 90 |
''' |
| 91 |
|
| 92 |
RETURN = ''' |
| 93 |
stdout: |
| 94 |
description: Simply reports whether the action reported a change. |
| 95 |
returned: Currently the returned value with always be changed=True. |
| 96 |
type: dict |
| 97 |
"sample": { |
| 98 |
"changed": true |
| 99 |
} |
| 100 |
''' |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
from ansible_collections.community.proxysql.plugins.module_utils.mysql import ( |
| 104 |
mysql_connect, |
| 105 |
mysql_driver, |
| 106 |
proxysql_common_argument_spec |
| 107 |
) |
| 108 |
from ansible.module_utils._text import to_native |
| 109 |
|
| 110 |
# =========================================== |
| 111 |
# proxysql module specific support methods. |
| 112 |
# |
| 113 |
|
| 114 |
|
| 115 |
def perform_checks(module): |
| 116 |
if module.params["config_layer"] == 'CONFIG' and \ |
| 117 |
(module.params["action"] != 'LOAD' or |
| 118 |
module.params["direction"] != 'FROM'): |
| 119 |
|
| 120 |
if (module.params["action"] != 'LOAD' and |
| 121 |
module.params["direction"] != 'FROM'): |
| 122 |
msg_string = ("Neither the action \"%s\" nor the direction" + |
| 123 |
" \"%s\" are valid combination with the CONFIG" + |
| 124 |
" config_layer") |
| 125 |
module.fail_json(msg=msg_string % (module.params["action"], |
| 126 |
module.params["direction"])) |
| 127 |
|
| 128 |
elif module.params["action"] != 'LOAD': |
| 129 |
msg_string = ("The action \"%s\" is not a valid combination" + |
| 130 |
" with the CONFIG config_layer") |
| 131 |
module.fail_json(msg=msg_string % module.params["action"]) |
| 132 |
|
| 133 |
else: |
| 134 |
msg_string = ("The direction \"%s\" is not a valid combination" + |
| 135 |
" with the CONFIG config_layer") |
| 136 |
module.fail_json(msg=msg_string % module.params["direction"]) |
| 137 |
|
| 138 |
|
| 139 |
def manage_config(manage_config_settings, cursor, check_mode): |
| 140 |
|
| 141 |
if not check_mode: |
| 142 |
query_string = "%s" % ' '.join(manage_config_settings) |
| 143 |
cursor.execute(query_string) |
| 144 |
|
| 145 |
return True |
| 146 |
|
| 147 |
# =========================================== |
| 148 |
# Module execution. |
| 149 |
# |
| 150 |
|
| 151 |
|
| 152 |
def main(): |
| 153 |
argument_spec = proxysql_common_argument_spec() |
| 154 |
argument_spec.update( |
| 155 |
action=dict(required=True, choices=['LOAD', |
| 156 |
'SAVE']), |
| 157 |
config_settings=dict(required=True, choices=['MYSQL USERS', |
| 158 |
'MYSQL SERVERS', |
| 159 |
'MYSQL QUERY RULES', |
| 160 |
'MYSQL VARIABLES', |
| 161 |
'PGSQL USERS', |
| 162 |
'PGSQL SERVERS', |
| 163 |
'PGSQL QUERY RULES', |
| 164 |
'PGSQL VARIABLES', |
| 165 |
'ADMIN VARIABLES', |
| 166 |
'SCHEDULER']), |
| 167 |
direction=dict(required=True, choices=['FROM', |
| 168 |
'TO']), |
| 169 |
config_layer=dict(required=True, choices=['MEMORY', |
| 170 |
'DISK', |
| 171 |
'RUNTIME', |
| 172 |
'CONFIG']) |
| 173 |
) |
| 174 |
|
| 175 |
module = AnsibleModule( |
| 176 |
supports_check_mode=True, |
| 177 |
argument_spec=argument_spec |
| 178 |
) |
| 179 |
|
| 180 |
perform_checks(module) |
| 181 |
|
| 182 |
login_user = module.params["login_user"] |
| 183 |
login_password = module.params["login_password"] |
| 184 |
config_file = module.params["config_file"] |
| 185 |
action = module.params["action"] |
| 186 |
config_settings = module.params["config_settings"] |
| 187 |
direction = module.params["direction"] |
| 188 |
config_layer = module.params["config_layer"] |
| 189 |
|
| 190 |
cursor = None |
| 191 |
try: |
| 192 |
cursor, db_conn, version = mysql_connect(module, |
| 193 |
login_user, |
| 194 |
login_password, |
| 195 |
config_file) |
| 196 |
except mysql_driver.Error as e: |
| 197 |
module.fail_json( |
| 198 |
msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e) |
| 199 |
) |
| 200 |
|
| 201 |
result = {} |
| 202 |
|
| 203 |
manage_config_settings = \ |
| 204 |
[action, config_settings, direction, config_layer] |
| 205 |
|
| 206 |
try: |
| 207 |
result['changed'] = manage_config(manage_config_settings, |
| 208 |
cursor, module.check_mode) |
| 209 |
except mysql_driver.Error as e: |
| 210 |
module.fail_json( |
| 211 |
msg="unable to manage config.. %s" % to_native(e) |
| 212 |
) |
| 213 |
|
| 214 |
module.exit_json(**result) |
| 215 |
|
| 216 |
|
| 217 |
if __name__ == '__main__': |
| 218 |
main() |