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

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

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

        - name: Remove chrony
          package:
            name: chrony
            state: absent

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

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

            - name: Check chronyd service
              command: chronyc -n tracking
              changed_when: false

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