Refactor inventory into OS groups and update playbooks accordingly
Group hosts by OS (ubuntu/rocky) in inventory, refactor playbooks to use group-targeted plays instead of ansible_os_family conditionals, add bot host, and add Docker volume backup playbook via rsync to storage.
This commit is contained in:
+73
-78
@@ -1,120 +1,115 @@
|
||||
---
|
||||
- name: Configure timezone and NTP (LXC-Aware)
|
||||
- name: Configure timezone on all hosts
|
||||
hosts: all
|
||||
become: yes
|
||||
become: true
|
||||
gather_facts: true
|
||||
|
||||
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:
|
||||
- virtual
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# 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
|
||||
remote_src: true
|
||||
|
||||
- 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)
|
||||
- name: Disable NTP on Ubuntu LXC containers
|
||||
hosts: ubuntu
|
||||
become: true
|
||||
|
||||
tasks:
|
||||
- name: Stop and mask timesyncd on LXC
|
||||
ansible.builtin.systemd:
|
||||
name: systemd-timesyncd
|
||||
state: stopped
|
||||
enabled: no
|
||||
masked: yes
|
||||
when:
|
||||
- ansible_os_family == "Debian"
|
||||
- ansible_virtualization_type == "lxc"
|
||||
enabled: false
|
||||
masked: true
|
||||
when: ansible_virtualization_type == "lxc"
|
||||
|
||||
- name: Stop and mask chronyd on LXC (RHEL-based)
|
||||
- name: Disable NTP on Rocky Linux LXC containers
|
||||
hosts: rocky
|
||||
become: true
|
||||
|
||||
tasks:
|
||||
- name: Stop and mask chronyd on LXC
|
||||
ansible.builtin.systemd:
|
||||
name: chronyd
|
||||
state: stopped
|
||||
enabled: no
|
||||
masked: yes
|
||||
when:
|
||||
- ansible_os_family == "RedHat"
|
||||
- ansible_virtualization_type == "lxc"
|
||||
enabled: false
|
||||
masked: true
|
||||
when: ansible_virtualization_type == "lxc"
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# FULL VM / HARDWARE HANDLING (Configure NTP)
|
||||
# ----------------------------------------------------------
|
||||
- name: Configure timesyncd on VMs (Ubuntu/Debian)
|
||||
- name: Configure NTP on Ubuntu VMs
|
||||
hosts: ubuntu
|
||||
become: true
|
||||
|
||||
vars:
|
||||
ntp_server: "time.google.com"
|
||||
|
||||
tasks:
|
||||
- name: Configure timesyncd
|
||||
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"
|
||||
when: 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
|
||||
enabled: true
|
||||
|
||||
- name: Configure NTP on Rocky Linux VMs
|
||||
hosts: rocky
|
||||
become: true
|
||||
|
||||
vars:
|
||||
ntp_server: "time.google.com"
|
||||
|
||||
tasks:
|
||||
- name: Disable default chrony pools
|
||||
ansible.builtin.replace:
|
||||
path: /etc/chrony.conf
|
||||
regexp: '^(pool|server) .*'
|
||||
replace: '# \1 removed by Ansible'
|
||||
when: ansible_virtualization_type != "lxc"
|
||||
|
||||
- name: Configure chrony NTP server
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/chrony.conf
|
||||
line: "server {{ ntp_server }} iburst"
|
||||
insertafter: EOF
|
||||
state: present
|
||||
when: ansible_virtualization_type != "lxc"
|
||||
notify: Restart chronyd
|
||||
|
||||
handlers:
|
||||
- name: Restart chronyd
|
||||
ansible.builtin.systemd:
|
||||
name: chronyd
|
||||
state: restarted
|
||||
enabled: yes
|
||||
enabled: true
|
||||
|
||||
- name: Verify time configuration
|
||||
hosts: all
|
||||
become: true
|
||||
|
||||
tasks:
|
||||
- name: Check current system time
|
||||
ansible.builtin.command: date
|
||||
register: check_time
|
||||
changed_when: false
|
||||
|
||||
- name: Summary
|
||||
ansible.builtin.debug:
|
||||
msg: "{{ inventory_hostname }} ({{ ansible_virtualization_type | default('unknown') }}) - {{ check_time.stdout }}"
|
||||
Reference in New Issue
Block a user