---
# NOTE(priteau): We need to run handlers to restart chronyd before forcing
# clock synchronisation, or the system clock could appear as not synchronised
# during CI jobs because the tests run too quickly after the chronyd restart.
- meta: flush_handlers

- name: Force time synchronisation
  become: True
  block:
    - name: Configure burst
      command: chronyc 'burst 4/4'

    - name: Step the clock
      command: chronyc makestep

    - name: Wait before hardware clock synchronisation
      command: "chronyc waitsync {{ chrony_waitsync_max_tries }} {{ chrony_waitsync_max_correction }}"

    - name: Ensure hwclock is installed
      package:
        name: "{{ 'util-linux' if ansible_facts.os_family == 'RedHat' else 'util-linux-extra' }}"
        state: present

    - name: Force hardware clock synchronisation
      command: hwclock --systohc
  when: ntp_force_sync | bool
