| 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_query_rules |
| 12 |
author: "Ben Mildren (@bmildren)" |
| 13 |
short_description: Modifies query rules using the proxysql admin interface |
| 14 |
description: |
| 15 |
- The M(community.proxysql.proxysql_query_rules) module modifies query rules using the |
| 16 |
proxysql admin interface. |
| 17 |
options: |
| 18 |
rule_id: |
| 19 |
description: |
| 20 |
- The unique id of the rule. Rules are processed in rule_id order. |
| 21 |
type: int |
| 22 |
active: |
| 23 |
description: |
| 24 |
- A rule with I(active) set to C(False) will be tracked in the database, |
| 25 |
but will be never loaded in the in-memory data structures. |
| 26 |
type: bool |
| 27 |
username: |
| 28 |
description: |
| 29 |
- Filtering criteria matching username. If I(username) is non-NULL, a |
| 30 |
query will match only if the connection is made with the correct |
| 31 |
username. |
| 32 |
type: str |
| 33 |
schemaname: |
| 34 |
description: |
| 35 |
- Filtering criteria matching schemaname. If I(schemaname) is non-NULL, a |
| 36 |
query will match only if the connection uses schemaname as its default |
| 37 |
schema. |
| 38 |
type: str |
| 39 |
flagIN: |
| 40 |
description: |
| 41 |
- Used in combination with I(flagOUT) and I(apply) to create chains of |
| 42 |
rules. |
| 43 |
type: int |
| 44 |
client_addr: |
| 45 |
description: |
| 46 |
- Match traffic from a specific source. |
| 47 |
type: str |
| 48 |
proxy_addr: |
| 49 |
description: |
| 50 |
- Match incoming traffic on a specific local IP. |
| 51 |
type: str |
| 52 |
proxy_port: |
| 53 |
description: |
| 54 |
- Match incoming traffic on a specific local port. |
| 55 |
type: int |
| 56 |
digest: |
| 57 |
description: |
| 58 |
- Match queries with a specific digest, as returned by |
| 59 |
stats_mysql_query_digest.digest. |
| 60 |
type: str |
| 61 |
match_digest: |
| 62 |
description: |
| 63 |
- Regular expression that matches the query digest. The dialect of |
| 64 |
regular expressions used is that of re2 - U(https://github.com/google/re2). |
| 65 |
type: str |
| 66 |
match_pattern: |
| 67 |
description: |
| 68 |
- Regular expression that matches the query text. The dialect of regular |
| 69 |
expressions used is that of re2 - U(https://github.com/google/re2). |
| 70 |
type: str |
| 71 |
negate_match_pattern: |
| 72 |
description: |
| 73 |
- If I(negate_match_pattern) is set to C(True), only queries not matching |
| 74 |
the query text will be considered as a match. This acts as a NOT |
| 75 |
operator in front of the regular expression matching against |
| 76 |
match_pattern. |
| 77 |
type: bool |
| 78 |
re_modifiers: |
| 79 |
description: |
| 80 |
- Comma separated list of options to modify the behavior of the RE engine. |
| 81 |
With C(CASELESS) the match is case insensitive. With C(GLOBAL) the replace |
| 82 |
is global (replaces all matches and not just the first). |
| 83 |
For backward compatibility, only C(CASELESS) is the enabled by default. |
| 84 |
type: str |
| 85 |
version_added: "1.3.0" |
| 86 |
flagOUT: |
| 87 |
description: |
| 88 |
- Used in combination with I(flagIN) and apply to create chains of rules. |
| 89 |
When set, I(flagOUT) signifies the I(flagIN) to be used in the next |
| 90 |
chain of rules. |
| 91 |
type: int |
| 92 |
replace_pattern: |
| 93 |
description: |
| 94 |
- This is the pattern with which to replace the matched pattern. Note |
| 95 |
that this is optional, and when omitted, the query processor will only |
| 96 |
cache, route, or set other parameters without rewriting. |
| 97 |
type: str |
| 98 |
destination_hostgroup: |
| 99 |
description: |
| 100 |
- Route matched queries to this hostgroup. This happens unless there is a |
| 101 |
started transaction and the logged in user has |
| 102 |
I(transaction_persistent) set to C(True) (see M(community.proxysql.proxysql_mysql_users)). |
| 103 |
type: int |
| 104 |
cache_ttl: |
| 105 |
description: |
| 106 |
- The number of milliseconds for which to cache the result of the query. |
| 107 |
Note in ProxySQL 1.1 I(cache_ttl) was in seconds. |
| 108 |
type: int |
| 109 |
cache_empty_result: |
| 110 |
description: |
| 111 |
- Controls if resultset without rows will be cached or not. |
| 112 |
type: bool |
| 113 |
version_added: '1.1.0' |
| 114 |
multiplex: |
| 115 |
description: |
| 116 |
- If C(0), multiplex will be disabled. |
| 117 |
- If C(1), try to enable multiplex. There can be other conditions preventing this (for example, user variables or transactions). |
| 118 |
- If C(2), multiplexing is not disabled for just the current query. |
| 119 |
- By default, does not change multiplexing policies. |
| 120 |
type: int |
| 121 |
choices: [0, 1, 2] |
| 122 |
version_added: '1.1.0' |
| 123 |
timeout: |
| 124 |
description: |
| 125 |
- The maximum timeout in milliseconds with which the matched or rewritten |
| 126 |
query should be executed. If a query run for longer than the specific |
| 127 |
threshold, the query is automatically killed. If timeout is not |
| 128 |
specified, the global variable mysql-default_query_timeout applies. |
| 129 |
type: int |
| 130 |
retries: |
| 131 |
description: |
| 132 |
- The maximum number of times a query needs to be re-executed in case of |
| 133 |
detected failure during the execution of the query. If retries is not |
| 134 |
specified, the global variable mysql-query_retries_on_failure applies. |
| 135 |
type: int |
| 136 |
delay: |
| 137 |
description: |
| 138 |
- Number of milliseconds to delay the execution of the query. This is |
| 139 |
essentially a throttling mechanism and QoS, and allows a way to give |
| 140 |
priority to queries over others. This value is added to the |
| 141 |
mysql-default_query_delay global variable that applies to all queries. |
| 142 |
type: int |
| 143 |
next_query_flagIN: |
| 144 |
description: |
| 145 |
- When is set, its value will become the I(flagIN) value for the next queries. |
| 146 |
type: int |
| 147 |
version_added: "1.3.0" |
| 148 |
mirror_flagOUT: |
| 149 |
description: |
| 150 |
- Enables query mirroring. If set I(mirror_flagOUT) can be used to |
| 151 |
evaluates the mirrored query against the specified chain of rules. |
| 152 |
type: int |
| 153 |
mirror_hostgroup: |
| 154 |
description: |
| 155 |
- Enables query mirroring. If set I(mirror_hostgroup) can be used to |
| 156 |
mirror queries to the same or different hostgroup. |
| 157 |
type: int |
| 158 |
OK_msg: |
| 159 |
description: |
| 160 |
- The specified message will be returned for a query that uses the |
| 161 |
defined rule. |
| 162 |
type: str |
| 163 |
version_added: '1.1.0' |
| 164 |
error_msg: |
| 165 |
description: |
| 166 |
- Query will be blocked, and the specified error_msg will be returned to |
| 167 |
the client. |
| 168 |
type: str |
| 169 |
log: |
| 170 |
description: |
| 171 |
- Query will be logged. |
| 172 |
type: bool |
| 173 |
apply: |
| 174 |
description: |
| 175 |
- Used in combination with I(flagIN) and I(flagOUT) to create chains of |
| 176 |
rules. Setting apply to True signifies the last rule to be applied. |
| 177 |
type: bool |
| 178 |
comment: |
| 179 |
description: |
| 180 |
- Free form text field, usable for a descriptive comment of the query |
| 181 |
rule. |
| 182 |
type: str |
| 183 |
state: |
| 184 |
description: |
| 185 |
- When C(present) - adds the rule, when C(absent) - removes the rule. |
| 186 |
type: str |
| 187 |
choices: [ "present", "absent" ] |
| 188 |
default: present |
| 189 |
force_delete: |
| 190 |
description: |
| 191 |
- By default we avoid deleting more than one schedule in a single batch, |
| 192 |
however if you need this behaviour and you are not concerned about the |
| 193 |
schedules deleted, you can set I(force_delete) to C(True). |
| 194 |
type: bool |
| 195 |
default: false |
| 196 |
extends_documentation_fragment: |
| 197 |
- community.proxysql.proxysql.managing_config |
| 198 |
- community.proxysql.proxysql.connectivity |
| 199 |
notes: |
| 200 |
- Supports C(check_mode). |
| 201 |
''' |
| 202 |
|
| 203 |
EXAMPLES = ''' |
| 204 |
--- |
| 205 |
# This example adds a rule to redirect queries from a specific user to another |
| 206 |
# hostgroup, it saves the mysql query rule config to disk, but avoids loading |
| 207 |
# the mysql query config config to runtime (this might be because several |
| 208 |
# rules are being added and the user wants to push the config to runtime in a |
| 209 |
# single batch using the community.general.proxysql_manage_config module). It |
| 210 |
# uses supplied credentials to connect to the proxysql admin interface. |
| 211 |
|
| 212 |
- name: Add a rule |
| 213 |
community.proxysql.proxysql_query_rules: |
| 214 |
login_user: admin |
| 215 |
login_password: admin |
| 216 |
username: 'guest_ro' |
| 217 |
match_pattern: "^SELECT.*" |
| 218 |
destination_hostgroup: 1 |
| 219 |
active: 1 |
| 220 |
retries: 3 |
| 221 |
state: present |
| 222 |
load_to_runtime: false |
| 223 |
|
| 224 |
# This example demonstrates the situation, if your application tries to set a |
| 225 |
# variable that will disable multiplexing, and you think it can be filtered out, |
| 226 |
# you can create a filter that returns OK without executing the request. |
| 227 |
|
| 228 |
- name: Add a filter rule |
| 229 |
community.proxysql.proxysql_query_rules: |
| 230 |
login_user: admin |
| 231 |
login_password: admin |
| 232 |
match_digest: '^SET @@wait_timeout = ?' |
| 233 |
active: 1 |
| 234 |
OK_msg: 'The wait_timeout variable is ignored' |
| 235 |
|
| 236 |
# This example adds a caching rule for a query that matches the digest. |
| 237 |
# The query digest can be obtained from the `stats_mysql_query_digest` |
| 238 |
# table. `cache_ttl` is specified in milliseconds. Empty responses are |
| 239 |
# not cached. |
| 240 |
|
| 241 |
- name: Add a cache rule |
| 242 |
community.proxysql.proxysql_query_rules: |
| 243 |
login_user: admin |
| 244 |
login_password: admin |
| 245 |
rule_id: 1 |
| 246 |
digest: 0xECA450EA500A9A55 |
| 247 |
cache_ttl: 30000 |
| 248 |
cache_empty_result: false |
| 249 |
destination_hostgroup: 1 |
| 250 |
active: true |
| 251 |
state: present |
| 252 |
save_to_disk: true |
| 253 |
load_to_runtime: true |
| 254 |
|
| 255 |
# This example demonstrates how to prevent disabling multiplexing for |
| 256 |
# situations where a request contains @. |
| 257 |
|
| 258 |
- name: Add a rule with multiplex |
| 259 |
community.proxysql.proxysql_query_rules: |
| 260 |
login_user: admin |
| 261 |
login_password: admin |
| 262 |
rule_id: 1 |
| 263 |
active: 1 |
| 264 |
match_digest: '^SELECT @@max_allowed_packet' |
| 265 |
multiplex: 2 |
| 266 |
|
| 267 |
# This example demonstrates how to use next_query_flagIN argument. It allows |
| 268 |
# ProxySQL query rules to be chained. The examples shows how you can have SELECTS |
| 269 |
# immediately follow INSERT/UPDATE/DELETE statements to query the primary hostgroup |
| 270 |
# and avoid replication lag |
| 271 |
|
| 272 |
- name: Add insert query rule |
| 273 |
proxysql_query_rules: |
| 274 |
match_digest: "^INSERT" |
| 275 |
destination_hostgroup: 1, |
| 276 |
next_query_flagIN: 1 |
| 277 |
|
| 278 |
- name: Add update query rule |
| 279 |
proxysql_query_rules: |
| 280 |
match_digest: "^UPDATE" |
| 281 |
destination_hostgroup: 1, |
| 282 |
next_query_flagIN: 1 |
| 283 |
|
| 284 |
- name: Add delete query rules |
| 285 |
proxysql_query_rules: |
| 286 |
match_digest: "^DELETE" |
| 287 |
destination_hostgroup: 1, |
| 288 |
next_query_flagIN: 1 |
| 289 |
|
| 290 |
- name: Add insert query rules |
| 291 |
proxysql_query_rules: |
| 292 |
match_digest: ".*" |
| 293 |
destination_hostgroup: 1, |
| 294 |
next_query_flagIN: 1 |
| 295 |
comment: Match every queries after an INSERT/UPDATE/DELETE query |
| 296 |
|
| 297 |
# This example removes all rules that use the username 'guest_ro', saves the |
| 298 |
# mysql query rule config to disk, and dynamically loads the mysql query rule |
| 299 |
# config to runtime. It uses credentials in a supplied config file to connect |
| 300 |
# to the proxysql admin interface. |
| 301 |
|
| 302 |
- name: Remove rules |
| 303 |
community.proxysql.proxysql_query_rules: |
| 304 |
config_file: '~/proxysql.cnf' |
| 305 |
username: 'guest_ro' |
| 306 |
state: absent |
| 307 |
force_delete: true |
| 308 |
''' |
| 309 |
|
| 310 |
RETURN = ''' |
| 311 |
stdout: |
| 312 |
description: The mysql user modified or removed from proxysql. |
| 313 |
returned: On create/update will return the newly modified rule, in all |
| 314 |
other cases will return a list of rules that match the supplied |
| 315 |
criteria. |
| 316 |
type: dict |
| 317 |
"sample": { |
| 318 |
"changed": true, |
| 319 |
"msg": "Added rule to mysql_query_rules", |
| 320 |
"rules": [ |
| 321 |
{ |
| 322 |
"active": "0", |
| 323 |
"apply": "0", |
| 324 |
"cache_ttl": null, |
| 325 |
"cache_empty_result": null, |
| 326 |
"multiplex": null, |
| 327 |
"client_addr": null, |
| 328 |
"comment": null, |
| 329 |
"delay": null, |
| 330 |
"destination_hostgroup": 1, |
| 331 |
"digest": null, |
| 332 |
"OK_msg": null, |
| 333 |
"error_msg": null, |
| 334 |
"flagIN": "0", |
| 335 |
"flagOUT": null, |
| 336 |
"log": null, |
| 337 |
"match_digest": null, |
| 338 |
"match_pattern": null, |
| 339 |
"mirror_flagOUT": null, |
| 340 |
"mirror_hostgroup": null, |
| 341 |
"negate_match_pattern": "0", |
| 342 |
"proxy_addr": null, |
| 343 |
"proxy_port": null, |
| 344 |
"reconnect": null, |
| 345 |
"replace_pattern": null, |
| 346 |
"retries": null, |
| 347 |
"rule_id": "1", |
| 348 |
"schemaname": null, |
| 349 |
"timeout": null, |
| 350 |
"username": "guest_ro" |
| 351 |
} |
| 352 |
], |
| 353 |
"state": "present" |
| 354 |
} |
| 355 |
''' |
| 356 |
|
| 357 |
from ansible.module_utils.basic import AnsibleModule |
| 358 |
from ansible_collections.community.proxysql.plugins.module_utils.mysql import ( |
| 359 |
mysql_connect, |
| 360 |
mysql_driver, |
| 361 |
proxysql_common_argument_spec, |
| 362 |
save_config_to_disk, |
| 363 |
load_config_to_runtime, |
| 364 |
) |
| 365 |
from ansible.module_utils._text import to_native |
| 366 |
|
| 367 |
# =========================================== |
| 368 |
# proxysql module specific support methods. |
| 369 |
# |
| 370 |
|
| 371 |
|
| 372 |
class ProxyQueryRule(object): |
| 373 |
|
| 374 |
def __init__(self, module, version): |
| 375 |
self.state = module.params["state"] |
| 376 |
self.force_delete = module.params["force_delete"] |
| 377 |
self.save_to_disk = module.params["save_to_disk"] |
| 378 |
self.load_to_runtime = module.params["load_to_runtime"] |
| 379 |
|
| 380 |
config_data_keys = ["rule_id", |
| 381 |
"active", |
| 382 |
"username", |
| 383 |
"schemaname", |
| 384 |
"flagIN", |
| 385 |
"client_addr", |
| 386 |
"proxy_addr", |
| 387 |
"proxy_port", |
| 388 |
"digest", |
| 389 |
"match_digest", |
| 390 |
"match_pattern", |
| 391 |
"negate_match_pattern", |
| 392 |
"re_modifiers", |
| 393 |
"flagOUT", |
| 394 |
"replace_pattern", |
| 395 |
"destination_hostgroup", |
| 396 |
"cache_ttl", |
| 397 |
"multiplex", |
| 398 |
"timeout", |
| 399 |
"retries", |
| 400 |
"delay", |
| 401 |
"next_query_flagIN", |
| 402 |
"mirror_flagOUT", |
| 403 |
"mirror_hostgroup", |
| 404 |
"error_msg", |
| 405 |
"OK_msg", |
| 406 |
"multiplex", |
| 407 |
"log", |
| 408 |
"apply", |
| 409 |
"comment"] |
| 410 |
|
| 411 |
if version.get('major') >= 2: |
| 412 |
config_data_keys.append("cache_empty_result") |
| 413 |
|
| 414 |
self.config_data = dict((k, module.params[k]) |
| 415 |
for k in config_data_keys) |
| 416 |
|
| 417 |
def check_rule_pk_exists(self, cursor): |
| 418 |
query_string = \ |
| 419 |
"""SELECT count(*) AS `rule_count` |
| 420 |
FROM mysql_query_rules |
| 421 |
WHERE rule_id = %s""" |
| 422 |
|
| 423 |
query_data = \ |
| 424 |
[self.config_data["rule_id"]] |
| 425 |
|
| 426 |
cursor.execute(query_string, query_data) |
| 427 |
check_count = cursor.fetchone() |
| 428 |
return (int(check_count['rule_count']) > 0) |
| 429 |
|
| 430 |
def check_rule_cfg_exists(self, cursor): |
| 431 |
query_string = \ |
| 432 |
"""SELECT count(*) AS `rule_count` |
| 433 |
FROM mysql_query_rules""" |
| 434 |
|
| 435 |
cols = 0 |
| 436 |
query_data = [] |
| 437 |
|
| 438 |
for col, val in self.config_data.items(): |
| 439 |
if val is not None: |
| 440 |
cols += 1 |
| 441 |
query_data.append(val) |
| 442 |
if cols == 1: |
| 443 |
query_string += "\n WHERE " + col + " = %s" |
| 444 |
else: |
| 445 |
query_string += "\n AND " + col + " = %s" |
| 446 |
|
| 447 |
if cols > 0: |
| 448 |
cursor.execute(query_string, query_data) |
| 449 |
else: |
| 450 |
cursor.execute(query_string) |
| 451 |
check_count = cursor.fetchone() |
| 452 |
return int(check_count['rule_count']) |
| 453 |
|
| 454 |
def get_rule_config(self, cursor, created_rule_id=None): |
| 455 |
query_string = \ |
| 456 |
"""SELECT * |
| 457 |
FROM mysql_query_rules""" |
| 458 |
|
| 459 |
if created_rule_id: |
| 460 |
query_data = [created_rule_id, ] |
| 461 |
query_string += "\nWHERE rule_id = %s" |
| 462 |
|
| 463 |
cursor.execute(query_string, query_data) |
| 464 |
rule = cursor.fetchone() |
| 465 |
else: |
| 466 |
cols = 0 |
| 467 |
query_data = [] |
| 468 |
|
| 469 |
for col, val in self.config_data.items(): |
| 470 |
if val is not None: |
| 471 |
cols += 1 |
| 472 |
query_data.append(val) |
| 473 |
if cols == 1: |
| 474 |
query_string += "\n WHERE " + col + " = %s" |
| 475 |
else: |
| 476 |
query_string += "\n AND " + col + " = %s" |
| 477 |
|
| 478 |
if cols > 0: |
| 479 |
cursor.execute(query_string, query_data) |
| 480 |
else: |
| 481 |
cursor.execute(query_string) |
| 482 |
rule = cursor.fetchall() |
| 483 |
|
| 484 |
return rule |
| 485 |
|
| 486 |
def create_rule_config(self, cursor): |
| 487 |
query_string = \ |
| 488 |
"""INSERT INTO mysql_query_rules (""" |
| 489 |
|
| 490 |
cols = 0 |
| 491 |
query_data = [] |
| 492 |
|
| 493 |
for col, val in self.config_data.items(): |
| 494 |
if val is not None: |
| 495 |
cols += 1 |
| 496 |
query_data.append(val) |
| 497 |
query_string += "\n" + col + "," |
| 498 |
|
| 499 |
query_string = query_string[:-1] |
| 500 |
|
| 501 |
query_string += \ |
| 502 |
(")\n" + |
| 503 |
"VALUES (" + |
| 504 |
"%s ," * cols) |
| 505 |
|
| 506 |
query_string = query_string[:-2] |
| 507 |
query_string += ")" |
| 508 |
|
| 509 |
cursor.execute(query_string, query_data) |
| 510 |
new_rule_id = cursor.lastrowid |
| 511 |
return True, new_rule_id |
| 512 |
|
| 513 |
def update_rule_config(self, cursor): |
| 514 |
query_string = """UPDATE mysql_query_rules""" |
| 515 |
|
| 516 |
cols = 0 |
| 517 |
query_data = [] |
| 518 |
|
| 519 |
for col, val in self.config_data.items(): |
| 520 |
if val is not None and col != "rule_id": |
| 521 |
cols += 1 |
| 522 |
query_data.append(val) |
| 523 |
if cols == 1: |
| 524 |
query_string += "\nSET " + col + "= %s," |
| 525 |
else: |
| 526 |
query_string += "\n " + col + " = %s," |
| 527 |
|
| 528 |
query_string = query_string[:-1] |
| 529 |
query_string += "\nWHERE rule_id = %s" |
| 530 |
|
| 531 |
query_data.append(self.config_data["rule_id"]) |
| 532 |
|
| 533 |
cursor.execute(query_string, query_data) |
| 534 |
return True |
| 535 |
|
| 536 |
def delete_rule_config(self, cursor): |
| 537 |
query_string = \ |
| 538 |
"""DELETE FROM mysql_query_rules""" |
| 539 |
|
| 540 |
cols = 0 |
| 541 |
query_data = [] |
| 542 |
|
| 543 |
for col, val in self.config_data.items(): |
| 544 |
if val is not None: |
| 545 |
cols += 1 |
| 546 |
query_data.append(val) |
| 547 |
if cols == 1: |
| 548 |
query_string += "\n WHERE " + col + " = %s" |
| 549 |
else: |
| 550 |
query_string += "\n AND " + col + " = %s" |
| 551 |
|
| 552 |
if cols > 0: |
| 553 |
cursor.execute(query_string, query_data) |
| 554 |
else: |
| 555 |
cursor.execute(query_string) |
| 556 |
check_count = cursor.rowcount |
| 557 |
return True, int(check_count) |
| 558 |
|
| 559 |
def manage_config(self, cursor, state): |
| 560 |
if state: |
| 561 |
if self.save_to_disk: |
| 562 |
save_config_to_disk(cursor, "QUERY RULES") |
| 563 |
if self.load_to_runtime: |
| 564 |
load_config_to_runtime(cursor, "QUERY RULES") |
| 565 |
|
| 566 |
def create_rule(self, check_mode, result, cursor): |
| 567 |
if not check_mode: |
| 568 |
result['changed'], new_rule_id = \ |
| 569 |
self.create_rule_config(cursor) |
| 570 |
result['msg'] = "Added rule to mysql_query_rules" |
| 571 |
self.manage_config(cursor, |
| 572 |
result['changed']) |
| 573 |
result['rules'] = \ |
| 574 |
self.get_rule_config(cursor, new_rule_id) |
| 575 |
else: |
| 576 |
result['changed'] = True |
| 577 |
result['msg'] = ("Rule would have been added to" + |
| 578 |
" mysql_query_rules, however" + |
| 579 |
" check_mode is enabled.") |
| 580 |
|
| 581 |
def update_rule(self, check_mode, result, cursor): |
| 582 |
if not check_mode: |
| 583 |
result['changed'] = \ |
| 584 |
self.update_rule_config(cursor) |
| 585 |
result['msg'] = "Updated rule in mysql_query_rules" |
| 586 |
self.manage_config(cursor, |
| 587 |
result['changed']) |
| 588 |
result['rules'] = \ |
| 589 |
self.get_rule_config(cursor) |
| 590 |
else: |
| 591 |
result['changed'] = True |
| 592 |
result['msg'] = ("Rule would have been updated in" + |
| 593 |
" mysql_query_rules, however" + |
| 594 |
" check_mode is enabled.") |
| 595 |
|
| 596 |
def delete_rule(self, check_mode, result, cursor): |
| 597 |
if not check_mode: |
| 598 |
result['rules'] = \ |
| 599 |
self.get_rule_config(cursor) |
| 600 |
result['changed'], result['rows_affected'] = \ |
| 601 |
self.delete_rule_config(cursor) |
| 602 |
result['msg'] = "Deleted rule from mysql_query_rules" |
| 603 |
self.manage_config(cursor, |
| 604 |
result['changed']) |
| 605 |
else: |
| 606 |
result['changed'] = True |
| 607 |
result['msg'] = ("Rule would have been deleted from" + |
| 608 |
" mysql_query_rules, however" + |
| 609 |
" check_mode is enabled.") |
| 610 |
|
| 611 |
# =========================================== |
| 612 |
# Module execution. |
| 613 |
# |
| 614 |
|
| 615 |
|
| 616 |
def main(): |
| 617 |
argument_spec = proxysql_common_argument_spec() |
| 618 |
argument_spec.update( |
| 619 |
rule_id=dict(type='int'), |
| 620 |
active=dict(type='bool'), |
| 621 |
username=dict(type='str'), |
| 622 |
schemaname=dict(type='str'), |
| 623 |
flagIN=dict(type='int'), |
| 624 |
client_addr=dict(type='str'), |
| 625 |
proxy_addr=dict(type='str'), |
| 626 |
proxy_port=dict(type='int'), |
| 627 |
digest=dict(type='str'), |
| 628 |
match_digest=dict(type='str'), |
| 629 |
match_pattern=dict(type='str'), |
| 630 |
negate_match_pattern=dict(type='bool'), |
| 631 |
re_modifiers=dict(type='str'), |
| 632 |
flagOUT=dict(type='int'), |
| 633 |
replace_pattern=dict(type='str'), |
| 634 |
destination_hostgroup=dict(type='int'), |
| 635 |
cache_ttl=dict(type='int'), |
| 636 |
cache_empty_result=dict(type='bool'), |
| 637 |
multiplex=dict(type='int', choices=[0, 1, 2]), |
| 638 |
timeout=dict(type='int'), |
| 639 |
retries=dict(type='int'), |
| 640 |
delay=dict(type='int'), |
| 641 |
next_query_flagIN=dict(type='int'), |
| 642 |
mirror_flagOUT=dict(type='int'), |
| 643 |
mirror_hostgroup=dict(type='int'), |
| 644 |
OK_msg=dict(type='str'), |
| 645 |
error_msg=dict(type='str'), |
| 646 |
log=dict(type='bool'), |
| 647 |
apply=dict(type='bool'), |
| 648 |
comment=dict(type='str'), |
| 649 |
state=dict(default='present', choices=['present', |
| 650 |
'absent']), |
| 651 |
force_delete=dict(default=False, type='bool'), |
| 652 |
save_to_disk=dict(default=True, type='bool'), |
| 653 |
load_to_runtime=dict(default=True, type='bool') |
| 654 |
) |
| 655 |
|
| 656 |
module = AnsibleModule( |
| 657 |
supports_check_mode=True, |
| 658 |
argument_spec=argument_spec |
| 659 |
) |
| 660 |
|
| 661 |
login_user = module.params["login_user"] |
| 662 |
login_password = module.params["login_password"] |
| 663 |
config_file = module.params["config_file"] |
| 664 |
|
| 665 |
cursor = None |
| 666 |
try: |
| 667 |
cursor, db_conn, version = mysql_connect(module, |
| 668 |
login_user, |
| 669 |
login_password, |
| 670 |
config_file, |
| 671 |
cursor_class='DictCursor') |
| 672 |
except mysql_driver.Error as e: |
| 673 |
module.fail_json( |
| 674 |
msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e) |
| 675 |
) |
| 676 |
|
| 677 |
proxysql_query_rule = ProxyQueryRule(module, version) |
| 678 |
result = {} |
| 679 |
|
| 680 |
result['state'] = proxysql_query_rule.state |
| 681 |
|
| 682 |
if proxysql_query_rule.state == "present": |
| 683 |
try: |
| 684 |
if not proxysql_query_rule.check_rule_cfg_exists(cursor): |
| 685 |
if proxysql_query_rule.config_data["rule_id"] and \ |
| 686 |
proxysql_query_rule.check_rule_pk_exists(cursor): |
| 687 |
proxysql_query_rule.update_rule(module.check_mode, |
| 688 |
result, |
| 689 |
cursor) |
| 690 |
else: |
| 691 |
proxysql_query_rule.create_rule(module.check_mode, |
| 692 |
result, |
| 693 |
cursor) |
| 694 |
else: |
| 695 |
result['changed'] = False |
| 696 |
result['msg'] = ("The rule already exists in" + |
| 697 |
" mysql_query_rules and doesn't need to be" + |
| 698 |
" updated.") |
| 699 |
result['rules'] = \ |
| 700 |
proxysql_query_rule.get_rule_config(cursor) |
| 701 |
|
| 702 |
except mysql_driver.Error as e: |
| 703 |
module.fail_json( |
| 704 |
msg="unable to modify rule.. %s" % to_native(e) |
| 705 |
) |
| 706 |
|
| 707 |
elif proxysql_query_rule.state == "absent": |
| 708 |
try: |
| 709 |
existing_rules = proxysql_query_rule.check_rule_cfg_exists(cursor) |
| 710 |
if existing_rules > 0: |
| 711 |
if existing_rules == 1 or \ |
| 712 |
proxysql_query_rule.force_delete: |
| 713 |
proxysql_query_rule.delete_rule(module.check_mode, |
| 714 |
result, |
| 715 |
cursor) |
| 716 |
else: |
| 717 |
module.fail_json( |
| 718 |
msg=("Operation would delete multiple rules" + |
| 719 |
" use force_delete to override this") |
| 720 |
) |
| 721 |
else: |
| 722 |
result['changed'] = False |
| 723 |
result['msg'] = ("The rule is already absent from the" + |
| 724 |
" mysql_query_rules memory configuration") |
| 725 |
except mysql_driver.Error as e: |
| 726 |
module.fail_json( |
| 727 |
msg="unable to remove rule.. %s" % to_native(e) |
| 728 |
) |
| 729 |
|
| 730 |
module.exit_json(**result) |
| 731 |
|
| 732 |
|
| 733 |
if __name__ == '__main__': |
| 734 |
main() |