82 lines
2.7 KiB
YAML
82 lines
2.7 KiB
YAML
---
|
|
- name: Get available storage on target node
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/storage"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
validate_certs: false
|
|
register: storage_info
|
|
|
|
- name: Set storage backend (prefer local-zfs if available)
|
|
ansible.builtin.set_fact:
|
|
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
|
|
|
|
- name: Display selected storage
|
|
ansible.builtin.debug:
|
|
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
|
|
|
|
- name: Get list of VMs on target node
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
validate_certs: false
|
|
register: vm_list
|
|
|
|
- name: Find template VMID by name
|
|
ansible.builtin.set_fact:
|
|
vm_template_id: "{{ (vm_list.json.data | selectattr('name', 'equalto', vm_template_name) | first).vmid }}"
|
|
|
|
- name: Display found template
|
|
ansible.builtin.debug:
|
|
msg: "Found template '{{ vm_template_name }}' with VMID {{ vm_template_id }} on {{ target_node }}"
|
|
|
|
- name: Clone VM from template on target node
|
|
community.proxmox.proxmox_kvm:
|
|
api_host: "{{ proxmox_api_host }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_token_id: "{{ proxmox_api_token_id }}"
|
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
|
node: "{{ target_node }}"
|
|
vmid: "{{ vm_template_id }}"
|
|
clone: "template"
|
|
name: "{{ vm_hostname }}"
|
|
full: true
|
|
storage: "{{ target_storage }}"
|
|
timeout: 600
|
|
register: cloned_vm
|
|
|
|
- name: Wait for VM to unlock
|
|
ansible.builtin.wait_for:
|
|
timeout: 30
|
|
|
|
- name: Configure VM resources
|
|
community.proxmox.proxmox_kvm:
|
|
api_host: "{{ proxmox_api_host }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_token_id: "{{ proxmox_api_token_id }}"
|
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
|
node: "{{ target_node }}"
|
|
vmid: "{{ cloned_vm.vmid }}"
|
|
cores: "{{ cpu_cores }}"
|
|
memory: "{{ ram_gb | int * 1024 }}"
|
|
net:
|
|
net0: "virtio,bridge=vmbr0"
|
|
update: true
|
|
|
|
- name: Start the VM
|
|
community.proxmox.proxmox_kvm:
|
|
api_host: "{{ proxmox_api_host }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_token_id: "{{ proxmox_api_token_id }}"
|
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
|
node: "{{ target_node }}"
|
|
vmid: "{{ cloned_vm.vmid }}"
|
|
state: started
|
|
|
|
- name: Set VMID fact for later use
|
|
ansible.builtin.set_fact:
|
|
created_vmid: "{{ cloned_vm.vmid }}"
|