commit 2c0b8b8c99d6f3d964ea320b1036b66477f183a7 Author: Sven Velt Date: Tue Dec 1 21:53:28 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75540da --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/README.md b/README.md new file mode 100644 index 0000000..13a3c69 --- /dev/null +++ b/README.md @@ -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 +- https://git.velt.biz/velt.biz/ + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..3c52018 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,10 @@ +--- +db_packages: + - mariadb-server + - python3-mysqldb + +db_servicename: mysql + +db_repl_user: repl +db_repl_password: repls3cr3t + diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..fd1c0e7 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: Restart MariaDB + service: + name: "{{ db_servicename }}" + state: restarted + diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..6e9d201 --- /dev/null +++ b/meta/main.yml @@ -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 + diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..be898cc --- /dev/null +++ b/tasks/main.yml @@ -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" + +