node specific templating
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
---
|
---
|
||||||
lxc_template_id: "128"
|
lxc_template_id: "128"
|
||||||
lxc_template_node: "homestrg1"
|
|
||||||
lxc_template_hostname: "ubuntu24lxc"
|
lxc_template_hostname: "ubuntu24lxc"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
validate_certs: false
|
validate_certs: false
|
||||||
register: storage_info
|
register: storage_info
|
||||||
|
|
||||||
- name: Set target storage backend (prefer local-zfs if available)
|
- name: Set storage backend (prefer local-zfs if available)
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
|
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
|
||||||
|
|
||||||
@@ -16,86 +16,23 @@
|
|||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
|
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
|
||||||
|
|
||||||
- name: Clone LXC from template (on template's node)
|
- name: Clone LXC from template on target node
|
||||||
community.proxmox.proxmox:
|
community.proxmox.proxmox:
|
||||||
api_host: "{{ proxmox_api_host }}"
|
api_host: "{{ proxmox_api_host }}"
|
||||||
api_user: "{{ proxmox_api_user }}"
|
api_user: "{{ proxmox_api_user }}"
|
||||||
api_token_id: "{{ proxmox_api_token_id }}"
|
api_token_id: "{{ proxmox_api_token_id }}"
|
||||||
api_token_secret: "{{ proxmox_api_token_secret }}"
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
||||||
node: "{{ lxc_template_node }}"
|
node: "{{ target_node }}"
|
||||||
clone: "{{ lxc_template_id }}"
|
clone: "{{ lxc_template_id }}"
|
||||||
hostname: "{{ vm_hostname }}"
|
hostname: "{{ vm_hostname }}"
|
||||||
full: true
|
full: true
|
||||||
storage: "local-lvm"
|
storage: "{{ target_storage }}"
|
||||||
timeout: 600
|
timeout: 600
|
||||||
register: cloned_lxc
|
register: cloned_lxc
|
||||||
|
|
||||||
- name: Migrate LXC to target node via API
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ lxc_template_node }}/lxc/{{ cloned_lxc.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 }}"
|
|
||||||
restart: "0"
|
|
||||||
validate_certs: false
|
|
||||||
status_code: [200]
|
|
||||||
register: migrate_task
|
|
||||||
when: lxc_template_node != target_node
|
|
||||||
|
|
||||||
- name: Wait for migration task to complete
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc/{{ cloned_lxc.vmid }}/status/current"
|
|
||||||
method: GET
|
|
||||||
headers:
|
|
||||||
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
||||||
validate_certs: false
|
|
||||||
register: lxc_check
|
|
||||||
until: lxc_check.status == 200
|
|
||||||
retries: 60
|
|
||||||
delay: 10
|
|
||||||
when: lxc_template_node != target_node
|
|
||||||
|
|
||||||
- name: Wait for LXC to unlock after migration
|
|
||||||
ansible.builtin.wait_for:
|
|
||||||
timeout: 30
|
|
||||||
when: lxc_template_node != target_node
|
|
||||||
|
|
||||||
- name: Move LXC rootfs to target storage (if different from local-lvm)
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc/{{ cloned_lxc.vmid }}/move_volume"
|
|
||||||
method: POST
|
|
||||||
headers:
|
|
||||||
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
||||||
body_format: form-urlencoded
|
|
||||||
body:
|
|
||||||
volume: "rootfs"
|
|
||||||
storage: "{{ target_storage }}"
|
|
||||||
delete: "1"
|
|
||||||
validate_certs: false
|
|
||||||
status_code: [200]
|
|
||||||
register: move_volume_task
|
|
||||||
when: target_storage != 'local-lvm'
|
|
||||||
|
|
||||||
- name: Wait for volume move to complete
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc/{{ cloned_lxc.vmid }}/status/current"
|
|
||||||
method: GET
|
|
||||||
headers:
|
|
||||||
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
||||||
validate_certs: false
|
|
||||||
register: lxc_status
|
|
||||||
until: lxc_status.json.data.lock is not defined
|
|
||||||
retries: 60
|
|
||||||
delay: 10
|
|
||||||
when: target_storage != 'local-lvm'
|
|
||||||
|
|
||||||
- name: Wait for LXC to unlock
|
- name: Wait for LXC to unlock
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
timeout: 30
|
timeout: 30
|
||||||
when: target_storage != 'local-lvm'
|
|
||||||
|
|
||||||
- name: Configure LXC resources
|
- name: Configure LXC resources
|
||||||
community.proxmox.proxmox:
|
community.proxmox.proxmox:
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
---
|
---
|
||||||
vm_template_id: "118"
|
vm_template_id: "118"
|
||||||
vm_template_node: "homestrg1"
|
|
||||||
vm_template_hostname: "ubuntu24vm"
|
vm_template_hostname: "ubuntu24vm"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
validate_certs: false
|
validate_certs: false
|
||||||
register: storage_info
|
register: storage_info
|
||||||
|
|
||||||
- name: Set target storage backend (prefer local-zfs if available)
|
- name: Set storage backend (prefer local-zfs if available)
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
|
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
|
||||||
|
|
||||||
@@ -16,84 +16,21 @@
|
|||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
|
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
|
||||||
|
|
||||||
- name: Clone VM from template (on template's node)
|
- name: Clone VM from template on target node
|
||||||
community.proxmox.proxmox_kvm:
|
community.proxmox.proxmox_kvm:
|
||||||
api_host: "{{ proxmox_api_host }}"
|
api_host: "{{ proxmox_api_host }}"
|
||||||
api_user: "{{ proxmox_api_user }}"
|
api_user: "{{ proxmox_api_user }}"
|
||||||
api_token_id: "{{ proxmox_api_token_id }}"
|
api_token_id: "{{ proxmox_api_token_id }}"
|
||||||
api_token_secret: "{{ proxmox_api_token_secret }}"
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
||||||
node: "{{ vm_template_node }}"
|
node: "{{ target_node }}"
|
||||||
vmid: "{{ vm_template_id }}"
|
vmid: "{{ vm_template_id }}"
|
||||||
clone: "template"
|
clone: "template"
|
||||||
name: "{{ vm_hostname }}"
|
name: "{{ vm_hostname }}"
|
||||||
full: true
|
full: true
|
||||||
storage: "local-lvm"
|
storage: "{{ target_storage }}"
|
||||||
timeout: 600
|
timeout: 600
|
||||||
register: cloned_vm
|
register: cloned_vm
|
||||||
|
|
||||||
- name: Migrate VM to target node via API
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ vm_template_node }}/qemu/{{ cloned_vm.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
|
|
||||||
status_code: [200]
|
|
||||||
register: migrate_task
|
|
||||||
when: vm_template_node != target_node
|
|
||||||
|
|
||||||
- name: Wait for migration task to complete
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ cloned_vm.vmid }}/status/current"
|
|
||||||
method: GET
|
|
||||||
headers:
|
|
||||||
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
||||||
validate_certs: false
|
|
||||||
register: vm_check
|
|
||||||
until: vm_check.status == 200
|
|
||||||
retries: 60
|
|
||||||
delay: 10
|
|
||||||
when: vm_template_node != target_node
|
|
||||||
|
|
||||||
- name: Wait for VM to unlock after migration
|
|
||||||
ansible.builtin.wait_for:
|
|
||||||
timeout: 30
|
|
||||||
when: vm_template_node != target_node
|
|
||||||
|
|
||||||
- name: Move VM disk to target storage (if different from local-lvm)
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ cloned_vm.vmid }}/move_disk"
|
|
||||||
method: POST
|
|
||||||
headers:
|
|
||||||
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
||||||
body_format: form-urlencoded
|
|
||||||
body:
|
|
||||||
disk: "scsi0"
|
|
||||||
storage: "{{ target_storage }}"
|
|
||||||
delete: "1"
|
|
||||||
validate_certs: false
|
|
||||||
status_code: [200]
|
|
||||||
register: move_disk_task
|
|
||||||
when: target_storage != 'local-lvm'
|
|
||||||
|
|
||||||
- name: Wait for disk move to complete
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ cloned_vm.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.json.data.lock is not defined
|
|
||||||
retries: 60
|
|
||||||
delay: 10
|
|
||||||
when: target_storage != 'local-lvm'
|
|
||||||
|
|
||||||
- name: Wait for VM to unlock
|
- name: Wait for VM to unlock
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
timeout: 30
|
timeout: 30
|
||||||
|
|||||||
Reference in New Issue
Block a user