---
- name: Configure systemd timesyncd
  become: true
  ansible.builtin.template:
    src: etc/systemd/timesyncd.conf.j2
    dest: /etc/systemd/timesyncd.conf
    backup: true
    mode: "0644"
    owner: root
    group: root
  notify:
    - Reload systemd
  tags:
    - systemd
    - timesyncd
    - CIS-UBUNTU2004-2.1.1.1
    - CIS-UBUNTU2004-2.1.1.2

- name: Stat timesyncd status
  ansible.builtin.command: systemctl status systemd-timesyncd
  register: timesyncd_status
  changed_when: false
  failed_when: false
  tags:
    - systemd
    - timesyncd
    - CIS-UBUNTU2004-2.1.1.1
    - CIS-UBUNTU2004-2.1.1.2

- name: Stat timedatectl show
  ansible.builtin.command: timedatectl show
  register: timedatectl_show
  changed_when: timedatectl_show.stdout.find('NTPSynchronized=yes') == -1 and timesyncd_status.rc == 0
  tags:
    - systemd
    - timesyncd
    - CIS-UBUNTU2004-2.1.1.1
    - CIS-UBUNTU2004-2.1.1.2

- name: Run timedatectl set-ntp
  become: true
  ansible.builtin.command: timedatectl set-ntp true # noqa no-changed-when
  when: timedatectl_show.stdout.find('NTPSynchronized=yes') == -1 and timesyncd_status.rc == 0
  tags:
    - systemd
    - timesyncd
    - CIS-UBUNTU2004-2.1.1.2
