From 416e6a314234fec6abcf6a2e9adeacf2cd5ce214 Mon Sep 17 00:00:00 2001 From: harry Date: Thu, 2 Apr 2026 20:59:41 +0000 Subject: [PATCH] Update playbooks/time-region.yml --- playbooks/time-region.yml | 82 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/playbooks/time-region.yml b/playbooks/time-region.yml index e953d96..dcb12c1 100644 --- a/playbooks/time-region.yml +++ b/playbooks/time-region.yml @@ -4,8 +4,9 @@ become: yes vars: - timezone_region: "Asia/Kolkata" - ntp_server: "0.in.pool.ntp.org" + # America/New_York correctly handles Eastern Standard/Daylight Time + timezone_region: "America/New_York" + ntp_server: "time.google.com" tasks: # ---------------------------------------------------------- @@ -29,10 +30,83 @@ 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: "Timezone successfully applied on {{ inventory_hostname }}" + 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" \ No newline at end of file