120 lines
3.7 KiB
YAML
120 lines
3.7 KiB
YAML
---
|
|
- name: Configure timezone and NTP (LXC-Aware)
|
|
hosts: all
|
|
become: yes
|
|
|
|
vars:
|
|
timezone_region: "America/New_York"
|
|
ntp_server: "time.google.com"
|
|
|
|
tasks:
|
|
# ----------------------------------------------------------
|
|
# 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:
|
|
src: "/usr/share/zoneinfo/{{ timezone_region }}"
|
|
dest: /etc/localtime
|
|
remote_src: yes
|
|
register: tz_copy
|
|
|
|
- name: Write /etc/timezone
|
|
ansible.builtin.copy:
|
|
dest: /etc/timezone
|
|
content: "{{ timezone_region }}\n"
|
|
register: tz_write
|
|
|
|
# ----------------------------------------------------------
|
|
# 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"
|
|
|
|
# ----------------------------------------------------------
|
|
# FULL VM / HARDWARE HANDLING (Configure NTP)
|
|
# ----------------------------------------------------------
|
|
- 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"
|
|
- ansible_virtualization_type != "lxc"
|
|
notify: Restart timesyncd
|
|
|
|
- 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"
|
|
- ansible_virtualization_type != "lxc"
|
|
|
|
- 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"
|
|
- ansible_virtualization_type != "lxc"
|
|
notify: Restart chronyd
|
|
|
|
# ----------------------------------------------------------
|
|
# FINAL SUMMARY
|
|
# ----------------------------------------------------------
|
|
- name: Verify current system time
|
|
ansible.builtin.command: date
|
|
register: check_time
|
|
changed_when: false
|
|
|
|
- name: Final summary
|
|
ansible.builtin.debug:
|
|
msg:
|
|
- "Setup complete on {{ inventory_hostname }} (Environment: {{ ansible_virtualization_type | default('unknown') }})"
|
|
- "System Time: {{ check_time.stdout }}"
|
|
|
|
# ----------------------------------------------------------
|
|
# HANDLERS (Only triggers for VMs)
|
|
# ----------------------------------------------------------
|
|
handlers:
|
|
- name: Restart timesyncd
|
|
ansible.builtin.systemd:
|
|
name: systemd-timesyncd
|
|
state: restarted
|
|
enabled: yes
|
|
|
|
- name: Restart chronyd
|
|
ansible.builtin.systemd:
|
|
name: chronyd
|
|
state: restarted
|
|
enabled: yes |