Update playbooks/check-time-sync.yml
This commit is contained in:
@@ -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') }}
|
||||
|
||||
Reference in New Issue
Block a user