Files
deploy-vm/tasks/migrate.yml
T
2026-05-29 18:30:16 +02:00

66 lines
2.8 KiB
YAML

---
# Migration optionnelle vers proxmox_node si différent du nœud du template.
# Déclenchée uniquement si proxmox_node est défini et != resolved_node (nœud du clone).
# La VM doit être arrêtée — ce qui est le cas entre le clone et le démarrage.
- name: deploy-vm | migrate VM to target node
when:
- proxmox_node | default('') | length > 0
- proxmox_node != resolved_node
block:
# Proxmox impose un ticket de session pour les appels POST directs (CSRF)
- name: deploy-vm | authenticate to Proxmox API (migration)
ansible.builtin.uri:
url: "https://{{ proxmox_host }}:8006/api2/json/access/ticket"
method: POST
body_format: form-urlencoded
body:
username: "{{ proxmox_api_user }}"
password: "{{ proxmox_api_password }}"
validate_certs: "{{ proxmox_validate_certs }}"
status_code: 200
register: _proxmox_auth
no_log: true
- name: "deploy-vm | migrate '{{ vm_name }}' : {{ resolved_node }} → {{ proxmox_node }}"
ansible.builtin.uri:
url: >-
https://{{ proxmox_host }}:8006/api2/json/nodes/{{ resolved_node }}/qemu/{{ resolved_vm_id }}/migrate
method: POST
headers:
Cookie: "PVEAuthCookie={{ _proxmox_auth.json.data.ticket }}"
CSRFPreventionToken: "{{ _proxmox_auth.json.data.CSRFPreventionToken }}"
body_format: form-urlencoded
body:
target: "{{ proxmox_node }}"
online: 0
"with-local-disks": "{{ 1 if vm_migrate_with_local_disks | bool else 0 }}"
validate_certs: "{{ proxmox_validate_certs }}"
status_code: 200
register: _migrate_result
# Proxmox retourne un UPID (ex: UPID:node:pid:…) — on poll jusqu'à status = stopped
- name: deploy-vm | wait for migration task to complete
ansible.builtin.uri:
url: >-
https://{{ proxmox_host }}:8006/api2/json/nodes/{{ resolved_node }}/tasks/{{ _migrate_result.json.data | urlencode }}/status
method: GET
headers:
Cookie: "PVEAuthCookie={{ _proxmox_auth.json.data.ticket }}"
validate_certs: "{{ proxmox_validate_certs }}"
register: _migration_status
until: _migration_status.json.data.status == 'stopped'
retries: "{{ (vm_wait_timeout | int / 10) | int }}"
delay: 10
- name: deploy-vm | assert migration succeeded
ansible.builtin.assert:
that: _migration_status.json.data.exitstatus == 'OK'
fail_msg: >-
Migration de '{{ vm_name }}' vers '{{ proxmox_node }}' échouée :
{{ _migration_status.json.data.exitstatus }}
- name: deploy-vm | update resolved_node after migration
ansible.builtin.set_fact:
resolved_node: "{{ proxmox_node }}"