---
- name: Configure time synchronization with NTP servers
  hosts: all
  vars:
    timesync_chrony_custom_settings:
      - "logdir /var/log/chrony"
      - "log tracking"
    timesync_ntp_servers:
      - hostname: 172.16.123.1
      - hostname: 172.16.123.2
        iburst: true
        minpoll: 4
      - hostname: 172.16.123.3
        pool: true
        iburst: true
        minpoll: 4
        maxpoll: 6
    timesync_ntp_provider: chrony
    timesync_step_threshold: 0.01
    timesync_dhcp_ntp_servers: true
    timesync_min_sources: 2
  roles:
    - redhat.rhel_system_roles.timesync

  tasks:
    - name: Run chrony tests
      tags: tests::verify
      block:
        - name: Flush handlers
          meta: flush_handlers

        - name: Wait for services to start
          wait_for:
            timeout: 2

        - name: Get list of currently used time sources
          shell: chronyc -n sources || ntpq -pn
          register: sources
          changed_when: false

        - name: Check time sources
          assert:
            that:
              - "'172.16.123.1' in sources.stdout"
              - "'172.16.123.2' in sources.stdout"
              - "'172.16.123.3' in sources.stdout"

        - name: Get list of log files
          command: ls /var/log/chrony/
          register: logfiles
          changed_when: false

        - name: Check log generated files
          assert:
            that:
              - "'tracking.log' in logfiles.stdout"
