- name: Check time synchronization using ntpdig (modern method) hosts: all become: true gather_facts: false vars: ntp_check_target: "pool.ntp.org" tasks: - name: Remove legacy ntpdate package (if present) apt: name: ntpdate state: absent - name: Ensure ntpsec-ntpdate is installed (provides ntpdig) apt: name: ntpsec-ntpdate state: present update_cache: true - name: Query time offset using ntpdig from {{ ntp_check_target }} command: "ntpdig {{ ntp_check_target }}" register: ntpdig_output changed_when: false failed_when: ntpdig_output.rc != 0 - name: Parse correct offset and error from ntpdig output set_fact: ntpdig_offset: "{{ ntpdig_output.stdout | regex_search('[+-][0-9]+\\.[0-9]+(?=\\s+\\+/-)', '\\0') | default('N/A') }}" ntpdig_error: "{{ ntpdig_output.stdout | regex_search('\\+/-\\s+([0-9]+\\.[0-9]+)', '\\1') | default('N/A') }}" - name: Show parsed ntpdig result debug: msg: | [{{ inventory_hostname }}] Offset: {{ ntpdig_offset }} sec Estimated error: ±{{ ntpdig_error }} sec Raw output: {{ ntpdig_output.stdout_lines | join('\n') }}