2025-11-15 17:22:21 +01:00
|
|
|
---
|
|
|
|
|
# create-vm.yml - Create base VM on Proxmox
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Check if VM already exists"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.stat:
|
2025-11-15 17:22:21 +01:00
|
|
|
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
|
|
|
|
register: vm_conf
|
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Display VM status"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
msg: "VM {{ vm_id }} ({{ hostname }}) - Status: {{ 'ALREADY EXISTS' if vm_conf.stat.exists else 'WILL BE CREATED' }}"
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Create base VM"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.command: >
|
2025-11-15 17:22:21 +01:00
|
|
|
qm create {{ vm_id }}
|
|
|
|
|
--name {{ hostname }}
|
|
|
|
|
--memory {{ memory }}
|
|
|
|
|
--cores {{ cores }}
|
|
|
|
|
--cpu {{ cpu_type }}
|
|
|
|
|
--net0 virtio,bridge={{ bridge }},macaddr={{ mac_address }}
|
|
|
|
|
--agent 1
|
|
|
|
|
register: vm_create
|
|
|
|
|
when: not vm_conf.stat.exists
|
|
|
|
|
changed_when: vm_create.rc == 0
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Handle VM creation error"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.fail:
|
2025-11-15 17:22:21 +01:00
|
|
|
msg: |
|
|
|
|
|
Failed to create VM {{ vm_id }}:
|
|
|
|
|
{{ vm_create.stderr | default('No error message') }}
|
|
|
|
|
when:
|
|
|
|
|
- not vm_conf.stat.exists
|
|
|
|
|
- vm_create is failed
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Verify VM was created"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.stat:
|
2025-11-15 17:22:21 +01:00
|
|
|
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
|
|
|
|
register: vm_conf_verify
|
|
|
|
|
changed_when: false
|
|
|
|
|
failed_when: not vm_conf_verify.stat.exists
|
|
|
|
|
|
|
|
|
|
- name: "[VM] Display VM creation result"
|
2025-11-15 21:49:12 +01:00
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
msg: "✓ VM {{ vm_id }} created successfully"
|
|
|
|
|
when: not vm_conf.stat.exists
|