Skip to content

Commit

Permalink
update galera detection and galera primary handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bodsch committed Sep 6, 2024
1 parent 4e14221 commit 4eb460a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 21 deletions.
14 changes: 7 additions & 7 deletions filter_plugins/mariadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def detect_galera(self, data, hostvars):
cluster_members = [],
cluster_primary_node = "",
cluster_replica_nodes = [],
primary = False
# primary = False
)

# cluster_names = []
Expand Down Expand Up @@ -123,15 +123,15 @@ def detect_galera(self, data, hostvars):
display.vv(f"- cluster_members: '{cluster_members}' : {members_count}")

if members_count == 0:
primary = True
# primary = True
primary_address = bind_address
else:
primary_address = cluster_members[0]

if primary_address == bind_address:
primary = True
else:
primary = False
# if primary_address == bind_address:
# primary = True
# else:
# primary = False

if isinstance(hostvars, Mapping):
node_information = {x: v.get("ansible_default_ipv4", None).get("address", None) for x, v in hostvars.items() if v.get("ansible_default_ipv4", {}).get("address", None)}
Expand All @@ -145,7 +145,7 @@ def detect_galera(self, data, hostvars):
cluster_members = cluster_members,
cluster_primary_node = primary_node,
cluster_replica_nodes = replica_nodes,
primary = primary
# primary = primary
)

# display.vv(f"= {result}")
Expand Down
4 changes: 2 additions & 2 deletions handlers/galera.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- name: restart galera primary cluster node
when:
- mariadb_galera_primary
- mariadb_galera_primary_node == ansible_hostname
- _mysql_cnf_changed.changed | default('false')
block:
- name: restart galera primary cluster node
Expand All @@ -24,7 +24,7 @@
loop_control:
label: "{{ item }}"
when:
- not mariadb_galera_primary
- mariadb_galera_primary_node != ansible_hostname
- _mysql_cnf_changed.changed | default('false')
- inventory_hostname == item

Expand Down
2 changes: 1 addition & 1 deletion handlers/galera_secondary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- name: restart galera secondary cluster nodes
when:
- not mariadb_galera_primary
- mariadb_galera_primary_node != ansible_hostname
block:
- name: restart galera secondary cluster nodes ({{ item }})
ansible.builtin.service:
Expand Down
6 changes: 3 additions & 3 deletions tasks/configure/galera-cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
- name: setup primary instance
ansible.builtin.include_tasks: galera/primary.yml
when:
- mariadb_galera_primary
- mariadb_galera_primary_node == ansible_hostname
- not galera_bootstraped_database

- name: setup secondary instances
ansible.builtin.include_tasks: galera/secondary.yml
when:
- not mariadb_galera_primary
- not mariadb_galera_primary_node == ansible_hostname

- name: reconfigure primary instance
ansible.builtin.include_tasks: galera/primary_reconfigure.yml
when:
- mariadb_galera_primary
- mariadb_galera_primary_node == ansible_hostname

rescue:

Expand Down
2 changes: 1 addition & 1 deletion tasks/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- name: ensure mariadb databases are present.
when:
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary)
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary_node == ansible_hostname)
run_once: "{{ 'true' if mariadb_galera_cluster else 'false' }}"
community.mysql.mysql_db:
name: "{{ item.name }}"
Expand Down
2 changes: 1 addition & 1 deletion tasks/monitoring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- mariadb_monitoring.system_user | length != 0
- mariadb_monitoring.username is defined
- mariadb_monitoring.username | length != 0
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary)
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary_node == ansible_hostname)
block:
- name: create monitoring database
community.mysql.mysql_db:
Expand Down
14 changes: 11 additions & 3 deletions tasks/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
get_mime: false
register: mysqld_binary_file

- name: detect if mariadb installed
- name: define mariadb_installed if mariadb installed
ansible.builtin.set_fact:
mariadb_installed: "{{ mysqld_binary_file.stat.exists }}"
when:
Expand All @@ -81,17 +81,25 @@
- name: detect galera cluster
ansible.builtin.set_fact:
_mariadb_galera_cluster: "{{ mariadb_config_galera | detect_galera(hostvars) }}"
run_once: true
when:
- mariadb_config_galera is defined
- mariadb_config_galera | bodsch.core.type == 'dict'
- mariadb_galera.node_addresses is defined
- mariadb_galera.node_addresses | bodsch.core.type == 'list'
- mariadb_galera.node_addresses | count > 0

- name: define mariadb_galera and mariadb_galera_primary
- name: d
debug:
msg:
- "{{ _mariadb_galera_cluster }}"
- "{{ hostname | default('') }}"
- "{{ ansible_hostname | default('') }}"

- name: define mariadb_galera and mariadb_galera_cluster
ansible.builtin.set_fact:
mariadb_galera_cluster: "{{ _mariadb_galera_cluster.galera }}"
mariadb_galera_primary: "{{ _mariadb_galera_cluster.primary }}"
# mariadb_galera_primary: "{{ _mariadb_galera_cluster.primary }}"
mariadb_galera_primary_node: "{{ _mariadb_galera_cluster.cluster_primary_node }}"
mariadb_galera_replica_nodes: "{{ _mariadb_galera_cluster.cluster_replica_nodes }}"

Expand Down
2 changes: 1 addition & 1 deletion tasks/secure-installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

- name: secure database (remove anonymous user, disallow remote root login, ...)
when:
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary)
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary_node == ansible_hostname)
mariadb_secure:
disallow_anonymous_users: true
disallow_test_database: true
Expand Down
2 changes: 1 addition & 1 deletion tasks/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- name: create or remove mariadb users
when:
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary)
- not mariadb_galera_cluster or (mariadb_galera_cluster and mariadb_galera_primary_node == ansible_hostname)
run_once: "{{ 'true' if mariadb_galera_cluster else 'false' }}"
community.mysql.mysql_user:
name: "{{ item.name }}"
Expand Down
2 changes: 1 addition & 1 deletion vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ mariadb_defaults_galera:

_mariadb_galera_cluster:
galera: False
primary: ""
# primary: ""
cluster_primary_node: ""
cluster_replica_nodes: []

Expand Down

0 comments on commit 4eb460a

Please sign in to comment.