Initial commit

This commit is contained in:
Sven Velt 2020-12-01 21:53:28 +01:00
commit 2c0b8b8c99
6 changed files with 355 additions and 0 deletions

155
.gitignore vendored Normal file
View file

@ -0,0 +1,155 @@
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# ---> Vim
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
# ---> Ansible
*.retry

42
README.md Normal file
View file

@ -0,0 +1,42 @@
Role Name
=========
Setup MariaDB replication
Requirements
------------
Two MariaDB-Servers
Role Variables
--------------
- db_server_id (host var/inventory): 1 is primary/master, other is replica/slave
- db_repl_user (defaults): Repliation user name
- db_repl_password (defaults): Replication password
- db_debug (undefined): Print some debug information
Dependencies
------------
- None
Example Playbook
----------------
- hosts: db-servers
roles:
- { role: mariadb-replication, db_debug: True }
License
-------
GPL-2.0-or-later
Author Information
------------------
- Sven Velt <sven-ansiblerole@velt.biz>
- https://git.velt.biz/velt.biz/

10
defaults/main.yml Normal file
View file

@ -0,0 +1,10 @@
---
db_packages:
- mariadb-server
- python3-mysqldb
db_servicename: mysql
db_repl_user: repl
db_repl_password: repls3cr3t

6
handlers/main.yml Normal file
View file

@ -0,0 +1,6 @@
---
- name: Restart MariaDB
service:
name: "{{ db_servicename }}"
state: restarted

21
meta/main.yml Normal file
View file

@ -0,0 +1,21 @@
galaxy_info:
author: Sven Velt
description: Setup MariaDB replication
company: velt.biz
issue_tracker_url: https://git.velt.biz/Ansible/mariadb-replication/issues/
license: GPL-2.0-or-later
min_ansible_version: 2.9
platforms:
- name: Ubuntu
versions:
- focal
galaxy_tags:
- database
- db
- mariadb

121
tasks/main.yml Normal file
View file

@ -0,0 +1,121 @@
---
- name: Install software
package:
name: "{{ item }}"
state: present
with_items: "{{ db_packages }}"
- name: "Quick-Fix MariaDB/systemd problems"
lineinfile:
path: /usr/lib/systemd/system/mariadb.service
regexp: "^SendSIGKILL=no"
line: "SendSIGKILL=yes"
backup: yes
register: db_systemd_fix
when: ansible_virtualization_type == "lxc"
- name: Reload systemd daemon
command: /bin/systemctl daemon-reload
when: db_systemd_fix is changed
- name: 'Enable service "{{ db_servicename }}"'
service:
name: "{{ db_servicename }}"
state: started
enabled: yes
- name: "Config: Listen on all ip addresses"
lineinfile:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: "^ *#* *bind-address"
line: "bind-address = 0.0.0.0"
backup: yes
notify: 'Restart MariaDB'
- name: "Config: Set server-id"
lineinfile:
dest: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: "^ *#* *server-id"
line: "server-id = {{ db_server_id }}"
backup: yes
notify: 'Restart MariaDB'
- name: "Config: Enable binlog"
lineinfile:
dest: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: "^ *#* *log_bin"
line: "log_bin = /var/log/mysql/mysql-bin.log"
backup: yes
when: db_server_id == 1
notify: 'Restart MariaDB'
- name: MariaDB create replication user
mysql_user:
name: "{{ db_repl_user }}"
password: "{{ db_repl_password }}"
priv: '*.*:REPLICATION SLAVE'
host: "%"
state: present
notify: 'Restart MariaDB'
- name: Get MariaDB master state
mysql_info:
filter: master_status
register: db_master_status
- debug:
msg: "{{ db_server_id }} - {{ db_master_status.master_status }}"
when: db_debug|default(False) == True
- name: "(Maybe) Restart MariaDB"
meta: flush_handlers
- name: Get MariaDB master state
mysql_info:
filter: master_status
register: db_master_status_new
- debug:
msg: "{{ db_server_id }} - {{ db_master_status_new.master_status }}"
when: db_debug|default(False) == True
- name: "(Master) Reset binlog"
command: '/usr/bin/mysql -u root -e "RESET MASTER"'
when: db_server_id == 1 and db_master_status.master_status == {}
- name: "(Slave) Get status"
mysql_replication:
mode: getslave
register: db_slave_status
- debug:
var: db_slave_status
when: db_debug|default(False) == True
- name: "(Slave) Setup replication"
command: '/usr/bin/mysql -u root -e "CHANGE MASTER TO master_host=\"{{ hostvars["db-01"]["ansible_default_ipv4"].address }}\", master_user=\"{{ db_repl_user }}\", master_password=\"{{ db_repl_password }}\", master_use_gtid=current_pos"'
when: db_server_id != 1 and db_slave_status.Slave_IO_Running|default("No") == "No"
- name: "(Slave) Start slave"
mysql_replication:
mode: startslave
when: db_server_id != 1 and db_slave_status.Slave_IO_Running|default("No") == "No"