---

- name: TEMPLATE | Deploy Galera configuration
  template:
    src: etc/mysql/mariadb.conf.d/20-galera.cnf.j2
    dest: /etc/mysql/mariadb.conf.d/20-galera.cnf
    mode: 0644
    owner: root
    group: root
  register: galeraconfig

- name: INCLUDE | Bootstrap first node
  import_tasks: 'bootstrap.yml'
  when: inventory_hostname == mariadb_galera_primary_node

- name: INCLUDE | Configure other nodes
  import_tasks: 'nodes.yml'
  when: inventory_hostname != mariadb_galera_primary_node

- name: SERVICE | Restart MariaDB if needed
  service:
    name: mariadb
    state: restarted
  when:
    ((galeraconfig or (p is defined and p.changed)) and
    (bootstrap_run is not defined)) or
    ((inventory_hostname != mariadb_galera_primary_node) and
    (mariadb_galera_resetup))