Update playbooks/time-region-ntp.yml

This commit is contained in:
2025-11-15 06:34:30 +00:00
parent 114d5632d3
commit a2068ae8cb
+2 -85
View File
@@ -29,93 +29,10 @@
msg: "Timezone set to {{ timezone_region }} successfully." msg: "Timezone set to {{ timezone_region }} successfully."
when: tz_copy.changed or tz_write.changed when: tz_copy.changed or tz_write.changed
# ----------------------------------------------------------
# UBUNTU NTP (systemd-timesyncd)
# ----------------------------------------------------------
- name: Configure NTP for Ubuntu (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
register: ubuntu_ntp
when: ansible_facts['os_family'] == "Debian"
- name: Restart timesyncd (Ubuntu)
ansible.builtin.systemd:
name: systemd-timesyncd
state: restarted
enabled: yes
when: ansible_facts['os_family'] == "Debian"
- name: Debug Ubuntu NTP
ansible.builtin.debug:
msg: "Ubuntu NTP configured with {{ ntp_server }}"
when:
- ansible_facts['os_family'] == "Debian"
- ubuntu_ntp.changed
# ----------------------------------------------------------
# ROCKY LINUX NTP (chrony)
# ----------------------------------------------------------
- name: Install chrony
ansible.builtin.package:
name: chrony
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Backup chrony.conf
ansible.builtin.copy:
src: /etc/chrony.conf
dest: /etc/chrony.conf.bak
remote_src: yes
when: ansible_facts['os_family'] == "RedHat"
- name: Remove existing server lines
ansible.builtin.replace:
path: /etc/chrony.conf
regexp: '^server .*'
replace: ''
register: rocky_cleanup
when: ansible_facts['os_family'] == "RedHat"
- name: Add new NTP server
ansible.builtin.lineinfile:
path: /etc/chrony.conf
line: "server {{ ntp_server }} iburst"
register: rocky_add
when: ansible_facts['os_family'] == "RedHat"
- name: Restart chronyd
ansible.builtin.systemd:
name: chronyd
state: restarted
enabled: yes
when: ansible_facts['os_family'] == "RedHat"
- name: Debug Rocky NTP
ansible.builtin.debug:
msg: "Rocky Linux NTP configured with {{ ntp_server }}"
when:
- ansible_facts['os_family'] == "RedHat"
- rocky_add.changed or rocky_cleanup.changed
# ----------------------------------------------------------
# PRINT TIME & NTP SOURCE (ZERO FAILURE SAFE)
# ----------------------------------------------------------
- name: Read timedatectl status
ansible.builtin.command: timedatectl
register: timedate_output
- name: Print time + time source for each server
ansible.builtin.debug:
msg: |
=== {{ inventory_hostname }} ===
{{ timedate_output.stdout }}
# ---------------------------------------------------------- # ----------------------------------------------------------
# FINAL SUMMARY # FINAL SUMMARY
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Final summary - name: Final summary
ansible.builtin.debug: ansible.builtin.debug:
msg: "Timezone + NTP successfully applied on {{ inventory_hostname }}" msg: "Timezone successfully applied on {{ inventory_hostname }}"