151 lines
4.6 KiB
YAML
151 lines
4.6 KiB
YAML
---
|
|
# preflight-checks.yml - Validate environment before running main tasks
|
|
|
|
- name: "[PREFLIGHT] Get Ansible python interpreter on ansible controller"
|
|
ansible.builtin.set_fact:
|
|
controller_python: "{{ ansible_playbook_python }}"
|
|
delegate_to: localhost
|
|
|
|
- name: "[PREFLIGHT] Install netaddr in ansible controller Python environment"
|
|
ansible.builtin.pip:
|
|
name: netaddr
|
|
state: present
|
|
executable: "{{ controller_python | dirname }}/pip"
|
|
delegate_to: localhost
|
|
run_once: true
|
|
become: false
|
|
|
|
|
|
|
|
- name: "[PREFLIGHT] Check if running on Proxmox host"
|
|
ansible.builtin.stat:
|
|
path: "/etc/pve/nodes"
|
|
register: pve_nodes
|
|
failed_when: not pve_nodes.stat.exists
|
|
changed_when: false
|
|
|
|
- name: "[PREFLIGHT] Verify qm command is available"
|
|
ansible.builtin.command: which qm
|
|
changed_when: false
|
|
failed_when: false
|
|
register: qm_check
|
|
|
|
- name: "[PREFLIGHT] Fail if qm not found"
|
|
ansible.builtin.fail:
|
|
msg: "qm command not found. This role requires Proxmox VE to be installed."
|
|
when: qm_check.rc != 0
|
|
|
|
- name: "[PREFLIGHT] Check if user can run qm commands"
|
|
ansible.builtin.command: qm list
|
|
become: true
|
|
changed_when: false
|
|
register: qm_version
|
|
|
|
- name: "[PREFLIGHT] Display Proxmox version"
|
|
ansible.builtin.debug:
|
|
msg: "Proxmox Version: {{ qm_version.stdout }}"
|
|
|
|
- name: "[PREFLIGHT] Verify storage pool exists"
|
|
ansible.builtin.shell: "pvesm status | awk '{print $1}' | grep -w {{ storage }}"
|
|
changed_when: false
|
|
register: storage_check
|
|
failed_when: storage_check.rc != 0
|
|
|
|
- name: "[PREFLIGHT] Fail if storage not found"
|
|
ansible.builtin.fail:
|
|
msg: "Storage pool '{{ storage }}' not found. Available pools: run 'pvesm status'"
|
|
when: storage_check.rc != 0
|
|
|
|
- name: Combine SSH public keys into one file
|
|
ansible.builtin.copy:
|
|
dest: '{{ ssh_keys_file }}'
|
|
content: |
|
|
{% for key in ssh_public_keys %}
|
|
{{ key }}
|
|
{% endfor %}
|
|
mode: '0644'
|
|
|
|
- name: "[PREFLIGHT] Check SSH key file exists"
|
|
ansible.builtin.stat:
|
|
path: "{{ ssh_keys_file | expanduser }}"
|
|
register: ssh_key_file
|
|
failed_when: not ssh_key_file.stat.exists
|
|
changed_when: false
|
|
|
|
# - name: "[PREFLIGHT] Check SSH key file exists"
|
|
# ansible.builtin.stat:
|
|
# path: "{{ ssh_key_path | expanduser }}"
|
|
# register: ssh_key_file
|
|
# failed_when: not ssh_key_file.stat.exists
|
|
# changed_when: false
|
|
|
|
- name: "[PREFLIGHT] Validate VM ID is unique"
|
|
ansible.builtin.command: "test ! -f /etc/pve/qemu-server/{{ vm_id }}.conf"
|
|
changed_when: false
|
|
failed_when: false
|
|
register: vm_id_check
|
|
|
|
- name: "[PREFLIGHT] Warn if VM ID already exists"
|
|
ansible.builtin.debug:
|
|
msg: "WARNING: VM ID {{ vm_id }} already exists. It will be skipped or updated."
|
|
when: vm_id_check.rc != 0
|
|
|
|
- name: "[PREFLIGHT] Validate clone IDs are unique"
|
|
ansible.builtin.command: "test ! -f /etc/pve/qemu-server/{{ item.id }}.conf"
|
|
changed_when: false
|
|
failed_when: false
|
|
loop: "{{ clones }}"
|
|
register: clone_id_checks
|
|
when: create_clones | default(false)
|
|
|
|
- name: "[PREFLIGHT] Warn if any clone IDs already exist"
|
|
ansible.builtin.debug:
|
|
msg: "WARNING: Clone ID {{ item.item.id }} already exists and will be skipped."
|
|
loop: "{{ clone_id_checks.results }}"
|
|
when: item.rc != 0 and create_clones | default(false)
|
|
|
|
- name: "[PREFLIGHT] Validate IP address format for clones"
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "item.ip | ansible.utils.ipaddr"
|
|
fail_msg: "Invalid IP address for clone {{ item.id }}: {{ item.ip }}"
|
|
loop: "{{ clones }}"
|
|
when: create_clones | default(false)
|
|
|
|
- name: "[PREFLIGHT] Validate static IP address format (if not DHCP)"
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "ip_address | ansible.utils.ipaddr"
|
|
fail_msg: "Invalid static IP address: {{ ip_address }}"
|
|
when: ip_mode == 'static'
|
|
|
|
- name: "[PREFLIGHT] Validate gateway IP address"
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "gateway | ansible.utils.ipaddr"
|
|
fail_msg: "Invalid gateway IP address: {{ gateway }}"
|
|
|
|
- name: "[PREFLIGHT] Validate DNS servers"
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "item | ansible.utils.ipaddr"
|
|
fail_msg: "Invalid DNS server IP: {{ item }}"
|
|
loop: "{{ dns }}"
|
|
when: dns is defined and dns | length > 0
|
|
|
|
- name: "[PREFLIGHT] Check snippets storage exists"
|
|
ansible.builtin.stat:
|
|
path: "/var/lib/vz/snippets"
|
|
register: snippets_dir
|
|
failed_when: not snippets_dir.stat.exists
|
|
changed_when: false
|
|
|
|
- name: "[PREFLIGHT] Summary - All checks passed"
|
|
ansible.builtin.debug:
|
|
msg: |
|
|
✓ Proxmox environment validated
|
|
✓ Storage pool '{{ storage }}' available
|
|
✓ SSH key found at {{ ssh_key_path }}
|
|
✓ VM ID {{ vm_id }} is available
|
|
✓ Ready to create VM: {{ hostname }}
|