diff --git a/playbooks/ntpdig-check.yml b/playbooks/ntpdig-check.yml new file mode 100644 index 0000000..4965b38 --- /dev/null +++ b/playbooks/ntpdig-check.yml @@ -0,0 +1,39 @@ +- 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 offset from ntpdig output + set_fact: + ntpdig_offset: "{{ ntpdig_output.stdout | regex_search('([+-]?\\d+\\.\\d+)', '\\1') | default('N/A') }}" + ntpdig_error: "{{ ntpdig_output.stdout | regex_search('\\+/-\\s+(\\d+\\.\\d+)', '\\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') }}