Update playbooks/time-region-ntp.yml

This commit is contained in:
2025-11-15 05:07:59 +00:00
parent d8f2c77f01
commit d6d422d470
+91 -71
View File
@@ -9,96 +9,116 @@
tasks: tasks:
# ---------------------------------------------------------- # ----------------------------------------------------------
# TIMEZONE (Works on Ubuntu LXC + Rocky) # TIMEZONE (LXC + Ubuntu + Rocky safe)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Set timezone (manual method for compatibility) - name: Copy timezone file
block: ansible.builtin.copy:
- name: Copy timezone file src: "/usr/share/zoneinfo/{{ timezone_region }}"
ansible.builtin.copy: dest: /etc/localtime
src: "/usr/share/zoneinfo/{{ timezone_region }}" remote_src: yes
dest: /etc/localtime register: tz_copy
remote_src: yes
- name: Write /etc/timezone - name: Write /etc/timezone
ansible.builtin.copy: ansible.builtin.copy:
dest: /etc/timezone dest: /etc/timezone
content: "{{ timezone_region }}\n" content: "{{ timezone_region }}\n"
register: tz_change register: tz_write
- name: Debug timezone set - name: Debug timezone set
ansible.builtin.debug: ansible.builtin.debug:
msg: "Timezone set to {{ timezone_region }} successfully." msg: "Timezone set to {{ timezone_region }} successfully."
when: tz_change.changed when: tz_copy.changed or tz_write.changed
# ---------------------------------------------------------- # ----------------------------------------------------------
# UBUNTU NTP (systemd-timesyncd) # UBUNTU NTP (systemd-timesyncd)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Configure NTP for Ubuntu (systemd-timesyncd) - name: Configure NTP for Ubuntu (systemd-timesyncd)
block: ansible.builtin.copy:
- name: Create timesyncd.conf dest: /etc/systemd/timesyncd.conf
ansible.builtin.copy: content: |
dest: /etc/systemd/timesyncd.conf [Time]
content: | NTP={{ ntp_server }}
[Time] FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org
NTP={{ ntp_server }} register: ubuntu_ntp
FallbackNTP=1.in.pool.ntp.org 2.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: "Ubuntu NTP configured with {{ ntp_server }}"
when: ubuntu_ntp.changed
when: ansible_facts['os_family'] == "Debian" 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) # ROCKY LINUX NTP (chrony)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Configure NTP for Rocky Linux (chrony) - name: Install chrony
block: ansible.builtin.package:
- name: Install chrony name: chrony
ansible.builtin.package: state: present
name: chrony
state: present
- name: Backup chrony.conf
ansible.builtin.copy:
src: /etc/chrony.conf
dest: /etc/chrony.conf.bak
remote_src: yes
- name: Remove existing server lines
ansible.builtin.replace:
path: /etc/chrony.conf
regexp: '^server .*'
replace: ''
register: removed_servers
- name: Add new time server entry
ansible.builtin.lineinfile:
path: /etc/chrony.conf
line: "server {{ ntp_server }} iburst"
- name: Restart chronyd
ansible.builtin.systemd:
name: chronyd
state: restarted
enabled: yes
- name: Debug Rocky NTP
ansible.builtin.debug:
msg: "Rocky Linux NTP configured with {{ ntp_server }}"
when: ansible_facts['os_family'] == "RedHat" 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
# ---------------------------------------------------------- # ----------------------------------------------------------
# Final output # PRINT TIME & NTP SOURCE
# ----------------------------------------------------------
- name: Read timedatectl status
ansible.builtin.command: timedatectl status
register: timedate_output
- name: Print server time + NTP source
ansible.builtin.debug:
msg: |
=== {{ inventory_hostname }} ===
Current Time: {{ timedate_output.stdout | regex_search('Local time:\\s*(.*)', '\\1') }}
Timezone: {{ timedate_output.stdout | regex_search('Time zone:\\s*(.*)', '\\1') }}
NTP Source: {{ timedate_output.stdout | regex_search('System clock synchronized:\\s*(.*)', '\\1') }}
NTP Server: {{ timedate_output.stdout | regex_search('NTP service:\\s*(.*)', '\\1') }}
# ----------------------------------------------------------
# FINAL SUMMARY
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Final summary - name: Final summary
ansible.builtin.debug: ansible.builtin.debug: