Update playbooks/time-region-ntp.yml

This commit is contained in:
2025-11-15 05:06:43 +00:00
parent b96bfe5118
commit d8f2c77f01
+57 -34
View File
@@ -1,5 +1,5 @@
--- ---
- name: Configure timezone and NTP on Ubuntu & Rocky Linux - name: Configure timezone and NTP
hosts: all hosts: all
become: yes become: yes
@@ -8,29 +8,40 @@
ntp_server: "0.in.pool.ntp.org" ntp_server: "0.in.pool.ntp.org"
tasks: tasks:
# ----------------------------------------------------------
- name: Set timezone # TIMEZONE (Works on Ubuntu LXC + Rocky)
ansible.builtin.timezone: # ----------------------------------------------------------
name: "{{ timezone_region }}" - name: Set timezone (manual method for compatibility)
register: timezone_result
- name: Debug timezone result
ansible.builtin.debug:
msg: "Timezone changed to {{ timezone_region }} successfully."
when: timezone_result.changed
# -------------------------------------------------------------
# Ubuntu: configure timesyncd
# -------------------------------------------------------------
- name: Configure NTP on Ubuntu (systemd-timesyncd)
block: block:
- name: Enable and configure systemd-timesyncd - name: Copy timezone file
ansible.builtin.copy:
src: "/usr/share/zoneinfo/{{ timezone_region }}"
dest: /etc/localtime
remote_src: yes
- name: Write /etc/timezone
ansible.builtin.copy:
dest: /etc/timezone
content: "{{ timezone_region }}\n"
register: tz_change
- name: Debug timezone set
ansible.builtin.debug:
msg: "Timezone set to {{ timezone_region }} successfully."
when: tz_change.changed
# ----------------------------------------------------------
# UBUNTU NTP (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: |
[Time] [Time]
NTP={{ ntp_server }} NTP={{ ntp_server }}
FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org 3.in.pool.ntp.org FallbackNTP=1.in.pool.ntp.org 2.in.pool.ntp.org
register: ubuntu_ntp register: ubuntu_ntp
- name: Restart timesyncd - name: Restart timesyncd
@@ -39,29 +50,40 @@
state: restarted state: restarted
enabled: yes enabled: yes
- name: Debug ubuntu NTP - name: Debug Ubuntu NTP
ansible.builtin.debug: ansible.builtin.debug:
msg: "NTP configured on Ubuntu using {{ ntp_server }}" msg: "Ubuntu NTP configured with {{ ntp_server }}"
when: ubuntu_ntp.changed when: ubuntu_ntp.changed
when: ansible_facts['os_family'] == "Debian" when: ansible_facts['os_family'] == "Debian"
# ------------------------------------------------------------- # ----------------------------------------------------------
# Rocky Linux: configure chrony # ROCKY LINUX NTP (chrony)
# ------------------------------------------------------------- # ----------------------------------------------------------
- name: Configure NTP on Rocky Linux (chrony) - name: Configure NTP for Rocky Linux (chrony)
block: block:
- name: Install chrony - name: Install chrony
ansible.builtin.package: ansible.builtin.package:
name: chrony name: chrony
state: present state: present
- name: Configure chrony NTP source - 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: ansible.builtin.replace:
path: /etc/chrony.conf path: /etc/chrony.conf
regexp: '^server .*$' regexp: '^server .*'
replace: "server {{ ntp_server }} iburst" replace: ''
register: rocky_ntp register: removed_servers
- name: Add new time server entry
ansible.builtin.lineinfile:
path: /etc/chrony.conf
line: "server {{ ntp_server }} iburst"
- name: Restart chronyd - name: Restart chronyd
ansible.builtin.systemd: ansible.builtin.systemd:
@@ -69,14 +91,15 @@
state: restarted state: restarted
enabled: yes enabled: yes
- name: Debug rocky NTP - name: Debug Rocky NTP
ansible.builtin.debug: ansible.builtin.debug:
msg: "NTP configured on Rocky Linux using {{ ntp_server }}" msg: "Rocky Linux NTP configured with {{ ntp_server }}"
when: rocky_ntp.changed
when: ansible_facts['os_family'] == "RedHat" when: ansible_facts['os_family'] == "RedHat"
# Final debug # ----------------------------------------------------------
- name: Print final success message # Final output
# ----------------------------------------------------------
- name: Final summary
ansible.builtin.debug: ansible.builtin.debug:
msg: "Timezone and NTP configuration applied successfully on {{ inventory_hostname }}" msg: "Timezone + NTP successfully applied on {{ inventory_hostname }}"