diff --git a/playbooks/check-time-sync.yml b/playbooks/check-time-sync.yml new file mode 100644 index 0000000..79b8cb5 --- /dev/null +++ b/playbooks/check-time-sync.yml @@ -0,0 +1,51 @@ +- name: Check NTP synchronization (chrony-aware) + hosts: all + become: true + gather_facts: false + + tasks: + + - name: Check if chronyc is installed + command: which chronyc + register: chronyc_installed + ignore_errors: true + + - name: Run chronyc tracking + command: chronyc tracking + register: chronyc_tracking + when: chronyc_installed.rc == 0 + changed_when: false + + - 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 + 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