Add playbooks/check-time-sync.yml

This commit is contained in:
2025-07-11 00:36:11 +00:00
parent 71e3e00506
commit 31c3bea578

View File

@@ -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