---
-
  name: "Setting the ntp_driftfile variable."
  ansible.builtin.set_fact:
    ntp_driftfile: "{{ __ntp_driftfile }}"
  when: ntp_driftfile is not defined

-
  name: "Setting the ntp_package variable."
  ansible.builtin.set_fact:
    ntp_package: "{{ __ntp_package }}"
  when: ntp_package is not defined

-
  name: "Setting the ntp_config_file variable."
  ansible.builtin.set_fact:
    ntp_config_file: "{{ __ntp_config_file }}"
  when: ntp_config_file is not defined

-
  name: "Setting the ntp_daemon variable."
  ansible.builtin.set_fact:
    ntp_daemon: "{{ __ntp_daemon }}"
  when: ntp_daemon is not defined

-
  name: "Ensuring NTP package is installed (traditional NTP)."
  ansible.builtin.package:
    name: "{{ ntp_package }}"
    state: present
  when: not (__use_systemd_timesyncd | bool)

-
  name: "Ensuring systemd package is present (for systemd-timesyncd)."
  ansible.builtin.package:
    name: systemd
    state: present
  when: __use_systemd_timesyncd | bool

-
  name: "Ensuring tzdata package is installed (Linux)."
  ansible.builtin.package:
    name: "{{ ntp_tzdata_package }}"
    state: present
  when: ansible_system == "Linux"

-
  name: "Setting timezone."
  community.general.timezone:
    name: "{{ ntp_timezone }}"
  notify: restart cron

-
  name: "Populating service facts."
  ansible.builtin.service_facts:

-
  name: "Disabling systemd-timesyncd if using traditional NTP."
  ansible.builtin.service:
    name: systemd-timesyncd.service
    enabled: false
    state: stopped
  when:
    - not (__use_systemd_timesyncd | bool)
    - ntp_enabled | bool
    - ansible_facts.services["systemd-timesyncd"] is defined

-
  name: "Ensuring NTP is running and enabled as configured."
  ansible.builtin.service:
    name: "{{ ntp_daemon }}"
    state: started
    enabled: true
  when: ntp_enabled | bool

-
  name: "Ensuring NTP is stopped and disabled as configured."
  ansible.builtin.service:
    name: "{{ ntp_daemon }}"
    state: stopped
    enabled: false
  when: not (ntp_enabled | bool)

-
  name: "Generating systemd-timesyncd configuration file."
  ansible.builtin.template:
    src: "timesyncd.conf.j2"
    dest: "{{ ntp_config_file }}"
    mode: "0644"
  notify: restart ntp
  when: 
    - ntp_manage_config | bool
    - __use_systemd_timesyncd | bool

-
  name: "Generating traditional NTP configuration file."
  ansible.builtin.template:
    src: "{{ ntp_config_file | basename }}.j2"
    dest: "{{ ntp_config_file }}"
    mode: "0644"
  notify: restart ntp
  when: 
    - ntp_manage_config | bool
    - not (__use_systemd_timesyncd | bool)
