Update playbooks/time-region.yml
This commit is contained in:
+48
-40
@@ -1,16 +1,23 @@
|
||||
---
|
||||
- name: Configure timezone and NTP
|
||||
- name: Configure timezone and NTP (LXC-Aware)
|
||||
hosts: all
|
||||
become: yes
|
||||
|
||||
vars:
|
||||
# America/New_York correctly handles Eastern Standard/Daylight Time
|
||||
timezone_region: "America/New_York"
|
||||
ntp_server: "time.google.com"
|
||||
|
||||
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
|
||||
ansible.builtin.copy:
|
||||
@@ -25,76 +32,79 @@
|
||||
content: "{{ timezone_region }}\n"
|
||||
register: tz_write
|
||||
|
||||
- name: Debug timezone set
|
||||
ansible.builtin.debug:
|
||||
msg: "Timezone set to {{ timezone_region }} successfully."
|
||||
when: tz_copy.changed or tz_write.changed
|
||||
# ----------------------------------------------------------
|
||||
# LXC CONTAINER HANDLING (Disable NTP)
|
||||
# ----------------------------------------------------------
|
||||
- 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:
|
||||
path: /etc/systemd/timesyncd.conf
|
||||
regexp: '^#?NTP='
|
||||
line: "NTP={{ ntp_server }}"
|
||||
state: present
|
||||
when: ansible_os_family == "Debian"
|
||||
when:
|
||||
- ansible_os_family == "Debian"
|
||||
- ansible_virtualization_type != "lxc"
|
||||
notify: Restart timesyncd
|
||||
|
||||
- name: Disable default chrony pools/servers (RHEL-based)
|
||||
- name: Disable default chrony pools on VMs (RHEL-based)
|
||||
ansible.builtin.replace:
|
||||
path: /etc/chrony.conf
|
||||
regexp: '^(pool|server) .*'
|
||||
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:
|
||||
path: /etc/chrony.conf
|
||||
line: "server {{ ntp_server }} iburst"
|
||||
insertafter: EOF
|
||||
state: present
|
||||
when: ansible_os_family == "RedHat"
|
||||
when:
|
||||
- ansible_os_family == "RedHat"
|
||||
- ansible_virtualization_type != "lxc"
|
||||
notify: Restart chronyd
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# VERIFICATION
|
||||
# FINAL SUMMARY
|
||||
# ----------------------------------------------------------
|
||||
- name: Flush handlers to restart NTP services immediately
|
||||
ansible.builtin.meta: flush_handlers
|
||||
|
||||
- name: Verify current system time
|
||||
ansible.builtin.command: date
|
||||
register: check_time
|
||||
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
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Configuration successfully applied on {{ inventory_hostname }}"
|
||||
- "Setup complete on {{ inventory_hostname }} (Environment: {{ ansible_virtualization_type | default('unknown') }})"
|
||||
- "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:
|
||||
- name: Restart timesyncd
|
||||
@@ -102,11 +112,9 @@
|
||||
name: systemd-timesyncd
|
||||
state: restarted
|
||||
enabled: yes
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
- name: Restart chronyd
|
||||
ansible.builtin.systemd:
|
||||
name: chronyd
|
||||
state: restarted
|
||||
enabled: yes
|
||||
when: ansible_os_family == "RedHat"
|
||||
Reference in New Issue
Block a user