--- - name: Configure timezone and NTP hosts: all become: yes vars: # America/New_York correctly handles Eastern Standard/Daylight Time timezone_region: "America/New_York" ntp_server: "time.google.com" tasks: # ---------------------------------------------------------- # TIMEZONE (LXC + Ubuntu + Rocky safe) # ---------------------------------------------------------- - name: Copy timezone file ansible.builtin.copy: src: "/usr/share/zoneinfo/{{ timezone_region }}" dest: /etc/localtime remote_src: yes register: tz_copy - name: Write /etc/timezone ansible.builtin.copy: dest: /etc/timezone content: "{{ timezone_region }}\n" register: tz_write - name: Debug timezone set ansible.builtin.debug: msg: "Timezone set to {{ timezone_region }} successfully." when: tz_copy.changed or tz_write.changed # ---------------------------------------------------------- # NTP CONFIGURATION (OS Specific) # ---------------------------------------------------------- - name: Configure timesyncd (Ubuntu/Debian) ansible.builtin.lineinfile: path: /etc/systemd/timesyncd.conf regexp: '^#?NTP=' line: "NTP={{ ntp_server }}" state: present when: ansible_os_family == "Debian" notify: Restart timesyncd - name: Disable default chrony pools/servers (RHEL-based) ansible.builtin.replace: path: /etc/chrony.conf regexp: '^(pool|server) .*' replace: '# \1 removed by Ansible' when: ansible_os_family == "RedHat" - name: Configure chrony (RHEL-based) ansible.builtin.lineinfile: path: /etc/chrony.conf line: "server {{ ntp_server }} iburst" insertafter: EOF state: present when: ansible_os_family == "RedHat" notify: Restart chronyd # ---------------------------------------------------------- # VERIFICATION # ---------------------------------------------------------- - name: Flush handlers to restart NTP services immediately ansible.builtin.meta: flush_handlers - name: Verify current system time ansible.builtin.command: date register: check_time changed_when: false - name: Verify NTP configuration (Ubuntu/Debian) ansible.builtin.shell: grep -i '^NTP=' /etc/systemd/timesyncd.conf || echo "NTP not set in config" register: check_ntp_deb changed_when: false when: ansible_os_family == "Debian" - name: Verify NTP configuration (RHEL-based) ansible.builtin.command: chronyc sources register: check_ntp_rhel changed_when: false failed_when: false when: ansible_os_family == "RedHat" # ---------------------------------------------------------- # FINAL SUMMARY # ---------------------------------------------------------- - name: Final summary ansible.builtin.debug: msg: - "Configuration successfully applied on {{ inventory_hostname }}" - "System Time: {{ check_time.stdout }}" - "Debian NTP Config: {{ check_ntp_deb.stdout | default('Skipped (Not Debian)') }}" - "RHEL Active NTP Sources: {{ check_ntp_rhel.stdout_lines[2:] | default('Skipped (Not RHEL)') | join(' | ') }}" # ---------------------------------------------------------- # HANDLERS # ---------------------------------------------------------- handlers: - name: Restart timesyncd ansible.builtin.systemd: name: systemd-timesyncd state: restarted enabled: yes when: ansible_os_family == "Debian" - name: Restart chronyd ansible.builtin.systemd: name: chronyd state: restarted enabled: yes when: ansible_os_family == "RedHat"