New release (#44)

* Manages MariaDB 10.6 + Debian 11
* Fix YAML lint
* Fix ansible lint
* Modernize TravisCI
* Fix service name depending OS version
* Add rsyslog on tests
* Remove useless variable in tests
* Use module mysql_replication instead of command for GTID setup
* Force 'mariadb' as service name if install from upstream
* Rename group vars file and valid YAML lint
* Galera install imrovements
* Do not force galera package name ('galera-3' was hardcoded)
* Prevent some crashes when galera is installed with mariadb at the same
time
* Install galera-3 on Buster + MariaDB from Debian repository
* Use FQDN Ansible modules
pull/45/merge
Emilien M 2021-09-02 18:17:54 +02:00 committed by GitHub
parent 860b212ee0
commit 2089f8eade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 317 additions and 178 deletions

2
.ansible-lint 100644
View File

@ -0,0 +1,2 @@
enable_list:
- fqcn-builtins

View File

@ -1,26 +1,40 @@
---
env:
- PLATFORM='docker-buster-default-master' ANSIBLE_VERSION='ansible>=2.9,<2.10'
- PLATFORM='docker-buster-upstream-master' ANSIBLE_VERSION='ansible>=2.9,<2.10'
- PLATFORM='docker-buster-default-galera-1' ANSIBLE_VERSION='ansible>=2.9,<2.10'
- PLATFORM='docker-buster-upstream-galera-1' ANSIBLE_VERSION='ansible>=2.9,<2.10'
global:
- VAGRANT_VERSION='2.2.18'
jobs:
- PLATFORM='docker-buster-default-master' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-buster-upstream-master' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-buster-default-galera-1' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-buster-upstream-galera-1' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-bullseye-default-master' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-bullseye-upstream-master' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-bullseye-default-galera-1' ANSIBLE_VERSION='>=2.11,<2.12'
- PLATFORM='docker-bullseye-upstream-galera-1' ANSIBLE_VERSION='>=2.11,<2.12'
sudo: required
dist: trusty
os:
- linux
dist: focal
language: python
python: 2.7
python:
- 3.8
services:
- docker
before_install:
- wget https://releases.hashicorp.com/vagrant/2.1.1/vagrant_2.1.1_x86_64.deb
- sudo dpkg -i vagrant_2.1.1_x86_64.deb
- sudo apt-get -q update
- sudo apt-get install -y yamllint
- sudo wget -nv https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}_x86_64.deb
- sudo dpkg -i vagrant_${VAGRANT_VERSION}_x86_64.deb
- vagrant plugin install vagrant-hostmanager
install:
- pip install "$ANSIBLE_VERSION"
- sudo pip install "ansible-core$ANSIBLE_VERSION"
- sudo pip install ansible-lint
- ansible-galaxy collection install community.general community.mysql community.crypto ansible.posix
script:
- VAGRANT_DEFAULT_PROVIDER=docker vagrant up $PLATFORM
@ -30,6 +44,14 @@ script:
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
- VAGRANT_DEFAULT_PROVIDER=docker vagrant status
- >
yamllint .
&& (echo 'YAML lint test: pass' && exit 0)
|| (echo 'YAML lint test: fail' && exit 1)
- >
ansible-lint -v tests/test.yml
&& (echo 'Ansible lint test: pass' && exit 0)
|| (echo 'Ansible lint test: fail' && exit 1)
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

6
.yamllint.yml 100644
View File

@ -0,0 +1,6 @@
---
extends: default
rules:
line-length: disable

View File

@ -1,14 +1,17 @@
MariaDB (MySQL) Ansible role for Debian
=======================================
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-HanXHX.mysql-blue.svg)](https://galaxy.ansible.com/HanXHX/mysql) [![Build Status](https://travis-ci.org/HanXHX/ansible-mysql.svg?branch=master)](https://travis-ci.org/HanXHX/ansible-mysql)
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-HanXHX.mysql-blue.svg)](https://galaxy.ansible.com/HanXHX/mysql) [![Build Status](https://app.travis-ci.com/HanXHX/ansible-mysql.svg?branch=master)](https://app.travis-ci.com/HanXHX/ansible-mysql)
Install and configure MariaDB (Galera Cluster). Manage replication (master/slave). Create users and databases.
| OS | Origin | MariaDB versions |
| --------------- | --------- | ------------------------- |
| Debian Buster | Debian | 10.3 |
| Debian Buster | Upstream | 10.3 / 10.4 |
| Debian Buster | Upstream | 10.3 to 10.6 |
| Debian Bullseye | Debian | 10.5 |
| Debian Bullseye | Upstream | 10.5 to 10.6 |
Notes
-----
@ -20,7 +23,8 @@ Notes
Requirements
------------
Ansible 2.9+
- Ansible >=2.11
- Collections: community.general / community.mysql / community.crypto / ansible.posix
Role Variables
--------------

40
Vagrantfile vendored
View File

@ -8,6 +8,9 @@ Vagrant.configure("2") do |config|
vbox_deb_buster = 'debian/buster64'
dk_deb_buster = 'hanxhx/vagrant-ansible:debian10'
vbox_deb_bullseye = 'debian/bullseye64'
dk_deb_bullseye = 'hanxhx/vagrant-ansible:debian11'
config.hostmanager.enabled = true
config.hostmanager.manage_host = false
config.hostmanager.manage_guest = true
@ -16,7 +19,7 @@ Vagrant.configure("2") do |config|
cases = [
# ========================
# Debian Buster
# Debian Buster (10)
# ========================
# Default replication
@ -36,6 +39,29 @@ Vagrant.configure("2") do |config|
{ os_name: 'buster', vbox: vbox_deb_buster, docker: dk_deb_buster, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '1'] },
{ os_name: 'buster', vbox: vbox_deb_buster, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '2'] },
{ os_name: 'buster', vbox: vbox_deb_buster, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '3'] },
# ========================
# Debian Bullseye (11)
# ========================
# Default replication
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: dk_deb_bullseye, vars: {mariadb_origin: 'default' }, groups: ['master'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'default' }, groups: ['slave'] },
# Upstream replication
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: dk_deb_bullseye, vars: {mariadb_origin: 'upstream' }, groups: ['master'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['slave'] },
# Galera Debian
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: dk_deb_bullseye, vars: {mariadb_origin: 'default' }, groups: ['galera', '1'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'default' }, groups: ['galera', '2'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'default' }, groups: ['galera', '3'] },
# Galera Upstream
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: dk_deb_bullseye, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '1'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '2'] },
{ os_name: 'bullseye', vbox: vbox_deb_bullseye, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '3'] },
]
cases.each_with_index do |opts,index|
@ -52,6 +78,11 @@ Vagrant.configure("2") do |config|
d.remains_running = true
d.has_ssh = true
end
if opts[:os_name].include? "bullseye"
m.vm.provision "shell", inline: "[ -f '/root/first_provision' ] || (apt-get update -qq && apt-get -y dist-upgrade && touch /root/first_provision)"
end
m.vm.provision "ansible" do |ansible|
ansible.playbook = "tests/test.yml"
ansible.verbose = 'vv'
@ -72,8 +103,13 @@ Vagrant.configure("2") do |config|
m.vm.network "private_network", ip: ip
m.vm.provider "virtualbox" do |v|
v.cpus = 1
v.memory = 256
v.memory = 512
end
if opts[:os_name].include? "bullseye"
m.vm.provision "shell", inline: "[ -f '/root/first_provision' ] || (apt-get update -qq && apt-get -y dist-upgrade && touch /root/first_provision)"
end
m.vm.provision "ansible" do |ansible|
ansible.playbook = "tests/test.yml"
ansible.verbose = 'vv'

View File

@ -65,7 +65,6 @@ mariadb_extra_configuration: {}
# mariadb_extra_configuration:
# 'innodb_awsome_feature': 1
# -------------------------------------
# Replication
# -------------------------------------
@ -81,6 +80,8 @@ mariadb_replication_port: '3306'
mariadb_backup_dir: '/var/backups/tmp-mysql'
mariadb_service_name: '{{ mariadb_default_service_name if mariadb_origin == "default" else "mariadb" }}'
# Master
mariadb_max_binlog_size: "100M"
mariadb_expire_logs_days: "10"
@ -116,7 +117,7 @@ mariadb_users: []
# -------------------------------------
# MariaDB
# -------------------------------------
mariadb_version: '10.3'
mariadb_version: '10.6'
mariadb_mirror_domain: "ftp.igh.cnrs.fr"
# See: https://downloads.mariadb.org/mariadb/repositories
mariadb_repository: "http://{{ mariadb_mirror_domain }}/pub/mariadb/repo/{{ mariadb_version }}/debian"
@ -132,6 +133,8 @@ mariadb_galera_members: []
mariadb_galera_primary_node: 'change_me' # See: https://github.com/ansible/ansible/issues/17453
mariadb_wsrep_stt_method: 'rsync'
mariadb_galera_package_name: 'galera-4'
# -------------------------------------
# Debug
# -------------------------------------

View File

@ -1,7 +1,7 @@
---
- name: restart mariadb
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: restarted
when: mariadb_notify_restart

View File

@ -1,5 +1,8 @@
---
galaxy_info:
namespace: hanxhx
role_name: mysql
author: Emilien Mantel
description: Install and configure MariaDB (and Galera Cluster) on Debian
company:
@ -9,6 +12,7 @@ galaxy_info:
- name: Debian
versions:
- buster
- bullseye
galaxy_tags:
- database
- packaging
@ -18,5 +22,5 @@ galaxy_info:
- debian
- galera
- cluster
dependencies: []
dependencies: []

View File

@ -1,31 +1,31 @@
---
- name: SET_FACT | Prepare mark var
set_fact:
ansible.builtin.set_fact:
__mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap"
- name: STAT | Bootstrap mark
stat:
ansible.builtin.stat:
path: "{{ __mark }}"
register: s
- block:
- name: SET_FACT | We must NOT restart after bootstrap!
set_fact:
ansible.builtin.set_fact:
mariadb_notify_restart: false
- name: SERVICE | Stop MariaDB
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: stopped
- name: COMMAND | Bootstrap first node (systemd)
command: galera_new_cluster
ansible.builtin.command: galera_new_cluster
when: ansible_service_mgr == 'systemd'
- name: SERVICE | Bootstrap first node (clean init)
service:
name: mysql
state: started
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: "started"
arguments: --wsrep-new-cluster
register: bootstrap_run
when: ansible_service_mgr != 'systemd'
@ -33,6 +33,6 @@
when: not s.stat.exists or mariadb_galera_resetup
- name: COMMAND | Create Bootstrap mark
command: "touch {{ __mark }}"
ansible.builtin.command: "touch {{ __mark }}"
args:
creates: "{{ __mark }}"

View File

@ -1,22 +1,29 @@
---
- name: APT | Install Galera
ansible.builtin.apt:
pkg: "{{ mariadb_galera_package_name }}"
- name: TEMPLATE | Deploy Galera configuration
template:
ansible.builtin.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'
ansible.builtin.import_tasks: 'bootstrap.yml'
when: inventory_hostname == mariadb_galera_primary_node
- name: INCLUDE | Configure other nodes
import_tasks: 'nodes.yml'
ansible.builtin.import_tasks: 'nodes.yml'
when: inventory_hostname != mariadb_galera_primary_node
- name: SERVICE | Restart MariaDB if needed
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: restarted
when:
((galeraconfig or (p is defined and p.changed)) and

View File

@ -1,25 +1,25 @@
---
- name: COMMAND | GET debian.cnf from primary node
command: cat /etc/mysql/debian.cnf
ansible.builtin.command: cat /etc/mysql/debian.cnf
register: debiancnf
delegate_to: '{{ mariadb_galera_primary_node }}'
changed_when: false
- name: COMMAND | Get current debian.cnf
command: cat /etc/mysql/debian.cnf
ansible.builtin.command: cat /etc/mysql/debian.cnf
register: ondc
changed_when: false
- block:
- name: SERVICE | Stop MariaDB
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: stopped
- name: COPY | Paste primary node's debian.cnf
copy:
ansible.builtin.copy:
content: "{{ debiancnf.stdout }}"
dest: /etc/mysql/debian.cnf
mode: 0600
@ -28,8 +28,8 @@
register: paste
- name: SERVICE | Start MariaDB
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: started
when: debiancnf.stdout != ondc.stdout

View File

@ -1,13 +1,13 @@
---
- name: INCLUDE | Setup MariaDB repository
import_tasks: 'mariadb/upstream.yml'
ansible.builtin.import_tasks: 'mariadb/upstream.yml'
when: mariadb_origin == 'upstream'
- name: INCLUDE | Install MariaDB
import_tasks: 'mariadb/default.yml'
ansible.builtin.import_tasks: 'mariadb/default.yml'
- name: APT | Install few MariaDB related tools
apt:
ansible.builtin.apt:
pkg: "{{ mariadb_tools }}"
install_recommends: no
install_recommends: false

View File

@ -1,10 +1,5 @@
---
- name: APT | Install MariaDB server
apt:
ansible.builtin.apt:
pkg: mariadb-server
- name: APT | Install Galera
apt:
pkg: galera-3
when: mariadb_use_galera

View File

@ -1,21 +1,28 @@
---
- name: APT | Install GPG
ansible.builtin.apt:
name: gpg
- name: APT_KEY | Install MariaDB key
apt_key:
ansible.builtin.apt_key:
keyserver: "{{ mariadb_key_server }}"
id: "{{ item }}"
loop: "{{ mariadb_key_ids }}"
- name: TEMPLATE | Deploy APT pinning (prevent upgrades from Debian)
template:
ansible.builtin.template:
src: etc/apt/preferences.d/95-mariadb.j2
dest: /etc/apt/preferences.d/95-mariadb
mode: 0644
owner: root
group: root
- name: APT_REPOSITORY | Add MariaDB repository
apt_repository:
ansible.builtin.apt_repository:
repo: 'deb {{ mariadb_repository }} {{ ansible_distribution_release }} main'
- name: APT_REPOSITORY | Add MariaDB (src) repository
apt_repository:
ansible.builtin.apt_repository:
repo: 'deb-src {{ mariadb_repository }} {{ ansible_distribution_release }} main'
when: mariadb_upstream_apt_src

View File

@ -1,21 +1,32 @@
---
- name: INCLUDE_VARS | Related to OS version
include_vars: "{{ ansible_distribution }}-{{ ansible_distribution_release }}.yml"
- name: INCLUDE_VARS | Extra variables
ansible.builtin.include_vars: "{{ lookup('first_found', params) }}"
vars:
params:
files:
- '{{ ansible_distribution }}-{{ ansible_distribution_release }}.yml'
- default.yml
paths:
- 'vars'
- name: STAT | Check if mysql exists
stat:
ansible.builtin.stat:
path: /usr/sbin/mysqld
register: mariadb_exists
changed_when: false
- name: INCLUDE | Install
import_tasks: install/main.yml
ansible.builtin.import_tasks: install/main.yml
- name: TEMPLATE | Deploy config files
template:
ansible.builtin.template:
src: "{{ item }}"
dest: "/{{ item | replace('.j2', '') }}"
mode: 0644
owner: root
group: root
register: config
loop:
- etc/mysql/my.cnf
- etc/mysql/conf.d/mysqldump.cnf.j2
@ -23,41 +34,40 @@
- etc/mysql/mariadb.conf.d/50-client.cnf.j2
- etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf.j2
- etc/mysql/mariadb.conf.d/50-server.cnf.j2
register: config
- name: SERVICE | Restart Mariadb now one at a time (prevent bugs)
throttle: 1
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: restarted
when:
config.changed and
not mariadb_galera_resetup
- name: SERVICE | Ensure service is started
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: started
- name: INCLUDE | Galera
import_tasks: galera/main.yml
ansible.builtin.import_tasks: galera/main.yml
when: mariadb_use_galera
- name: INCLUDE | Replication
import_tasks: replication/main.yml
ansible.builtin.import_tasks: replication/main.yml
when: mariadb_replication_master or mariadb_replication_slave
- name: INCLUDE | Secure install
import_tasks: 'secure.yml'
- name: SERVICE | Ensure service is started
service:
name: mysql
state: started
ansible.builtin.import_tasks: 'secure.yml'
- name: MYSQL_DB | Create databases
mysql_db:
community.mysql.mysql_db:
name: "{{ item }}"
state: present
loop: "{{ mariadb_databases }}"
- name: MYSQL_USER | Manages users...
mysql_user:
community.mysql.mysql_user:
name: "{{ item.name }}"
password: "{{ item.password }}"
priv: "{{ item.priv }}"

View File

@ -1,9 +1,9 @@
---
- name: INCLUDE | Replication Master
import_tasks: master.yml
ansible.builtin.import_tasks: master.yml
when: mariadb_replication_master
- name: INCLUDE | Replication slave
import_tasks: slave.yml
ansible.builtin.import_tasks: slave.yml
when: mariadb_replication_slave

View File

@ -1,7 +1,10 @@
---
- name: TEMPLATE | Deploy master configuration
template:
ansible.builtin.template:
src: etc/mysql/mariadb.conf.d/40-master.cnf.j2
dest: /etc/mysql/mariadb.conf.d/40-master.cnf
mode: 0644
owner: root
group: root
notify: restart mariadb

View File

@ -1,33 +1,36 @@
---
- name: MYSQL_VARIABLES | Set read only
mysql_variables:
community.mysql.mysql_variables:
variable: read_only
value: 'ON'
when: mariadb_slave_readonly
- name: TEMPLATE | Deploy slave configuration
template:
ansible.builtin.template:
src: etc/mysql/mariadb.conf.d/40-slave.cnf.j2
dest: /etc/mysql/mariadb.conf.d/40-slave.cnf
mode: 0644
owner: root
group: root
notify: restart mariadb
- name: MYSQL_REPLICATION | Get slave status
mysql_replication:
community.mysql.mysql_replication:
mode: getslave
ignore_errors: yes
ignore_errors: true
register: slave_status
- name: INCLUDE | Import data
import_tasks: slave/import_data.yml
ansible.builtin.import_tasks: slave/import_data.yml
when: (slave_status.failed or not slave_status.Is_Slave) and mariadb_slave_import_data
- name: Configure GTID Recplication
import_tasks: slave/gtid.yml
ansible.builtin.import_tasks: slave/gtid.yml
when: slave_status.failed or not slave_status.Is_Slave or mariadb_slave_force_setup
- name: MYSQL_REPLICATION | Get slave status
mysql_replication:
community.mysql.mysql_replication:
mode: getslave
ignore_errors: yes
ignore_errors: true
register: slave_status

View File

@ -1,11 +1,11 @@
---
- name: MYSQL_REPLICATION | Stop slave
mysql_replication:
community.mysql.mysql_replication:
mode: stopslave
- name: MYSQL_REPLICATION | Configure master host
mysql_replication:
community.mysql.mysql_replication:
mode: changemaster
master_host: "{{ mariadb_replication_host }}"
master_port: "{{ mariadb_replication_port }}"
@ -13,15 +13,10 @@
master_password: "{{ mariadb_replication_password }}"
no_log: "{{ not mariadb_debug_role }}"
# -- Wait this PR in Ansible released version (https://github.com/ansible/ansible/pull/62648)
# name: MYSQL_REPLICATION | Setup replication with GTID
# mysql_replication:
# master_use_gtid: current_pos
- name: COMMAND | Configure GTID
command: mariadb -e "CHANGE MASTER TO master_use_gtid=current_pos";
changed_when: true
- name: MYSQL_REPLICATION | Setup replication with GTID
community.mysql.mysql_replication:
primary_use_gtid: current_pos
- name: MYSQL_REPLICATION | Start slave
mysql_replication:
community.mysql.mysql_replication:
mode: startslave

View File

@ -1,7 +1,7 @@
---
- name: FILE | Create SSH client dir
file:
ansible.builtin.file:
path: "{{ ansible_env.HOME }}/.ssh"
state: directory
mode: 0700
@ -14,24 +14,27 @@
- block:
- name: AUTHORIZED_KEY | Auth slave to backup host
authorized_key:
ansible.posix.authorized_key:
user: "{{ mariadb_backup_user }}"
state: present
key: "{{ gen_ssh.public_key }}"
- name: FILE | Create backup directory
file:
ansible.builtin.file:
path: "{{ mariadb_backup_dir }}"
state: directory
mode: 0755
owner: root
group: root
- name: COMMAND | Prepare backup another server
command: "mariabackup --backup -u root --target-dir={{ mariadb_backup_dir }}"
ansible.builtin.command: "mariabackup --backup -u root --target-dir={{ mariadb_backup_dir }}"
args:
creates: "{{ mariadb_backup_dir }}/xtrabackup_info"
register: backup
- name: SHELL | Dump
shell: "mariabackup --prepare --target-dir={{ mariadb_backup_dir }}"
- name: COMMAND | Dump
ansible.builtin.command: "mariabackup --prepare --target-dir={{ mariadb_backup_dir }}"
when: backup.changed
register: prep
changed_when: "'This target seems to be not prepared yet' in prep.stderr"
@ -39,45 +42,47 @@
delegate_to: "{{ mariadb_slave_import_from }}"
- name: MYSQL_VARIABLES | Get datadir
mysql_variables:
community.mysql.mysql_variables:
variable: datadir
register: datadir
- name: SET_FACT | Get MariaDB datadir
set_fact:
ansible.builtin.set_fact:
mariadb_datadir: "{{ datadir.msg }}"
- name: SERVICE | Stop MariaDB before importing data
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: stopped
# TODO: add an "ignore warning"
- name: COMMAND | Sync backup to slave - TODO remove vagrant as static user (see why mariadb_backup_user is not working)
shell: "sudo -E rsync --rsync-path='sudo rsync' -a -e 'ssh -o StrictHostKeyChecking=no' {{ mariadb_backup_user }}@{{ mariadb_slave_import_from }}:{{ mariadb_backup_dir }}/ {{ mariadb_datadir }}/"
become: no
ansible.builtin.command: "sudo -E rsync --rsync-path='sudo rsync' -a -e 'ssh -o StrictHostKeyChecking=no' {{ mariadb_backup_user }}@{{ mariadb_slave_import_from }}:{{ mariadb_backup_dir }}/ {{ mariadb_datadir }}/"
become: false
tags:
- skip_ansible_lint
- name: FILE | Re-apply owner
file:
ansible.builtin.file:
path: "{{ mariadb_datadir }}"
state: directory
mode: 0755
owner: mysql
group: mysql
recurse: yes
recurse: true
- name: SHELL | Remove InnoDB redo logs
shell: "rm -f {{ mariadb_datadir }}/ib_logfile*"
ansible.builtin.shell: "rm -f {{ mariadb_datadir }}/ib_logfile*"
args:
removes: "{{ mariadb_datadir }}/ib_logfile0"
warn: false
- name: SERVICE | Start MariaDB
service:
name: mysql
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: started
- name: FILE | Delete dump
file:
ansible.builtin.file:
path: "{{ mariadb_backup_dir }}"
state: absent
delegate_to: "{{ mariadb_slave_import_from }}"

View File

@ -1,3 +1,5 @@
---
mariadb_bind_address: '0.0.0.0'
mariadb_extra_configuration:
innodb_commit_concurrency: 0

View File

@ -1,3 +1,5 @@
---
mariadb_use_galera: true
mariadb_galera_members:
- '{% if is_docker %}docker-{% else %}vbox-{% endif %}{{ ansible_distribution_release }}-{{ mariadb_origin }}-galera-1'

View File

@ -1,3 +1,5 @@
---
# Master durability
mariadb_sync_binlog: '1'
mariadb_innodb_flush_log_at_trx_commit: '1'

View File

@ -1,3 +1,5 @@
---
mariadb_replication_master: false
mariadb_replication_slave: true
mariadb_replication_user: 'replication'

View File

@ -5,21 +5,26 @@
pre_tasks:
- name: SETUP | Get facts
setup:
ansible.builtin.setup:
register: s
- name: DEBUG | Show facts
debug:
ansible.builtin.debug:
var: s
tasks:
- name: APT | Install some packages
apt:
ansible.builtin.apt:
name: "{{ p }}"
update_cache: yes
update_cache: true
cache_valid_time: 3600
vars:
p: ['ca-certificates', 'curl', 'strace', 'vim']
p: ['ca-certificates', 'curl', 'strace', 'rsyslog', 'vim']
- name: SERVICE | Ensure rsyslog is started
ansible.builtin.service:
name: rsyslog
state: started
- hosts: master
roles:
@ -27,29 +32,37 @@
tasks:
- name: COPY | Deploy first dump
copy:
ansible.builtin.copy:
src: import1.sql
dest: /tmp/import1.sql
mode: 0644
owner: root
group: root
register: c
- name: MYSQL_DB | Import first dump
mysql_db:
community.mysql.mysql_db:
name: "{{ item }}"
state: import
target: /tmp/import1.sql
login_unix_socket: "{{ mariadb_socket }}"
loop: ['testrepl', 'norepl']
when: c.changed
tags:
- skip_ansible_lint
- hosts: slave
pre_tasks:
- name: SHELL | Get master IP
shell: getent hosts {{ ansible_hostname | replace ('slave', 'master') }} | cut -d ' ' -f 1
ansible.builtin.shell: set -o pipefail && getent hosts {{ ansible_hostname | replace ('slave', 'master') }} | cut -d ' ' -f 1
args:
executable: /bin/bash
register: ip
changed_when: false
- name: SET_FACT | Apply some configuration
set_fact:
ansible.builtin.set_fact:
# MariaDB don't read /etc/hosts (from vagrant host plugin)
mariadb_replication_host: "{{ ip.stdout }}"
# Need this to use vagrant 'delegate_to'
@ -62,40 +75,48 @@
- block:
- name: COPY | Deploy dump
copy:
ansible.builtin.copy:
src: import2.sql
dest: /tmp/import2.sql
mode: 0644
owner: root
group: root
delegate_to: "{{ mariadb_slave_import_from }}"
register: c
- name: MYSQL_DB | Import another dump
mysql_db:
community.mysql.mysql_db:
name: "{{ item }}"
state: import
target: /tmp/import2.sql
login_unix_socket: "{{ mariadb_socket }}"
loop: ['testrepl', 'norepl']
when: c.changed
delegate_to: "{{ mariadb_slave_import_from }}"
- name: MYSQL_REPLICATION | Get slave infos
mysql_replication:
community.mysql.mysql_replication:
mode: getslave
register: slave
- name: FAIL | if slave threads are not running
fail:
ansible.builtin.fail:
msg: "Slave issue"
when: slave.Slave_IO_Running != 'Yes' or slave.Slave_SQL_Running != 'Yes'
- hosts: galera
pre_tasks:
- name: APT_REPOSITORY | Force galera-3 on Buster + MariaDB from Debian repository (prevent crashes)
ansible.builtin.set_fact:
mariadb_galera_package_name: 'galera-3'
when: ansible_distribution_release == 'buster' and mariadb_origin == 'default'
- name: SET_FACT | Apply some configuration
set_fact:
ansible.builtin.set_fact:
mariadb_galera_primary_node: '{% if is_docker %}docker-{% else %}vbox-{% endif %}{{ ansible_distribution_release }}-{{ mariadb_origin }}-galera-1'
mariadb_wsrep_node_address: "{{ '127.0.0.1' if is_docker else ansible_eth1.ipv4.address }}"
mariadb_version: '10.3'
roles:
- ../../

View File

@ -1,5 +1,3 @@
mariadb_tools:
- mariadb-backup
- mysqltuner
- percona-toolkit
- python{% if ansible_python_version is version('3', '>=') %}3{% endif %}-mysqldb
---
mariadb_default_service_name: 'mysql'

3
vars/default.yml 100644
View File

@ -0,0 +1,3 @@
---
mariadb_default_service_name: 'mariadb'

7
vars/main.yml 100644
View File

@ -0,0 +1,7 @@
---
mariadb_tools:
- mariadb-backup
- mysqltuner
- percona-toolkit
- python{% if ansible_python_version is version('3', '>=') %}3{% endif %}-mysqldb