2023-09-10 12:52:53 +02:00
|
|
|
---
|
|
|
|
- name: Write config
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: "mysql.cnf.j2"
|
|
|
|
dest: "/etc/mysql/mariadb.conf.d/50-server.cnf"
|
|
|
|
mode: "0644"
|
|
|
|
become: true
|
|
|
|
|
|
|
|
- name: restart mysql
|
|
|
|
ansible.builtin.service:
|
|
|
|
name: mysql
|
|
|
|
state: restarted
|
|
|
|
enabled: true
|
|
|
|
become: true
|
|
|
|
|
|
|
|
- name: Create replication user
|
|
|
|
community.mysql.mysql_user:
|
|
|
|
state: present
|
|
|
|
login_user: root
|
|
|
|
login_password: "{{ mysql_root_password }}"
|
|
|
|
name: "{{ mysql.replication_user }}"
|
|
|
|
password: "{{ mysql_replication_pass }}"
|
|
|
|
host: "%"
|
|
|
|
tls_requires:
|
|
|
|
priv:
|
|
|
|
"*.*": "REPLICATION SLAVE"
|
|
|
|
login_unix_socket: /var/run/mysqld/mysqld.sock
|
|
|
|
become: true
|
2023-09-14 22:59:08 +02:00
|
|
|
when: ansible_hostname == mysql.replication_master_hostname
|
2023-09-10 12:52:53 +02:00
|
|
|
|
|
|
|
- name: Get master replication status
|
|
|
|
community.mysql.mysql_replication:
|
|
|
|
login_user: root
|
|
|
|
login_password: "{{ mysql_root_password }}"
|
|
|
|
mode: getmaster
|
|
|
|
register: repl_stat
|
2023-09-14 22:59:08 +02:00
|
|
|
when: ansible_hostname == mysql.replication_master_hostname
|
2023-09-10 12:52:53 +02:00
|
|
|
|
|
|
|
- debug: var=repl_stat
|
|
|
|
|
|
|
|
- name: Check if slave is already configured as replica
|
|
|
|
community.mysql.mysql_replication:
|
|
|
|
login_user: root
|
|
|
|
login_password: "{{ mysql_root_password }}"
|
|
|
|
mode: getslave
|
|
|
|
ignore_errors: true
|
|
|
|
register: slave
|
|
|
|
when: ansible_hostname in mysql.replication_slaves
|
|
|
|
|
|
|
|
- name: Setup slaves
|
|
|
|
community.mysql.mysql_replication:
|
|
|
|
login_user: root
|
|
|
|
login_password: "{{ mysql_root_password }}"
|
|
|
|
mode: changemaster
|
2023-09-14 22:59:08 +02:00
|
|
|
master_host: "{{ mysql.replication_master_ip }}"
|
2023-09-10 12:52:53 +02:00
|
|
|
master_log_file: "{{ hostvars['web1'].repl_stat.File }}"
|
|
|
|
master_log_pos: "{{ hostvars['web1'].repl_stat.Position }}"
|
|
|
|
master_user: "{{ mysql.replication_user }}"
|
|
|
|
master_password: "{{ mysql_replication_pass }}"
|
2023-09-14 22:59:08 +02:00
|
|
|
when: ansible_hostname in mysql.replication_slaves
|
2023-09-10 12:52:53 +02:00
|
|
|
|
|
|
|
- name: Start slave replication
|
|
|
|
community.mysql.mysql_replication:
|
|
|
|
login_user: root
|
|
|
|
login_password: "{{ mysql_root_password }}"
|
|
|
|
mode: startslave
|
|
|
|
when: ansible_hostname in mysql.replication_slaves and not slave.Is_Slave
|