---
- name: Configure NTP with ntp as current provider
  hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: 172.16.123.1
  roles:
    - linux-system-roles.timesync

  pre_tasks:
    - name: Common test setup tasks
      include_tasks: tasks/setup.yml
      vars:
        __timesync_ostree_unsupported: true

    - name: Remove then install chrony
      check_mode: false
      tags: tests::setup
      block:
        - name: Remove chrony
          package:
            name: chrony
            state: absent

        - name: Install ntp
          package:
            name: ntp
            state: present
          register: package_install
          ignore_errors: true  # noqa ignore-errors

        - name: Skip test if no package
          meta: end_play
          when: package_install.failed

        - name: Enable ntpd
          service:
            name: ntpd
            state: started
            enabled: true

  post_tasks:
    - name: Verify ntpd is running
      tags: tests::verify
      block:
        - name: Wait for services to start
          wait_for:
            timeout: 2

        - name: Check ntpd service
          shell: |
            set -eu
            if set -o | grep -q pipefail; then
              set -o pipefail  # no pipefail on debian, some ubuntu
            fi
            ntpq -c rv | grep 'associd=0'
          changed_when: false
