Update playbooks/time-region-ntp.yml

This commit is contained in:
2025-11-15 05:07:59 +00:00
parent d8f2c77f01
commit d6d422d470
+38 -18
View File
@@ -9,33 +9,30 @@
tasks: tasks:
# ---------------------------------------------------------- # ----------------------------------------------------------
# TIMEZONE (Works on Ubuntu LXC + Rocky) # TIMEZONE (LXC + Ubuntu + Rocky safe)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Set timezone (manual method for compatibility)
block:
- name: Copy timezone file - name: Copy timezone file
ansible.builtin.copy: ansible.builtin.copy:
src: "/usr/share/zoneinfo/{{ timezone_region }}" src: "/usr/share/zoneinfo/{{ timezone_region }}"
dest: /etc/localtime dest: /etc/localtime
remote_src: yes remote_src: yes
register: tz_copy
- 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:
- name: Create timesyncd.conf
ansible.builtin.copy: ansible.builtin.copy:
dest: /etc/systemd/timesyncd.conf dest: /etc/systemd/timesyncd.conf
content: | content: |
@@ -43,62 +40,85 @@
NTP={{ ntp_server }} NTP={{ ntp_server }}
FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org
register: ubuntu_ntp register: ubuntu_ntp
when: ansible_facts['os_family'] == "Debian"
- name: Restart timesyncd - name: Restart timesyncd (Ubuntu)
ansible.builtin.systemd: ansible.builtin.systemd:
name: systemd-timesyncd name: systemd-timesyncd
state: restarted state: restarted
enabled: yes enabled: yes
when: ansible_facts['os_family'] == "Debian"
- name: Debug Ubuntu NTP - name: Debug Ubuntu NTP
ansible.builtin.debug: ansible.builtin.debug:
msg: "Ubuntu NTP configured with {{ ntp_server }}" msg: "Ubuntu NTP configured with {{ ntp_server }}"
when: ubuntu_ntp.changed when:
- ansible_facts['os_family'] == "Debian"
when: ansible_facts['os_family'] == "Debian" - ubuntu_ntp.changed
# ---------------------------------------------------------- # ----------------------------------------------------------
# ROCKY LINUX NTP (chrony) # ROCKY LINUX NTP (chrony)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Configure NTP for Rocky Linux (chrony)
block:
- name: Install chrony - name: Install chrony
ansible.builtin.package: ansible.builtin.package:
name: chrony name: chrony
state: present state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Backup chrony.conf - name: Backup chrony.conf
ansible.builtin.copy: ansible.builtin.copy:
src: /etc/chrony.conf src: /etc/chrony.conf
dest: /etc/chrony.conf.bak dest: /etc/chrony.conf.bak
remote_src: yes remote_src: yes
when: ansible_facts['os_family'] == "RedHat"
- name: Remove existing server lines - name: Remove existing server lines
ansible.builtin.replace: ansible.builtin.replace:
path: /etc/chrony.conf path: /etc/chrony.conf
regexp: '^server .*' regexp: '^server .*'
replace: '' replace: ''
register: removed_servers register: rocky_cleanup
when: ansible_facts['os_family'] == "RedHat"
- name: Add new time server entry - name: Add new NTP server
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /etc/chrony.conf path: /etc/chrony.conf
line: "server {{ ntp_server }} iburst" line: "server {{ ntp_server }} iburst"
register: rocky_add
when: ansible_facts['os_family'] == "RedHat"
- name: Restart chronyd - name: Restart chronyd
ansible.builtin.systemd: ansible.builtin.systemd:
name: chronyd name: chronyd
state: restarted state: restarted
enabled: yes enabled: yes
when: ansible_facts['os_family'] == "RedHat"
- name: Debug Rocky NTP - name: Debug Rocky NTP
ansible.builtin.debug: ansible.builtin.debug:
msg: "Rocky Linux NTP configured with {{ ntp_server }}" msg: "Rocky Linux NTP configured with {{ ntp_server }}"
when:
when: ansible_facts['os_family'] == "RedHat" - 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: