86 lines
2.7 KiB
YAML
86 lines
2.7 KiB
YAML
---
|
|
- name: Get next available VMID from Proxmox API
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/cluster/nextid"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
validate_certs: false
|
|
register: nextid_response
|
|
|
|
- name: Set VMID fact
|
|
ansible.builtin.set_fact:
|
|
next_vmid: "{{ nextid_response.json.data }}"
|
|
|
|
- name: Clone VM from template
|
|
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: "{{ vm_template_node }}"
|
|
vmid: "{{ vm_template_id }}"
|
|
clone: "template"
|
|
name: "{{ vm_hostname }}"
|
|
newid: "{{ next_vmid }}"
|
|
full: true
|
|
storage: "local-lvm"
|
|
timeout: 300
|
|
register: cloned_vm
|
|
|
|
- name: Migrate VM to target node
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ vm_template_node }}/qemu/{{ next_vmid }}/migrate"
|
|
method: POST
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
body_format: form-urlencoded
|
|
body:
|
|
target: "{{ target_node }}"
|
|
online: "0"
|
|
with-local-disks: "1"
|
|
validate_certs: false
|
|
when: vm_template_node != target_node
|
|
register: migrate_result
|
|
|
|
- name: Wait for migration to complete
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ next_vmid }}/status/current"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
validate_certs: false
|
|
register: vm_status
|
|
until: vm_status.status == 200
|
|
retries: 30
|
|
delay: 10
|
|
when: vm_template_node != target_node
|
|
|
|
- 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: "{{ next_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: "{{ next_vmid }}"
|
|
state: started
|
|
|
|
- name: Set VMID fact for later use
|
|
ansible.builtin.set_fact:
|
|
created_vmid: "{{ next_vmid }}"
|