--- - name: Configure timezone and NTP on Ubuntu & Rocky Linux hosts: all become: yes vars: timezone_region: "Asia/Kolkata" ntp_server: "0.in.pool.ntp.org" tasks: - name: Set timezone ansible.builtin.timezone: name: "{{ timezone_region }}" register: timezone_result - name: Debug timezone result ansible.builtin.debug: msg: "Timezone changed to {{ timezone_region }} successfully." when: timezone_result.changed # ------------------------------------------------------------- # Ubuntu: configure timesyncd # ------------------------------------------------------------- - name: Configure NTP on Ubuntu (systemd-timesyncd) block: - name: Enable and configure systemd-timesyncd ansible.builtin.copy: dest: /etc/systemd/timesyncd.conf content: | [Time] NTP={{ ntp_server }} FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org 3.in.pool.ntp.org register: ubuntu_ntp - name: Restart timesyncd ansible.builtin.systemd: name: systemd-timesyncd state: restarted enabled: yes - name: Debug ubuntu NTP ansible.builtin.debug: msg: "NTP configured on Ubuntu using {{ ntp_server }}" when: ubuntu_ntp.changed when: ansible_facts['os_family'] == "Debian" # ------------------------------------------------------------- # Rocky Linux: configure chrony # ------------------------------------------------------------- - name: Configure NTP on Rocky Linux (chrony) block: - name: Install chrony ansible.builtin.package: name: chrony state: present - name: Configure chrony NTP source ansible.builtin.replace: path: /etc/chrony.conf regexp: '^server .*$' replace: "server {{ ntp_server }} iburst" register: rocky_ntp - name: Restart chronyd ansible.builtin.systemd: name: chronyd state: restarted enabled: yes - name: Debug rocky NTP ansible.builtin.debug: msg: "NTP configured on Rocky Linux using {{ ntp_server }}" when: rocky_ntp.changed when: ansible_facts['os_family'] == "RedHat" # Final debug - name: Print final success message ansible.builtin.debug: msg: "Timezone and NTP configuration applied successfully on {{ inventory_hostname }}"