From 7f4b91cffa8a6e7dcbbfbdeef22ab6f188c49d94 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Thu, 24 Nov 2022 13:25:23 +0000
Subject: [PATCH] Add TZ variable

---
 README.md               |  4 ++++
 defaults/main.yml       |  1 +
 meta/argument_specs.yml |  4 ++++
 tasks/timesyncd.yml     | 12 ++++++++++++
 4 files changed, 21 insertions(+)

diff --git a/README.md b/README.md
index 6b60b8c..0007d13 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,10 @@ Set the `systemd` variable to `false` to prevent any tasks in this role being ru
 
 When the `systemd_timesyncd_reboot` variable is set to `true` servers which have incorrect clocks will be rebooted by this role in order to correct their clocks, it defaults to `false`.
 
+### systemd_tz
+
+The time zone to be used (the hardware clock is set to UTC), for example `Europe/London`, it defaults to `Etc/UTC`.
+
 ### systemd_units
 
 A list of systemd units to configure, for example:
diff --git a/defaults/main.yml b/defaults/main.yml
index 6775b54..b4cfe28 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -1,6 +1,7 @@
 ---
 systemd: true
 systemd_timesyncd_reboot: false
+systemd_tz: Etc/UTC
 systemd_units:
   - name: systemd-timesyncd
     files:
diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml
index fd69c50..5f42aed 100644
--- a/meta/argument_specs.yml
+++ b/meta/argument_specs.yml
@@ -70,4 +70,8 @@ argument_specs:
         type: bool
         required: false
         description: Reboot if the clock is not synced with time servers.
+      systemd_tz:
+        type: str
+        required: true
+        description: The timezone for the clock to be set to.
 ...
diff --git a/tasks/timesyncd.yml b/tasks/timesyncd.yml
index f34a433..fdce628 100644
--- a/tasks/timesyncd.yml
+++ b/tasks/timesyncd.yml
@@ -42,12 +42,24 @@
     - name: Set a variable for the timedatectl status dictionary
       ansible.builtin.set_fact:
         systemd_timedatectl_status: "{{ systemd_timedatectl_status_command.stdout | community.general.jc('timedatectl') }}"
+        systemd_timedatectl_tz: "{{ (systemd_timedatectl_status_command.stdout | community.general.jc('timedatectl') | community.general.json_query('time_zone') | ansible.builtin.split(' '))[0] }}"
 
     - name: Debug the timedatectl status
       ansible.builtin.debug:
         var: systemd_timedatectl_status
         verbosity: 2
 
+    - name: Debug the timedatectl tz
+      ansible.builtin.debug:
+        var: systemd_timedatectl_tz
+        verbosity: 2
+
+    - name: Set the timezone
+      community.general.timezone:
+        name: "{{ systemd_tz }}"
+        hwclock: UTC
+      when: systemd_timedatectl_tz != systemd_tz
+
     - name: Set the time if not synchronized
       block:
 
-- 
GitLab