Update playbooks/time-region.yml

This commit is contained in:
2026-04-02 20:59:41 +00:00
parent 6b568299da
commit 416e6a3142
+77 -3
View File
@@ -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"