---
# ---
# NTP Server
# ---

- name: (ntp.yml) Ensure ntpsec package is installed.
  ansible.builtin.apt:
    name:
      - ntpsec
    state: present
  when:
    - ansible_facts.os_family == "Debian"
  tags:
    - ntp-server

- name: (ntp.yml) Check file '/etc/ntpsec/ntp.conf.ORIG' exists
  ansible.builtin.stat:
    path: /etc/ntpsec/ntp.conf.ORIG
  register: etc_ntpsec_conf_ORIG
  when:
    - ansible_facts.distribution == "Debian"
  tags:
    - ntp-server

- name: (ntp.yml) Ensure directory '/var/log/ntpsec' is present
  ansible.builtin.file:
    path: /var/log/ntpsec
    state: directory
    owner: ntpsec
    group: ntpsec
    mode: "0755"
  when:
    - ansible_facts.distribution == "Debian"

- name: (ntp.yml) Backup installation version of file '/etc/ntpsec/ntp.conf'
  ansible.builtin.command: cp /etc/ntpsec/ntp.conf /etc/ntpsec/ntp.conf.ORIG
  when:
    - groups['oopen_office_server']|string is search(inventory_hostname)
    - etc_ntpsec_conf_ORIG.stat.exists == False
    - local_ntp_service is defined and local_ntp_service|bool
  tags:
    - ntp-server

- name: (ntp.yml) Update '/etc/ntpsec/ntp.conf'
  ansible.builtin.template:
    src: "etc/ntpsec/ntp.conf.j2"
    dest: /etc/ntpsec/ntp.conf
    owner: root
    group: root
    mode: "0644"
  notify: Restart ntp
  when:
    - groups['oopen_office_server']|string is search(inventory_hostname)
    - local_ntp_service is defined and local_ntp_service|bool
  tags:
    - ntp-server
