93 lines
3.3 KiB
YAML
93 lines
3.3 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 LXC from template via API
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ lxc_template_node }}/lxc/{{ lxc_template_id }}/clone"
|
|
method: POST
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
body_format: form-urlencoded
|
|
body:
|
|
newid: "{{ next_vmid }}"
|
|
hostname: "{{ vm_hostname }}"
|
|
full: "1"
|
|
storage: "local-lvm"
|
|
validate_certs: false
|
|
register: clone_result
|
|
|
|
- name: Wait for clone task to complete
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ lxc_template_node }}/lxc/{{ next_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.status == 200
|
|
retries: 60
|
|
delay: 5
|
|
|
|
- name: Migrate LXC to target node
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ lxc_template_node }}/lxc/{{ 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 }}"
|
|
restart: "0"
|
|
validate_certs: false
|
|
when: lxc_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 }}/lxc/{{ next_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.status == 200
|
|
retries: 30
|
|
delay: 10
|
|
when: lxc_template_node != target_node
|
|
|
|
- name: Configure LXC resources
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc/{{ next_vmid }}/config"
|
|
method: PUT
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
body_format: form-urlencoded
|
|
body:
|
|
cores: "{{ cpu_cores }}"
|
|
memory: "{{ ram_gb | int * 1024 }}"
|
|
net0: "name=eth0,bridge=vmbr0,ip=dhcp"
|
|
validate_certs: false
|
|
|
|
- name: Start the LXC container
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc/{{ next_vmid }}/status/start"
|
|
method: POST
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
|
|
validate_certs: false
|
|
|
|
- name: Set VMID fact for later use
|
|
ansible.builtin.set_fact:
|
|
created_vmid: "{{ next_vmid }}"
|