From 96dd47d06ad6aa3b2f76199fc4116b3ef2e78285 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Sun, 17 Apr 2022 19:09:11 +0100
Subject: [PATCH] Various tweaks

---
 defaults/main.yml               |  2 +-
 tasks/mariadb_root_password.yml | 26 ++++++++++++++++++++++----
 tasks/mariadb_root_socket.yml   | 31 +++++++++++++++++++++++++------
 tasks/tz.yml                    |  2 +-
 4 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/defaults/main.yml b/defaults/main.yml
index 100cd49..acba72a 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -35,7 +35,7 @@ mariadb_query_cache_limit: 0
 mariadb_query_cache_size: 0
 
 # Import time zone tables
-mariadb_time_zone_import: false
+mariadb_time_zone_import: true
 
 # If the mariadb_root_auth variable is not set then nothing will be done to the
 # root account login, which, by default, uses a socket for logins and doesn't
diff --git a/tasks/mariadb_root_password.yml b/tasks/mariadb_root_password.yml
index 9e328da..6f965fe 100644
--- a/tasks/mariadb_root_password.yml
+++ b/tasks/mariadb_root_password.yml
@@ -29,10 +29,10 @@
       when: mariadb_community
       no_log: true
 
-    - name: Enable MariaDB root logins with a password
-      ansible.builtin.command: mysql -uroot -e "UPDATE mysql.user SET authentication_string=PASSWORD('{{ mariadb_root_password }}'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost'; FLUSH PRIVILEGES;"
-      when: not mariadb_community
-      no_log: true
+    # - name: Enable MariaDB root logins with a password
+    #   ansible.builtin.command: mysql -uroot -e "UPDATE mysql.user SET authentication_string=PASSWORD('{{ mariadb_root_password }}'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost'; FLUSH PRIVILEGES;"
+    #   when: not mariadb_community
+    #   no_log: true
 
     - name: MariaDB root password written to /root/.my.cnf
       ansible.builtin.template:
@@ -44,6 +44,24 @@
         force: true
       no_log: true
 
+    - name: Query the MariaDB databases, users and version using the /root/.my.cnf file
+      community.mysql.mysql_info:
+        filter:
+          - users
+        return_empty_dbs: true
+        login_user: root
+        config_file: /root/.my.cnf
+      register: mariadb_info
+
+    - name: Set a variable for the root authentication plugin
+      ansible.builtin.set_fact:
+        mariadb_root_auth_current: "{{ mariadb_info.users.localhost.root.plugin }}"
+
+    - name: Debug MariaDB root authentication plugin
+      ansible.builtin.debug:
+        var: mariadb_root_auth_current
+        verbosity: 2
+
   tags:
     - mariadb
 ...
diff --git a/tasks/mariadb_root_socket.yml b/tasks/mariadb_root_socket.yml
index 6f6ee4d..a5b52c0 100644
--- a/tasks/mariadb_root_socket.yml
+++ b/tasks/mariadb_root_socket.yml
@@ -16,18 +16,37 @@
         - ( mariadb_root_password is defined ) and ( mariadb_root_password | length > 0 )
       no_log: true
 
-    - name: Enable MariaDB root logins with a socket
-      ansible.builtin.command: mysql -uroot -p{{ mariadb_root_password }} -e "ALTER USER root@localhost IDENTIFIED VIA unix_socket; FLUSH PRIVILEGES;" mysql
-      when:
-        - not mariadb_community
-        - ( mariadb_root_password is defined ) and ( mariadb_root_password | length > 0 )
-      no_log: true
+    # - name: Enable MariaDB root logins with a socket
+    #   ansible.builtin.command: mysql -uroot -p{{ mariadb_root_password }} -e "ALTER USER root@localhost IDENTIFIED VIA unix_socket; FLUSH PRIVILEGES;" mysql
+    #   when:
+    #     - not mariadb_community
+    #     - ( mariadb_root_password is defined ) and ( mariadb_root_password | length > 0 )
+    #   no_log: true
 
     - name: Remove /root/.my.cnf
       ansible.builtin.file:
         path: /root/.my.cnf
         state: absent
 
+    - name: Query the MariaDB users using a socket
+      community.mysql.mysql_info:
+        filter:
+          - databases
+          - users
+          - version
+        return_empty_dbs: true
+        login_user: root
+        login_unix_socket: /run/mysqld/mysqld.sock
+      register: mariadb_info
+
+    - name: Set a variable for the root authentication plugin
+      ansible.builtin.set_fact:
+        mariadb_root_auth_current: "{{ mariadb_info.users.localhost.root.plugin }}"
+  
+    - name: Debug MariaDB root authentication plugin
+      ansible.builtin.debug:
+        var: mariadb_root_auth_current
+        verbosity: 2
   tags:
     - mariadb
 ...
diff --git a/tasks/tz.yml b/tasks/tz.yml
index 5e4a16a..de4848e 100644
--- a/tasks/tz.yml
+++ b/tasks/tz.yml
@@ -99,7 +99,7 @@
             state: absent
           when: mariadb_tzdata_mariadb_file.stat.exists
 
-       - name: "/root/.my.tzdata file icontaining {{ mariadb_tzdata_latest }} present"
+       - name: "/root/.my.tzdata file containing {{ mariadb_tzdata_latest }} present"
          lineinfile:
            path: /root/.my.tzdata
            line: "{{ mariadb_tzdata_latest }}"
-- 
GitLab