Update playbooks/check-time-sync.yml

This commit is contained in:
2025-07-11 00:40:45 +00:00
parent 31c3bea578
commit 98e07f59dd

View File

@@ -1,51 +1,25 @@
- name: Check NTP synchronization (chrony-aware) - name: Quick check of time offset using ntpdate
hosts: all hosts: all
become: true become: true
gather_facts: false gather_facts: false
vars:
ntp_check_target: "pool.ntp.org"
tasks: tasks:
- name: Check if chronyc is installed - name: Ensure ntpdate is installed
command: which chronyc apt:
register: chronyc_installed name: ntpdate
ignore_errors: true state: present
update_cache: true
- name: Run chronyc tracking - name: Query time offset from {{ ntp_check_target }}
command: chronyc tracking command: "ntpdate -q {{ ntp_check_target }}"
register: chronyc_tracking register: ntp_offset
when: chronyc_installed.rc == 0
changed_when: false changed_when: false
failed_when: ntp_offset.rc != 0
- name: Extract sync metrics from chronyc tracking - name: Extract and display final offset line
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: debug:
msg: | msg: |
[{{ inventory_hostname }}] [{{ inventory_hostname }}] -> {{ ntp_offset.stdout_lines | select('search', 'adjust time') | list | first | default('No offset reported') }}
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