From 98e07f59dd95d2d8c13dae6dd2e6ebdceecb1715 Mon Sep 17 00:00:00 2001 From: Ryan Hamilton Date: Fri, 11 Jul 2025 00:40:45 +0000 Subject: [PATCH] Update playbooks/check-time-sync.yml --- playbooks/check-time-sync.yml | 54 +++++++++-------------------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/playbooks/check-time-sync.yml b/playbooks/check-time-sync.yml index 79b8cb5..e69c0a8 100644 --- a/playbooks/check-time-sync.yml +++ b/playbooks/check-time-sync.yml @@ -1,51 +1,25 @@ -- name: Check NTP synchronization (chrony-aware) +- name: Quick check of time offset using ntpdate hosts: all become: true gather_facts: false + vars: + ntp_check_target: "pool.ntp.org" tasks: - - name: Check if chronyc is installed - command: which chronyc - register: chronyc_installed - ignore_errors: true + - name: Ensure ntpdate is installed + apt: + name: ntpdate + state: present + update_cache: true - - name: Run chronyc tracking - command: chronyc tracking - register: chronyc_tracking - when: chronyc_installed.rc == 0 + - name: Query time offset from {{ ntp_check_target }} + command: "ntpdate -q {{ ntp_check_target }}" + register: ntp_offset changed_when: false + failed_when: ntp_offset.rc != 0 - - name: Extract sync metrics from chronyc tracking - set_fact: - chrony_summary: - system_time: "{{ chronyc_tracking.stdout | regex_search('System time\\s+: (.+)', '\\1') | default('N/A') }}" - last_offset: "{{ chronyc_tracking.stdout | regex_search('Last offset\\s+: (.+)', '\\1') | default('N/A') }}" - frequency: "{{ chronyc_tracking.stdout | regex_search('Frequency\\s+: (.+)', '\\1') | default('N/A') }}" - stratum: "{{ chronyc_tracking.stdout | regex_search('Stratum\\s+: (.+)', '\\1') | default('N/A') }}" - when: chronyc_installed.rc == 0 - - - name: Show chrony sync summary + - name: Extract and display final offset line debug: msg: | - [{{ inventory_hostname }}] - System Time : {{ chrony_summary.system_time }} - Last Offset : {{ chrony_summary.last_offset }} - Frequency : {{ chrony_summary.frequency }} - Stratum : {{ chrony_summary.stratum }} - when: chronyc_installed.rc == 0 - - - name: Fallback - timedatectl if chrony is not installed - command: timedatectl status - register: timedatectl_status - when: chronyc_installed.rc != 0 - changed_when: false - - - name: Show fallback timedatectl status - debug: - msg: | - [{{ inventory_hostname }}] - Chrony not installed. - timedatectl says: - {{ timedatectl_status.stdout_lines | join('\n') }} - when: chronyc_installed.rc != 0 + [{{ inventory_hostname }}] -> {{ ntp_offset.stdout_lines | select('search', 'adjust time') | list | first | default('No offset reported') }}