---
- name: Configure NTP with ntp as specified provider
  hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: 172.16.123.1
    timesync_ntp_provider: ntp
  tasks:
    - name: Common test setup tasks
      include_tasks: tasks/setup.yml
      vars:
        __timesync_ostree_unsupported: true

    - name: Install then remove ntp
      check_mode: false
      tags: tests::setup
      block:
        - name: Install ntp
          package:
            name: ntp
            state: present
          register: package_install
          ignore_errors: true  # noqa ignore-errors

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

        - name: Remove ntp
          package:
            name: ntp
            state: absent

    - name: Run test
      block:
        - name: Run the role
          include_role:
            name: linux-system-roles.timesync
            public: true

        - name: Verify ntpd service
          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

      always:
        - name: Cleanup after tests
          include_tasks: tasks/cleanup.yml
