From d0bf95ff09d2bc6e3e9e2a8ffc7157e32ec2a2eb Mon Sep 17 00:00:00 2001 From: Jan-Niklas Meier Date: Sat, 11 Jul 2020 10:54:28 +0200 Subject: [PATCH] Adding option to configure RTC changes --- README.md | 12 +++++++----- defaults/main.yml | 2 ++ handlers/main.yml | 5 ----- tasks/main.yml | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index de946d7..ce3e0f3 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,13 @@ systemd with timesyncd compiled in ## Role Variables -| Name | Default/Required | Description | -|-------------------------------|:---------------------:|-----------------------------------------------------| -| `timesync_timezone` | `Etc/UTC` | Timezone to set (relative to `/usr/share/zoneinfo`) | -| `timesync_ntp_hosts` | | Array of NTP hosts | -| `timesync_fallback_ntp_hosts` | `{0..3}.pool.ntp.org` | Array of fallback NTP hosts | +| Name | Default/Required | Description | +|-------------------------------|:---------------------:|----------------------------------------------------------------------------------| +| `timesync_timezone` | `Etc/UTC` | Timezone to set (relative to `/usr/share/zoneinfo`) | +| `timesync_ntp_hosts` | | Array of NTP hosts | +| `timesync_fallback_ntp_hosts` | `{0..3}.pool.ntp.org` | Array of fallback NTP hosts | +| `timesync_rtc_set` | `auto` | Determines if the RTC will be updated. `auto` will only update if the RTC exists | +| `timesync_rtc_dev` | `/dev/rtc` | The RTC device to be used | ## Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index 90ee685..ec62db5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -5,3 +5,5 @@ timesync_fallback_ntp_hosts: - 1.pool.ntp.org - 2.pool.ntp.org - 3.pool.ntp.org +timesync_rtc_set: auto +timesync_rtc_dev: /dev/rtc \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml index 0048c6d..0436b8a 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -4,8 +4,3 @@ name: systemd-timesyncd.service state: restarted listen: systemd-timesyncd configuration changed - -- name: Write adjtime - command: hwclock --systohc - when: ansible_virtualization_role == "host" or ansible_virtualization_role == "NA" - listen: systemd-timesyncd configuration changed diff --git a/tasks/main.yml b/tasks/main.yml index 76e7ffa..b375d91 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -18,3 +18,30 @@ name: systemd-timesyncd.service enabled: yes state: started + +- name: Flush handlers for restart prior to RTC update + meta: flush_handlers + +- name: Check if RTC exists + stat: + path: "{{timesync_rtc_dev}}" + register: rtc_dev + when: timesync_rtc_set == "auto" + +- name: "Evaluate RTC auto update" + set_fact: + timesync_rtc_set: true + when: timesync_rtc_set == "auto" and rtc_dev.stat.exists and rtc.dev.stat.writeable and (ansible_virtualization_role == "host" or ansible_virtualization_role == "NA") + +- name: Wait for time to synchronize + shell: "timedatectl | grep 'NTP synchronized' | cut -d' ' -f3" + register: synchronized + retries: 5 + delay: 5 + until: synchronized.stdout == "yes" + when: timesync_rtc_set == true + +- name: Write adjtime + command: "hwclock --systohc --rtc={{timesync_rtc_dev}}" + when: timesync_rtc_set == true +