From 806aa3835b243924b3b0858e4e9cec7c6859e9ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Xavi=20Lle=C3=B3=20Tom=C3=A1s?= <xavi@verdnatura.es>
Date: Thu, 6 Feb 2025 14:17:38 +0100
Subject: [PATCH] refs #8414 - Refactor rol

---
 roles/db/defaults/main.yaml |  9 ++--
 roles/db/tasks/main.yml     |  1 +
 roles/db/tasks/mariadb.yml  | 99 +++++++++++++++++++------------------
 3 files changed, 55 insertions(+), 54 deletions(-)

diff --git a/roles/db/defaults/main.yaml b/roles/db/defaults/main.yaml
index 0144754..d648087 100644
--- a/roles/db/defaults/main.yaml
+++ b/roles/db/defaults/main.yaml
@@ -9,15 +9,16 @@ certificates:
   - { content: '{{ ca_company_deprecated }}', dest: '/etc/mysql/ca.pem', mode: 'u=rw,g=r,o=r' }
   - { content: '{{ cert_mysql }}', dest: '/etc/mysql/cert.pem', mode: 'u=rw,g=r,o=r' }
   - { content: '{{ cert_mysql_key }}', dest: '/etc/mysql/key.pem', mode: 'u=rw,g=,o=' }
-required_directories:
+required_directories_master:
   - { path: /mnt/local-backup, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }
   - { path: /mnt/mysqlbin, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }
+  - { path: /mnt/mysqlbin/binlog, owner: mysql, group: mysql, mode: 'u=rwx,g=,o=' }    
+required_directories:
   - { path: /mnt/mysqltmp, owner: root, group: root, mode: 'u=rwx,g=rwx,o=rwxt' }
-  - { path: /mnt/mysqlbin/binlog, owner: mysql, group: mysql, mode: 'u=rwx,g=,o=' }
   - { path: /root/scripts, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }
   - { path: /root/mariabackup, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }
   - { path: /mnt/mysqldata/mysql, owner: mysql, group: mysql, mode: 'u=rwx,g=rx,o=rx' }
-  - { path: /etc/systemd/system/mariadb.service.d, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }  
+  - { path: /etc/systemd/system/mariadb.service.d, owner: root, group: root, mode: 'u=rwx,g=rx,o=rx' }
 required_files_and_mariabackup_files_and_root_scripts:
   - { src: "mariadb_override.conf", dest: "/etc/systemd/system/mariadb.service.d/override.conf", mode: "u=rw,g=r,o=r" }
   - { src: "mysql-flush.sh", dest: "/etc/qemu/fsfreeze-hook.d/mysql-flush.sh", mode: "u=rwx,g=rx,o=rx" }  
@@ -33,11 +34,9 @@ required_files_and_mariabackup_files_and_root_scripts:
   - { src: "files/scripts/README.md", dest: "/root/scripts/README.md", mode: "u=rw,g=r,o=r" }
   - { src: "files/scripts/scheduler-log.sh", dest: "/root/scripts/scheduler-log.sh", mode: "u=rwx,g=rx,o=rx" }
   - { src: "files/marianrpe/95-mariadb.cfg", dest: "/etc/nagios/nrpe.d/95-mariadb.cfg", mode: "u=rw,g=r,o=r" }
-
 mariabackup_test_dev_files:
   - { src: "mariabackuptest/apply.sql", dest: "/root/mariabackup/", mode: "u=rw,g=r,o=r" }
   - { src: "mariabackuptest/bacula-after.sh", dest: "/root/mariabackup/", mode: "u=rwx,g=rx,o=rx" }
-
 downloads:
   - { url: "https://r.mariadb.com/downloads/mariadb_repo_setup", dest: "/tmp/mariadb_repo_setup", mode: "u=rwx,g=rx,o=rx" }
   - { url: "https://repo.percona.com/apt/percona-release_latest.generic_all.deb", dest: "/tmp/percona-release_latest.generic_all.deb", mode: "u=rw,g=r,o=r" }
diff --git a/roles/db/tasks/main.yml b/roles/db/tasks/main.yml
index 9bd454d..dbdb57d 100644
--- a/roles/db/tasks/main.yml
+++ b/roles/db/tasks/main.yml
@@ -1 +1,2 @@
 - import_tasks: mariadb.yml
+  tags: service
diff --git a/roles/db/tasks/mariadb.yml b/roles/db/tasks/mariadb.yml
index f7cce5d..5db0200 100644
--- a/roles/db/tasks/mariadb.yml
+++ b/roles/db/tasks/mariadb.yml
@@ -25,31 +25,16 @@
     state: present
     install_recommends: no
 
+- name: Update apt cache
+  apt:
+   update_cache: yes
+
 - name: Install MariaDB packages
   apt:
     name: "{{ mariadb_base_packages }}"
     state: present
     install_recommends: no
 
-- name: Ensure required directories exist
-  file:
-    path: "{{ item.path }}"
-    state: directory
-    owner: "{{ item.owner }}"
-    group: "{{ item.group }}"
-    mode: "{{ item.mode }}"
-  loop: "{{ required_directories }}"
-
-- name: Ensure required custom and Mariabackup files are copied to their destinations and root scripts
-  copy:
-    src: "{{ item.src }}"
-    dest: "{{ item.dest }}"
-    owner: root
-    group: root
-    mode: "{{ item.mode }}"
-  loop: "{{ required_files_and_mariabackup_files_and_root_scripts }}"
-  notify: reload-systemd
-
 - name: Add tmpfs in /etc/fstab
   blockinfile:
     path: /etc/fstab
@@ -58,10 +43,6 @@
       tmpfs /mnt/mysqltmp         tmpfs rw,size={{ mysqltmpsize }}         0 0
   register: fstab
 
-- name: Mount all filesystems from /etc/fstab
-  command: mount -a
-  when: fstab.changed
-
 - name: Set MariaDB Cron to /etc/cron.d
   template:
     src: templates/cron_mariadb
@@ -81,8 +62,27 @@
   loop: "{{ certificates }}"
   notify: restart-mariadb
 
+- name: Ensure required directories exist
+  file:
+    path: "{{ item.path }}"
+    state: directory
+    owner: "{{ item.owner }}"
+    group: "{{ item.group }}"
+    mode: "{{ item.mode }}"
+  loop: "{{ required_directories }}"
+
 - when: db.branch == 'master'
   block:
+
+  - name: Ensure required directories exist in Production
+    file:
+      path: "{{ item.path }}"
+      state: directory
+      owner: "{{ item.owner }}"
+      group: "{{ item.group }}"
+      mode: "{{ item.mode }}"
+    loop: "{{ required_directories_master }}"
+  
   - name: Set MariaDB custom configuration Production
     copy:
       src: "{{ item }}"
@@ -107,18 +107,6 @@
 - when: db.branch == 'test'
 
   block:
-  - name: Set MariaDB custom configuration TestDB
-    copy:
-      src: "{{ item }}"
-      dest: /etc/mysql/mariadb.conf.d/
-      owner: root
-      group: root
-      mode: u=rw,g=r,o=r
-    with_items:
-      - "files/mariaconfigfiles/z90-vn.cnf"
-      - "files/mariaconfigfiles/z92-testing_dev.cnf"
-    notify: restart-mariadb
-
   - name: Set MariaBackup custom template configuration TestDB
     template:
       src: test-db_dev/apply.config.sh
@@ -137,18 +125,6 @@
 - when: db.branch == 'dev'
 
   block:
-  - name: Set MariaDB custom configuration DevDB
-    copy:
-      src: "{{ item }}"
-      dest: /etc/mysql/mariadb.conf.d/
-      owner: root
-      group: root
-      mode: u=rw,g=r,o=r
-    with_items:
-      - "files/mariaconfigfiles/z90-vn.cnf"
-      - "files/mariaconfigfiles/z92-testing_dev.cnf"
-    notify: restart-mariadb
-
   - name: Set MariaBackup custom template configuration DevDB
     template:
       src: test-db_dev/apply.config.sh
@@ -166,7 +142,19 @@
 
 - when: db.branch == 'dev' or db.branch == 'test'
 
-  block:  
+  block:
+  - name: Set MariaDB custom configuration TestDB or DevDB
+    copy:
+      src: "{{ item }}"
+      dest: /etc/mysql/mariadb.conf.d/
+      owner: root
+      group: root
+      mode: u=rw,g=r,o=r
+    with_items:
+      - "files/mariaconfigfiles/z90-vn.cnf"
+      - "files/mariaconfigfiles/z92-testing_dev.cnf"
+    notify: restart-mariadb
+
   - name: Set Mariabackup files for TestDB or DevDB
     copy:
       src: "{{ item.src }}"
@@ -197,7 +185,6 @@
     path: /var/lib/mysql/
   register: mysql_dir
 
-
 - when: mysql_dir.stat.exists
   block:
 
@@ -214,3 +201,17 @@
     file:
       path: /var/lib/mysql/
       state: absent
+
+- name: Ensure required custom and Mariabackup files are copied to their destinations and root scripts
+  copy:
+    src: "{{ item.src }}"
+    dest: "{{ item.dest }}"
+    owner: root
+    group: root
+    mode: "{{ item.mode }}"
+  loop: "{{ required_files_and_mariabackup_files_and_root_scripts }}"
+  notify: reload-systemd
+
+- name: Mount all filesystems from /etc/fstab
+  command: mount -a
+  when: fstab.changed
\ No newline at end of file