community.proxysql.proxysql_query_rules (1.5.1) — module

Modifies query rules using the proxysql admin interface

Authors: Ben Mildren (@bmildren)

Install collection

Install with ansible-galaxy collection install community.proxysql:==1.5.1


Add to requirements.yml

  collections:
    - name: community.proxysql
      version: 1.5.1

Description

The M(community.proxysql.proxysql_query_rules) module modifies query rules using the proxysql admin interface.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
---
# This example adds a rule to redirect queries from a specific user to another
# hostgroup, it saves the mysql query rule config to disk, but avoids loading
# the mysql query config config to runtime (this might be because several
# rules are being added and the user wants to push the config to runtime in a
# single batch using the community.general.proxysql_manage_config module). It
# uses supplied credentials to connect to the proxysql admin interface.

- name: Add a rule
  community.proxysql.proxysql_query_rules:
    login_user: admin
    login_password: admin
    username: 'guest_ro'
    match_pattern: "^SELECT.*"
    destination_hostgroup: 1
    active: 1
    retries: 3
    state: present
    load_to_runtime: false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# This example demonstrates the situation, if your application tries to set a
# variable that will disable multiplexing, and you think it can be filtered out,
# you can create a filter that returns OK without executing the request.

- name: Add a filter rule
  community.proxysql.proxysql_query_rules:
    login_user: admin
    login_password: admin
    match_digest: '^SET @@wait_timeout = ?'
    active: 1
    OK_msg: 'The wait_timeout variable is ignored'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# This example adds a caching rule for a query that matches the digest.
# The query digest can be obtained from the `stats_mysql_query_digest`
# table. `cache_ttl` is specified in milliseconds. Empty responses are
# not cached.

- name: Add a cache rule
  community.proxysql.proxysql_query_rules:
    login_user: admin
    login_password: admin
    rule_id: 1
    digest: 0xECA450EA500A9A55
    cache_ttl: 30000
    cache_empty_result: false
    destination_hostgroup: 1
    active: true
    state: present
    save_to_disk: true
    load_to_runtime: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# This example demonstrates how to prevent disabling multiplexing for
# situations where a request contains @.

- name: Add a rule with multiplex
  community.proxysql.proxysql_query_rules:
    login_user: admin
    login_password: admin
    rule_id: 1
    active: 1
    match_digest: '^SELECT @@max_allowed_packet'
    multiplex: 2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# This example demonstrates how to use next_query_flagIN argument. It allows
# ProxySQL query rules to be chained. The examples shows how you can have SELECTS
# immediately follow INSERT/UPDATE/DELETE statements to query the primary hostgroup
# and avoid replication lag

- name: Add insert query rule
  proxysql_query_rules:
    match_digest: "^INSERT"
    destination_hostgroup: 1,
    next_query_flagIN: 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add update query rule
  proxysql_query_rules:
    match_digest: "^UPDATE"
    destination_hostgroup: 1,
    next_query_flagIN: 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add delete query rules
  proxysql_query_rules:
    match_digest: "^DELETE"
    destination_hostgroup: 1,
    next_query_flagIN: 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add insert query rules
  proxysql_query_rules:
    match_digest: ".*"
    destination_hostgroup: 1,
    next_query_flagIN: 1
    comment: Match every queries after an INSERT/UPDATE/DELETE query
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# This example removes all rules that use the username 'guest_ro', saves the
# mysql query rule config to disk, and dynamically loads the mysql query rule
# config to runtime.  It uses credentials in a supplied config file to connect
# to the proxysql admin interface.

- name: Remove rules
  community.proxysql.proxysql_query_rules:
    config_file: '~/proxysql.cnf'
    username: 'guest_ro'
    state: absent
    force_delete: true

Inputs

    
log:
    description:
    - Query will be logged.
    type: bool

apply:
    description:
    - Used in combination with I(flagIN) and I(flagOUT) to create chains of rules. Setting
      apply to True signifies the last rule to be applied.
    type: bool

delay:
    description:
    - Number of milliseconds to delay the execution of the query. This is essentially
      a throttling mechanism and QoS, and allows a way to give priority to queries over
      others. This value is added to the mysql-default_query_delay global variable that
      applies to all queries.
    type: int

state:
    choices:
    - present
    - absent
    default: present
    description:
    - When C(present) - adds the rule, when C(absent) - removes the rule.
    type: str

OK_msg:
    description:
    - The specified message will be returned for a query that uses the defined rule.
    type: str
    version_added: 1.1.0
    version_added_collection: community.proxysql

active:
    description:
    - A rule with I(active) set to C(False) will be tracked in the database, but will
      be never loaded in the in-memory data structures.
    type: bool

digest:
    description:
    - Match queries with a specific digest, as returned by stats_mysql_query_digest.digest.
    type: str

flagIN:
    description:
    - Used in combination with I(flagOUT) and I(apply) to create chains of rules.
    type: int

comment:
    description:
    - Free form text field, usable for a descriptive comment of the query rule.
    type: str

flagOUT:
    description:
    - Used in combination with I(flagIN) and apply to create chains of rules. When set,
      I(flagOUT) signifies the I(flagIN) to be used in the next chain of rules.
    type: int

retries:
    description:
    - The maximum number of times a query needs to be re-executed in case of detected
      failure during the execution of the query. If retries is not specified, the global
      variable mysql-query_retries_on_failure applies.
    type: int

rule_id:
    description:
    - The unique id of the rule. Rules are processed in rule_id order.
    type: int

timeout:
    description:
    - The maximum timeout in milliseconds with which the matched or rewritten query should
      be executed. If a query run for longer than the specific threshold, the query is
      automatically killed. If timeout is not specified, the global variable mysql-default_query_timeout
      applies.
    type: int

username:
    description:
    - Filtering criteria matching username.  If I(username) is non-NULL, a query will
      match only if the connection is made with the correct username.
    type: str

cache_ttl:
    description:
    - The number of milliseconds for which to cache the result of the query. Note in ProxySQL
      1.1 I(cache_ttl) was in seconds.
    type: int

error_msg:
    description:
    - Query will be blocked, and the specified error_msg will be returned to the client.
    type: str

multiplex:
    choices:
    - 0
    - 1
    - 2
    description:
    - If C(0), multiplex will be disabled.
    - If C(1), try to enable multiplex. There can be other conditions preventing this
      (for example, user variables or transactions).
    - If C(2), multiplexing is not disabled for just the current query.
    - By default, does not change multiplexing policies.
    type: int
    version_added: 1.1.0
    version_added_collection: community.proxysql

login_host:
    default: 127.0.0.1
    description:
    - The host used to connect to ProxySQL admin interface.
    type: str

login_port:
    default: 6032
    description:
    - The port used to connect to ProxySQL admin interface.
    type: int

login_user:
    description:
    - The username used to authenticate to ProxySQL admin interface.
    type: str

proxy_addr:
    description:
    - Match incoming traffic on a specific local IP.
    type: str

proxy_port:
    description:
    - Match incoming traffic on a specific local port.
    type: int

schemaname:
    description:
    - Filtering criteria matching schemaname. If I(schemaname) is non-NULL, a query will
      match only if the connection uses schemaname as its default schema.
    type: str

client_addr:
    description:
    - Match traffic from a specific source.
    type: str

config_file:
    default: ''
    description:
    - Specify a config file from which I(login_user) and I(login_password) are to be read.
    type: path

force_delete:
    default: false
    description:
    - By default we avoid deleting more than one schedule in a single batch, however if
      you need this behaviour and you are not concerned about the schedules deleted, you
      can set I(force_delete) to C(True).
    type: bool

match_digest:
    description:
    - Regular expression that matches the query digest. The dialect of regular expressions
      used is that of re2 - U(https://github.com/google/re2).
    type: str

re_modifiers:
    description:
    - Comma separated list of options to modify the behavior of the RE engine. With C(CASELESS)
      the match is case insensitive. With C(GLOBAL) the replace is global (replaces all
      matches and not just the first). For backward compatibility, only C(CASELESS) is
      the enabled by default.
    type: str
    version_added: 1.3.0
    version_added_collection: community.proxysql

save_to_disk:
    default: true
    description:
    - Save config to sqlite db on disk to persist the configuration.
    type: bool

match_pattern:
    description:
    - Regular expression that matches the query text. The dialect of regular expressions
      used is that of re2 - U(https://github.com/google/re2).
    type: str

login_password:
    description:
    - The password used to authenticate to ProxySQL admin interface.
    type: str

mirror_flagOUT:
    description:
    - Enables query mirroring. If set I(mirror_flagOUT) can be used to evaluates the mirrored
      query against the specified chain of rules.
    type: int

load_to_runtime:
    default: true
    description:
    - Dynamically load config to runtime memory.
    type: bool

replace_pattern:
    description:
    - This is the pattern with which to replace the matched pattern. Note that this is
      optional, and when omitted, the query processor will only cache, route, or set other
      parameters without rewriting.
    type: str

mirror_hostgroup:
    description:
    - Enables query mirroring. If set I(mirror_hostgroup) can be used to mirror queries
      to the same or different hostgroup.
    type: int

login_unix_socket:
    description:
    - The socket used to connect to ProxySQL admin interface.
    type: str

next_query_flagIN:
    description:
    - When is set, its value will become the I(flagIN) value for the next queries.
    type: int
    version_added: 1.3.0
    version_added_collection: community.proxysql

cache_empty_result:
    description:
    - Controls if resultset without rows will be cached or not.
    type: bool
    version_added: 1.1.0
    version_added_collection: community.proxysql

negate_match_pattern:
    description:
    - If I(negate_match_pattern) is set to C(True), only queries not matching the query
      text will be considered as a match. This acts as a NOT operator in front of the
      regular expression matching against match_pattern.
    type: bool

destination_hostgroup:
    description:
    - Route matched queries to this hostgroup. This happens unless there is a started
      transaction and the logged in user has I(transaction_persistent) set to C(True)
      (see M(community.proxysql.proxysql_mysql_users)).
    type: int

Outputs

stdout:
  description: The mysql user modified or removed from proxysql.
  returned: On create/update will return the newly modified rule, in all other cases
    will return a list of rules that match the supplied criteria.
  sample:
    changed: true
    msg: Added rule to mysql_query_rules
    rules:
    - OK_msg: null
      active: '0'
      apply: '0'
      cache_empty_result: null
      cache_ttl: null
      client_addr: null
      comment: null
      delay: null
      destination_hostgroup: 1
      digest: null
      error_msg: null
      flagIN: '0'
      flagOUT: null
      log: null
      match_digest: null
      match_pattern: null
      mirror_flagOUT: null
      mirror_hostgroup: null
      multiplex: null
      negate_match_pattern: '0'
      proxy_addr: null
      proxy_port: null
      reconnect: null
      replace_pattern: null
      retries: null
      rule_id: '1'
      schemaname: null
      timeout: null
      username: guest_ro
    state: present
  type: dict