Update playbooks/time-region.yml

This commit is contained in:
2026-04-02 21:03:53 +00:00
parent 416e6a3142
commit 76d8329558
+49 -41
View File
@@ -1,16 +1,23 @@
--- ---
- name: Configure timezone and NTP - name: Configure timezone and NTP (LXC-Aware)
hosts: all hosts: all
become: yes become: yes
vars: vars:
# America/New_York correctly handles Eastern Standard/Daylight Time
timezone_region: "America/New_York" timezone_region: "America/New_York"
ntp_server: "time.google.com" ntp_server: "time.google.com"
tasks: tasks:
# ---------------------------------------------------------- # ----------------------------------------------------------
# TIMEZONE (LXC + Ubuntu + Rocky safe) # SETUP FACTS
# ----------------------------------------------------------
- name: Ensure virtualization facts are gathered
ansible.builtin.setup:
gather_subset:
- virtualization
# ----------------------------------------------------------
# TIMEZONE (Required for ALL systems, LXC and VMs)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Copy timezone file - name: Copy timezone file
ansible.builtin.copy: ansible.builtin.copy:
@@ -25,76 +32,79 @@
content: "{{ timezone_region }}\n" content: "{{ timezone_region }}\n"
register: tz_write register: tz_write
- name: Debug timezone set # ----------------------------------------------------------
ansible.builtin.debug: # LXC CONTAINER HANDLING (Disable NTP)
msg: "Timezone set to {{ timezone_region }} successfully." # ----------------------------------------------------------
when: tz_copy.changed or tz_write.changed - name: Stop and mask timesyncd on LXC (Ubuntu/Debian)
ansible.builtin.systemd:
name: systemd-timesyncd
state: stopped
enabled: no
masked: yes
when:
- ansible_os_family == "Debian"
- ansible_virtualization_type == "lxc"
- name: Stop and mask chronyd on LXC (RHEL-based)
ansible.builtin.systemd:
name: chronyd
state: stopped
enabled: no
masked: yes
when:
- ansible_os_family == "RedHat"
- ansible_virtualization_type == "lxc"
# ---------------------------------------------------------- # ----------------------------------------------------------
# NTP CONFIGURATION (OS Specific) # FULL VM / HARDWARE HANDLING (Configure NTP)
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Configure timesyncd (Ubuntu/Debian) - name: Configure timesyncd on VMs (Ubuntu/Debian)
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /etc/systemd/timesyncd.conf path: /etc/systemd/timesyncd.conf
regexp: '^#?NTP=' regexp: '^#?NTP='
line: "NTP={{ ntp_server }}" line: "NTP={{ ntp_server }}"
state: present state: present
when: ansible_os_family == "Debian" when:
- ansible_os_family == "Debian"
- ansible_virtualization_type != "lxc"
notify: Restart timesyncd notify: Restart timesyncd
- name: Disable default chrony pools/servers (RHEL-based) - name: Disable default chrony pools on VMs (RHEL-based)
ansible.builtin.replace: ansible.builtin.replace:
path: /etc/chrony.conf path: /etc/chrony.conf
regexp: '^(pool|server) .*' regexp: '^(pool|server) .*'
replace: '# \1 removed by Ansible' replace: '# \1 removed by Ansible'
when: ansible_os_family == "RedHat" when:
- ansible_os_family == "RedHat"
- ansible_virtualization_type != "lxc"
- name: Configure chrony (RHEL-based) - name: Configure chrony on VMs (RHEL-based)
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /etc/chrony.conf path: /etc/chrony.conf
line: "server {{ ntp_server }} iburst" line: "server {{ ntp_server }} iburst"
insertafter: EOF insertafter: EOF
state: present state: present
when: ansible_os_family == "RedHat" when:
- ansible_os_family == "RedHat"
- ansible_virtualization_type != "lxc"
notify: Restart chronyd notify: Restart chronyd
# ---------------------------------------------------------- # ----------------------------------------------------------
# VERIFICATION # FINAL SUMMARY
# ---------------------------------------------------------- # ----------------------------------------------------------
- name: Flush handlers to restart NTP services immediately
ansible.builtin.meta: flush_handlers
- name: Verify current system time - name: Verify current system time
ansible.builtin.command: date ansible.builtin.command: date
register: check_time register: check_time
changed_when: false 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 - name: Final summary
ansible.builtin.debug: ansible.builtin.debug:
msg: msg:
- "Configuration successfully applied on {{ inventory_hostname }}" - "Setup complete on {{ inventory_hostname }} (Environment: {{ ansible_virtualization_type | default('unknown') }})"
- "System Time: {{ check_time.stdout }}" - "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 (Only triggers for VMs)
# ---------------------------------------------------------- # ----------------------------------------------------------
handlers: handlers:
- name: Restart timesyncd - name: Restart timesyncd
@@ -102,11 +112,9 @@
name: systemd-timesyncd name: systemd-timesyncd
state: restarted state: restarted
enabled: yes enabled: yes
when: ansible_os_family == "Debian"
- 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_os_family == "RedHat"