Skip to content

Commit

Permalink
Add the ability to set Patroni tags (including custom tags) at the ho…
Browse files Browse the repository at this point in the history
…st level (vitabaks#611)
  • Loading branch information
vitabaks authored Mar 28, 2024
1 parent 0a09ef1 commit d0d9059
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
12 changes: 7 additions & 5 deletions inventory
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# "postgresql_exists='true'" if PostgreSQL is already exists and running
# "hostname=" variable is optional (used to change the server name)
# "new_node=true" to add a new server to an existing cluster using the add_pgnode.yml playbook
# patroni_tags="key=value" tags for Patroni in "key=value" format separated by commas (details here: https://patroni.readthedocs.io/en/latest/yaml_configuration.html#tags)

# In this example, all components will be installed on PostgreSQL nodes.
# You can deploy the haproxy balancers and the etcd or consul cluster on other dedicated servers (recomended).
Expand All @@ -21,7 +22,7 @@
10.128.64.140 consul_node_role=server consul_bootstrap_expect=true consul_datacenter=dc1
10.128.64.142 consul_node_role=server consul_bootstrap_expect=true consul_datacenter=dc1
10.128.64.143 consul_node_role=server consul_bootstrap_expect=true consul_datacenter=dc1
#10.128.64.144 consul_node_role=client consul_datacenter=dc1
#10.128.64.144 consul_node_role=client consul_datacenter=dc2
#10.128.64.145 consul_node_role=client consul_datacenter=dc2

# if with_haproxy_load_balancing: true
Expand All @@ -33,12 +34,13 @@

# PostgreSQL nodes
[master]
10.128.64.140 hostname=pgnode01 postgresql_exists=false
10.128.64.140 hostname=pgnode01 postgresql_exists=false # patroni_tags="datacenter=dc1"

[replica]
10.128.64.142 hostname=pgnode02 postgresql_exists=false
10.128.64.143 hostname=pgnode03 postgresql_exists=false
#10.128.64.144 hostname=pgnode04 postgresql_exists=false new_node=true
10.128.64.142 hostname=pgnode02 postgresql_exists=false # patroni_tags="datacenter=dc1"
10.128.64.143 hostname=pgnode03 postgresql_exists=false # patroni_tags="datacenter=dc1"
#10.128.64.144 hostname=pgnode04 postgresql_exists=false patroni_tags="datacenter=dc2" new_node=true
#10.128.64.145 hostname=pgnode04 postgresql_exists=false patroni_tags="datacenter=dc2" new_node=true

[postgres_cluster:children]
master
Expand Down
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
consul_bootstrap_expect: true # if dcs_type: "consul"
postgresql_version: "16" # to test custom WAL dir
pgbouncer_processes: 2 # Test multiple pgbouncer processes (so_reuseport)
patroni_tags: "datacenter=dc1,key1=value1"
cacheable: true
delegate_to: localhost
run_once: true # noqa run-once
Expand Down
16 changes: 14 additions & 2 deletions roles/patroni/templates/patroni.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,22 @@ watchdog:
safety_margin: 5

tags:
nofailover: false
{% if patroni_tags is defined and patroni_tags | length > 0 %}
{{ patroni_tags | replace(" ", "") | replace("=", ": ") | replace(",", "\n ") }}
{% endif %}
{% set normalized_tags = patroni_tags | default('') | replace(" ", "") %}
{% if 'nosync=' not in normalized_tags %}
nosync: false
{% endif %}
{% if 'noloadbalance=' not in normalized_tags %}
noloadbalance: false
{% endif %}
{% if 'nofailover=' not in normalized_tags %}
nofailover: false
{% endif %}
{% if 'clonefrom=' not in normalized_tags %}
clonefrom: false
nosync: false
{% endif %}

# specify a node to replicate from (cascading replication)
# replicatefrom: (node name)
Expand Down
6 changes: 3 additions & 3 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ patroni_etcd_password: "" # (optional) password for etcd authentication
patroni_etcd_protocol: "" # (optional) http or https, if not specified http is used

# more options you can specify in the roles/patroni/templates/patroni.yml.j2
# https://patroni.readthedocs.io/en/latest/SETTINGS.html#etcd
# https://patroni.readthedocs.io/en/latest/SETTINGS.html#consul
# https://patroni.readthedocs.io/en/latest/yaml_configuration.html#etcd
# https://patroni.readthedocs.io/en/latest/yaml_configuration.html#consul

# if dcs_type: "consul"
consul_version: "1.15.8"
Expand Down Expand Up @@ -343,7 +343,7 @@ patroni_master_start_timeout: 300
patroni_maximum_lag_on_failover: 1048576 # (1MB) the maximum bytes a follower may lag to be able to participate in leader election.
patroni_maximum_lag_on_replica: "100MB" # the maximum of lag that replica can be in order to be available for read-only queries.

# https://patroni.readthedocs.io/en/latest/SETTINGS.html?highlight=callbacks#dynamic-configuration-settings
# https://patroni.readthedocs.io/en/latest/yaml_configuration.html#postgresql
patroni_callbacks: []
# - {action: "on_role_change", script: ""}
# - {action: "on_stop", script: ""}
Expand Down

0 comments on commit d0d9059

Please sign in to comment.