community.proxysql.proxysql_pgsql_query_rules module – Modifies pgsql query rules using the proxysql admin interface

Note

This module is part of the community.proxysql collection (version 1.8.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.proxysql. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: community.proxysql.proxysql_pgsql_query_rules.

New in community.proxysql 1.8.0

Synopsis

Requirements

The below requirements are needed on the host that executes this module.

  • PyMySQL

  • mysqlclient

Parameters

Parameter

Comments

active

boolean

A rule with active set to False will be tracked in the database, but will be never loaded in the in-memory data structures. If omitted the proxysql database default for active is False.

Choices:

  • false

  • true

apply

boolean

Used in combination with flagIN and flagOUT to create chains of rules. Setting apply to True signifies the last rule to be applied. If omitted the proxysql database default for apply is False.

Choices:

  • false

  • true

attributes

string

A JSON field that can be used to specify load balancing between hostgroups based on query rules. If omitted the proxysql database default for attributes is ''.

cache_empty_result

boolean

Controls if resultset without rows will be cached or not.

Choices:

  • false

  • true

cache_timeout

integer

The maximum timeout in milliseconds for caching the result of the query.

cache_ttl

integer

The number of milliseconds for which to cache the result of the query.

client_addr

string

Match traffic from a specific source.

comment

string

Free form text field, usable for a descriptive comment of the query rule.

config_file

path

Specify a config file from which login_user and login_password are to be read.

Default: ""

database

string

Filtering criteria matching database. If database is non-NULL, a query will match only if the connection uses database as its default schema.

delay

integer

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.

destination_hostgroup

integer

Route matched queries to this hostgroup.

digest

string

Match queries with a specific digest, as returned by stats_pgsql_query_digest.digest.

error_msg

string

Query will be blocked, and the specified error_msg will be returned to the client.

flagIN

integer

Used in combination with flagOUT and apply to create chains of rules. If omitted the proxysql database default for flagIN is 0.

flagOUT

integer

Used in combination with flagIN and apply to create chains of rules. When set, flagOUT signifies the flagIN to be used in the next chain of rules.

force_delete

boolean

By default we avoid deleting more than one query rule in a single batch, however if you need this behaviour and you are not concerned about the query rules deleted, you can set force_delete to True.

Choices:

  • false ← (default)

  • true

load_to_runtime

boolean

Dynamically load config to runtime memory.

Choices:

  • false

  • true ← (default)

log

boolean

Query will be logged.

Choices:

  • false

  • true

login_host

string

The host used to connect to ProxySQL admin interface.

Default: "127.0.0.1"

login_password

string

The password used to authenticate to ProxySQL admin interface.

login_port

integer

The port used to connect to ProxySQL admin interface.

Default: 6032

login_unix_socket

string

The socket used to connect to ProxySQL admin interface.

login_user

string

The username used to authenticate to ProxySQL admin interface.

match_digest

string

Regular expression that matches the query digest. The dialect of regular expressions used is that of re2 - https://github.com/google/re2.

match_pattern

string

Regular expression that matches the query text. The dialect of regular expressions used is that of re2 - https://github.com/google/re2.

mirror_flagOUT

integer

Enables query mirroring. If set mirror_flagOUT can be used to evaluates the mirrored query against the specified chain of rules.

mirror_hostgroup

integer

Enables query mirroring. If set mirror_hostgroup can be used to mirror queries to the same or different hostgroup.

multiplex

integer

If 0, multiplex will be disabled.

If 1, try to enable multiplex. There can be other conditions preventing this.

If 2, multiplexing is not disabled for just the current query.

By default, does not change multiplexing policies.

Choices:

  • 0

  • 1

  • 2

negate_match_pattern

boolean

If negate_match_pattern is set to 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. If omitted the proxysql database default for negate_match_pattern is False.

Choices:

  • false

  • true

next_query_flagIN

integer

When is set, its value will become the flagIN value for the next queries.

OK_msg

string

The specified message will be returned for a query that uses the defined rule.

proxy_addr

string

Match incoming traffic on a specific local IP.

proxy_port

integer

Match incoming traffic on a specific local port.

re_modifiers

string

Comma separated list of options to modify the behavior of the RE engine. With CASELESS the match is case insensitive. With GLOBAL the replace is global (replaces all matches and not just the first). For backward compatibility, only CASELESS is enabled by default.

reconnect

boolean

Not yet implemented in ProxySQL.

Choices:

  • false

  • true

replace_pattern

string

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.

retries

integer

The maximum number of times a query needs to be re-executed in case of detected failure during the execution of the query.

rule_id

integer

The unique id of the rule. Rules are processed in rule_id order.

save_to_disk

boolean

Save config to sqlite db on disk to persist the configuration.

Choices:

  • false

  • true ← (default)

state

string

When present - adds the rule, when absent - removes the rule.

Choices:

  • "present" ← (default)

  • "absent"

sticky_conn

boolean

Not yet implemented in ProxySQL.

Choices:

  • false

  • true

timeout

integer

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.

username

string

Filtering criteria matching username. If username is non-NULL, a query will match only if the connection is made with the correct username.

Attributes

Attribute

Support

Description

check_mode

Support: full

Do not make any changes to memory, disk, or runtime.

Examples

---
# This example adds a rule to redirect queries from a specific user to another
# hostgroup, it saves the pgsql query rule config to disk, but avoids loading
# the pgsql query 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.proxysql.proxysql_manage_config module). It uses
# supplied credentials to connect to the proxysql admin interface.

- name: Add a rule
  community.proxysql.proxysql_pgsql_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

# This example removes all rules that use the username 'guest_ro', saves the
# pgsql query rule config to disk, and dynamically loads the pgsql 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_pgsql_query_rules:
    config_file: '~/proxysql.cnf'
    username: 'guest_ro'
    state: absent
    force_delete: true

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

stdout

dictionary

The pgsql query rule 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 pgsql_query_rules", "rules": [{"OK_msg": null, "active": "0", "apply": "0", "cache_empty_result": null, "cache_timeout": null, "cache_ttl": null, "client_addr": null, "comment": null, "database": 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", "timeout": null, "username": "guest_ro"}], "state": "present"}

Authors

  • Janos Ruszo (@jruszo)