commit 7249950ef53b25c5d7448363980ebac1143d4b58
parent fde3a0eb45f700ab605658192b7161c1b40f9c4e
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 23 Feb 2021 13:41:58 +0100
parent fde3a0eb45f700ab605658192b7161c1b40f9c4e
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 23 Feb 2021 13:41:58 +0100
roles/mariadb: create role
6 files changed, 171 insertions(+), 0 deletions(-)
A
|
74
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/roles/mariadb/tasks/checks.yml b/roles/mariadb/tasks/checks.yml @@ -0,0 +1,6 @@ +--- + +- fail: msg="Databases have to be defined when Users should be created!" + when: + - services.mariadb.users is defined + - services.mariadb.databases is not defined
diff --git a/roles/mariadb/tasks/install.yml b/roles/mariadb/tasks/install.yml @@ -0,0 +1,24 @@ +--- + +- name: "[Alpine] Install package: mariadb mariadb-common mariadb-client python-pymysql" + apk: + name: mariadb mariadb-common mariadb-client python-pymysql + state: present + when: + - ansible_distribution == "Alpine" + +- name: "[Archlinux] Install package: mariadb python-pymysql" + pacman: + name: + - mariadb + - python-pymysql + state: present + when: + - ansible_distribution == "Archlinux" + +- name: "[Archlinux] Create database structure at: /var/lib/mysql" + command: + cmd: "mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql" + creates: /var/lib/mysql/mysql + when: + - ansible_distribution == "Archlinux"
diff --git a/roles/mariadb/tasks/main.yml b/roles/mariadb/tasks/main.yml @@ -0,0 +1,17 @@ +--- + +- include: install.yml + when: + - services.mariadb.enable is true + +- include: start.yml + when: + - services.mariadb.enable is true + +- include: state.yml + when: + - services.mariadb.enable is true + +- include: remove.yml + when: + - services.mariadb.enable is false
diff --git a/roles/mariadb/tasks/remove.yml b/roles/mariadb/tasks/remove.yml @@ -0,0 +1,33 @@ +--- + +- name: "[OpenRC] Disable and stop service: mariadb" + service: + name: mariadb + enabled: no + state: stopped + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Disable and stop service: mariadb" + systemd: + name: mariadb + enabled: no + state: stopped + when: + - ansible_service_mgr == "systemd" + +- name: "[Alpine] Remove package: mariadb mariadb-common mariadb-client python-pymysql" + apk: + name: mariadb mariadb-common mariadb-client python-pymysql + state: absent + when: + - ansible_distribution == "Alpine" + +- name: "[Archlinux] Remove package: mariadb python-pymysql" + pacman: + name: + - mariadb + - python-pymysql + state: absent + when: + - ansible_distribution == "Archlinux"
diff --git a/roles/mariadb/tasks/start.yml b/roles/mariadb/tasks/start.yml @@ -0,0 +1,17 @@ +--- + +- name: "[OpenRC] Enable and start service: mariadb" + service: + name: mariadb + enabled: yes + state: started + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Enable and start service: mariadb" + systemd: + name: mariadb + enabled: yes + state: started + when: + - ansible_service_mgr == "systemd"
diff --git a/roles/mariadb/tasks/state.yml b/roles/mariadb/tasks/state.yml @@ -0,0 +1,74 @@ +--- + +- name: "Create specified MySQL databases" + community.mysql.mysql_db: + login_unix_socket: /run/mysqld/mysqld.sock + state: "{{ item.state | default('present') }}" + name: "{{ item.name }}" + loop: "{{ query('list', services.mariadb.databases)[0] }}" + when: + - services.mariadb.databases is defined + +- name: "Create specified Mysql-Users" + community.mysql.mysql_user: + login_unix_socket: /run/mysqld/mysqld.sock + state: "{{ item.state | default('present') }}" + name: "{{ item.username }}" + password: "{{ item.password }}" + priv: "{{ item.privileges }}" + loop: "{{ query('list', services.mariadb.users)[0] }}" + when: + - services.mariadb.users is defined + +- community.mysql.mysql_info: + login_unix_socket: /run/mysqld/mysqld.sock + filter: + - databases + - users + return_empty_dbs: yes + register: mysql_info + when: + - services.mariadb.databases is defined + +- name: "Dump databases that are not meant to be deployed" + community.mysql.mysql_db: + login_unix_socket: /run/mysqld/mysqld.sock + state: dump + name: "{{ item }}" + target: "/root/{{ item }}.sql" + loop: "{{ mysql_info['databases'] | list }}" + when: + - services.mariadb.databases is defined + - "item != 'mysql'" + - "item != 'test'" + - "item != 'information_schema'" + - "item != 'performance_schema'" + - "item not in (services.mariadb.databases | map(attribute='name') | list)" + +- name: "Delete databases that are not meant to be deployed" + community.mysql.mysql_db: + login_unix_socket: /run/mysqld/mysqld.sock + state: absent + name: "{{ item }}" + loop: "{{ mysql_info['databases'] | list }}" + when: + - services.mariadb.databases is defined + - "item != 'mysql'" + - "item != 'test'" + - "item != 'information_schema'" + - "item != 'performance_schema'" + - "item not in (services.mariadb.databases | map(attribute='name') | list)" + +- name: "Delete users that are not meant to be deployed" + community.mysql.mysql_user: + login_unix_socket: /run/mysqld/mysqld.sock + state: absent + user: "{{ item }}" + loop: "{{ mysql_info['users']['localhost'] | list }}" + when: + - services.mariadb.databases is defined + - "item != 'mysql'" + - "item != 'mariadb.sys'" + - "item != ''" + - "item != 'root'" + - "item not in (services.mariadb.users | map(attribute='username') | list)"