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: env:
- PLATFORM='docker-buster-default-master' ANSIBLE_VERSION='ansible>=2.9,<2.10' global:
- PLATFORM='docker-buster-upstream-master' ANSIBLE_VERSION='ansible>=2.9,<2.10' - VAGRANT_VERSION='2.2.18'
- PLATFORM='docker-buster-default-galera-1' ANSIBLE_VERSION='ansible>=2.9,<2.10' jobs:
- PLATFORM='docker-buster-upstream-galera-1' ANSIBLE_VERSION='ansible>=2.9,<2.10' - 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 os:
- linux
dist: trusty dist: focal
language: python language: python
python: 2.7 python:
- 3.8
services: services:
- docker - docker
before_install: before_install:
- wget https://releases.hashicorp.com/vagrant/2.1.1/vagrant_2.1.1_x86_64.deb - sudo apt-get -q update
- sudo dpkg -i vagrant_2.1.1_x86_64.deb - 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 - vagrant plugin install vagrant-hostmanager
install: 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: script:
- VAGRANT_DEFAULT_PROVIDER=docker vagrant up $PLATFORM - VAGRANT_DEFAULT_PROVIDER=docker vagrant up $PLATFORM
@ -30,6 +44,14 @@ script:
&& (echo 'Idempotence test: pass' && exit 0) && (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1) || (echo 'Idempotence test: fail' && exit 1)
- VAGRANT_DEFAULT_PROVIDER=docker vagrant status - 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: notifications:
webhooks: https://galaxy.ansible.com/api/v1/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 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. Install and configure MariaDB (Galera Cluster). Manage replication (master/slave). Create users and databases.
| OS | Origin | MariaDB versions | | OS | Origin | MariaDB versions |
| --------------- | --------- | ------------------------- | | --------------- | --------- | ------------------------- |
| Debian Buster | Debian | 10.3 | | 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 Notes
----- -----
@ -20,7 +23,8 @@ Notes
Requirements Requirements
------------ ------------
Ansible 2.9+ - Ansible >=2.11
- Collections: community.general / community.mysql / community.crypto / ansible.posix
Role Variables Role Variables
-------------- --------------

40
Vagrantfile vendored
View File

@ -8,6 +8,9 @@ Vagrant.configure("2") do |config|
vbox_deb_buster = 'debian/buster64' vbox_deb_buster = 'debian/buster64'
dk_deb_buster = 'hanxhx/vagrant-ansible:debian10' 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.enabled = true
config.hostmanager.manage_host = false config.hostmanager.manage_host = false
config.hostmanager.manage_guest = true config.hostmanager.manage_guest = true
@ -16,7 +19,7 @@ Vagrant.configure("2") do |config|
cases = [ cases = [
# ======================== # ========================
# Debian Buster # Debian Buster (10)
# ======================== # ========================
# Default replication # 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: 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', '2'] },
{ os_name: 'buster', vbox: vbox_deb_buster, docker: nil, vars: {mariadb_origin: 'upstream' }, groups: ['galera', '3'] }, { 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| cases.each_with_index do |opts,index|
@ -52,6 +78,11 @@ Vagrant.configure("2") do |config|
d.remains_running = true d.remains_running = true
d.has_ssh = true d.has_ssh = true
end 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| m.vm.provision "ansible" do |ansible|
ansible.playbook = "tests/test.yml" ansible.playbook = "tests/test.yml"
ansible.verbose = 'vv' ansible.verbose = 'vv'
@ -72,8 +103,13 @@ Vagrant.configure("2") do |config|
m.vm.network "private_network", ip: ip m.vm.network "private_network", ip: ip
m.vm.provider "virtualbox" do |v| m.vm.provider "virtualbox" do |v|
v.cpus = 1 v.cpus = 1
v.memory = 256 v.memory = 512
end 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| m.vm.provision "ansible" do |ansible|
ansible.playbook = "tests/test.yml" ansible.playbook = "tests/test.yml"
ansible.verbose = 'vv' ansible.verbose = 'vv'

View File

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

View File

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

View File

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

View File

@ -1,31 +1,31 @@
--- ---
- name: SET_FACT | Prepare mark var - name: SET_FACT | Prepare mark var
set_fact: ansible.builtin.set_fact:
__mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap" __mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap"
- name: STAT | Bootstrap mark - name: STAT | Bootstrap mark
stat: ansible.builtin.stat:
path: "{{ __mark }}" path: "{{ __mark }}"
register: s register: s
- block: - block:
- name: SET_FACT | We must NOT restart after bootstrap! - name: SET_FACT | We must NOT restart after bootstrap!
set_fact: ansible.builtin.set_fact:
mariadb_notify_restart: false mariadb_notify_restart: false
- name: SERVICE | Stop MariaDB - name: SERVICE | Stop MariaDB
service: ansible.builtin.service:
name: mysql name: "{{ mariadb_service_name }}"
state: stopped state: stopped
- name: COMMAND | Bootstrap first node (systemd) - name: COMMAND | Bootstrap first node (systemd)
command: galera_new_cluster ansible.builtin.command: galera_new_cluster
when: ansible_service_mgr == 'systemd' when: ansible_service_mgr == 'systemd'
- name: SERVICE | Bootstrap first node (clean init) - name: SERVICE | Bootstrap first node (clean init)
service: ansible.builtin.service:
name: mysql name: "{{ mariadb_service_name }}"
state: started state: "started"
arguments: --wsrep-new-cluster arguments: --wsrep-new-cluster
register: bootstrap_run register: bootstrap_run
when: ansible_service_mgr != 'systemd' when: ansible_service_mgr != 'systemd'
@ -33,6 +33,6 @@
when: not s.stat.exists or mariadb_galera_resetup when: not s.stat.exists or mariadb_galera_resetup
- name: COMMAND | Create Bootstrap mark - name: COMMAND | Create Bootstrap mark
command: "touch {{ __mark }}" ansible.builtin.command: "touch {{ __mark }}"
args: args:
creates: "{{ __mark }}" 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 - name: TEMPLATE | Deploy Galera configuration
template: ansible.builtin.template:
src: etc/mysql/mariadb.conf.d/20-galera.cnf.j2 src: etc/mysql/mariadb.conf.d/20-galera.cnf.j2
dest: /etc/mysql/mariadb.conf.d/20-galera.cnf dest: /etc/mysql/mariadb.conf.d/20-galera.cnf
mode: 0644
owner: root
group: root
register: galeraconfig register: galeraconfig
- name: INCLUDE | Bootstrap first node - name: INCLUDE | Bootstrap first node
import_tasks: 'bootstrap.yml' ansible.builtin.import_tasks: 'bootstrap.yml'
when: inventory_hostname == mariadb_galera_primary_node when: inventory_hostname == mariadb_galera_primary_node
- name: INCLUDE | Configure other nodes - name: INCLUDE | Configure other nodes
import_tasks: 'nodes.yml' ansible.builtin.import_tasks: 'nodes.yml'
when: inventory_hostname != mariadb_galera_primary_node when: inventory_hostname != mariadb_galera_primary_node
- name: SERVICE | Restart MariaDB if needed - name: SERVICE | Restart MariaDB if needed
service: ansible.builtin.service:
name: mysql name: "{{ mariadb_service_name }}"
state: restarted state: restarted
when: when:
((galeraconfig or (p is defined and p.changed)) and ((galeraconfig or (p is defined and p.changed)) and

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,11 @@
--- ---
- name: MYSQL_REPLICATION | Stop slave - name: MYSQL_REPLICATION | Stop slave
mysql_replication: community.mysql.mysql_replication:
mode: stopslave mode: stopslave
- name: MYSQL_REPLICATION | Configure master host - name: MYSQL_REPLICATION | Configure master host
mysql_replication: community.mysql.mysql_replication:
mode: changemaster mode: changemaster
master_host: "{{ mariadb_replication_host }}" master_host: "{{ mariadb_replication_host }}"
master_port: "{{ mariadb_replication_port }}" master_port: "{{ mariadb_replication_port }}"
@ -13,15 +13,10 @@
master_password: "{{ mariadb_replication_password }}" master_password: "{{ mariadb_replication_password }}"
no_log: "{{ not mariadb_debug_role }}" 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
# name: MYSQL_REPLICATION | Setup replication with GTID community.mysql.mysql_replication:
# mysql_replication: primary_use_gtid: current_pos
# 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 | Start slave - name: MYSQL_REPLICATION | Start slave
mysql_replication: community.mysql.mysql_replication:
mode: startslave mode: startslave

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,21 +5,26 @@
pre_tasks: pre_tasks:
- name: SETUP | Get facts - name: SETUP | Get facts
setup: ansible.builtin.setup:
register: s register: s
- name: DEBUG | Show facts - name: DEBUG | Show facts
debug: ansible.builtin.debug:
var: s var: s
tasks: tasks:
- name: APT | Install some packages - name: APT | Install some packages
apt: ansible.builtin.apt:
name: "{{ p }}" name: "{{ p }}"
update_cache: yes update_cache: true
cache_valid_time: 3600 cache_valid_time: 3600
vars: 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 - hosts: master
roles: roles:
@ -27,29 +32,37 @@
tasks: tasks:
- name: COPY | Deploy first dump - name: COPY | Deploy first dump
copy: ansible.builtin.copy:
src: import1.sql src: import1.sql
dest: /tmp/import1.sql dest: /tmp/import1.sql
mode: 0644
owner: root
group: root
register: c register: c
- name: MYSQL_DB | Import first dump - name: MYSQL_DB | Import first dump
mysql_db: community.mysql.mysql_db:
name: "{{ item }}" name: "{{ item }}"
state: import state: import
target: /tmp/import1.sql target: /tmp/import1.sql
login_unix_socket: "{{ mariadb_socket }}"
loop: ['testrepl', 'norepl'] loop: ['testrepl', 'norepl']
when: c.changed when: c.changed
tags:
- skip_ansible_lint
- hosts: slave - hosts: slave
pre_tasks: pre_tasks:
- name: SHELL | Get master IP - 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 register: ip
changed_when: false changed_when: false
- name: SET_FACT | Apply some configuration - name: SET_FACT | Apply some configuration
set_fact: ansible.builtin.set_fact:
# MariaDB don't read /etc/hosts (from vagrant host plugin) # MariaDB don't read /etc/hosts (from vagrant host plugin)
mariadb_replication_host: "{{ ip.stdout }}" mariadb_replication_host: "{{ ip.stdout }}"
# Need this to use vagrant 'delegate_to' # Need this to use vagrant 'delegate_to'
@ -62,40 +75,48 @@
- block: - block:
- name: COPY | Deploy dump - name: COPY | Deploy dump
copy: ansible.builtin.copy:
src: import2.sql src: import2.sql
dest: /tmp/import2.sql dest: /tmp/import2.sql
mode: 0644
owner: root
group: root
delegate_to: "{{ mariadb_slave_import_from }}" delegate_to: "{{ mariadb_slave_import_from }}"
register: c register: c
- name: MYSQL_DB | Import another dump - name: MYSQL_DB | Import another dump
mysql_db: community.mysql.mysql_db:
name: "{{ item }}" name: "{{ item }}"
state: import state: import
target: /tmp/import2.sql target: /tmp/import2.sql
login_unix_socket: "{{ mariadb_socket }}"
loop: ['testrepl', 'norepl'] loop: ['testrepl', 'norepl']
when: c.changed when: c.changed
delegate_to: "{{ mariadb_slave_import_from }}" delegate_to: "{{ mariadb_slave_import_from }}"
- name: MYSQL_REPLICATION | Get slave infos - name: MYSQL_REPLICATION | Get slave infos
mysql_replication: community.mysql.mysql_replication:
mode: getslave mode: getslave
register: slave register: slave
- name: FAIL | if slave threads are not running - name: FAIL | if slave threads are not running
fail: ansible.builtin.fail:
msg: "Slave issue" msg: "Slave issue"
when: slave.Slave_IO_Running != 'Yes' or slave.Slave_SQL_Running != 'Yes' when: slave.Slave_IO_Running != 'Yes' or slave.Slave_SQL_Running != 'Yes'
- hosts: galera - hosts: galera
pre_tasks: 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 - 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_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_wsrep_node_address: "{{ '127.0.0.1' if is_docker else ansible_eth1.ipv4.address }}"
mariadb_version: '10.3'
roles: roles:
- ../../ - ../../

View File

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

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