attempt to use proxmox api instead

This commit is contained in:
2026-02-02 14:10:25 -05:00
parent 9a4bc8e387
commit 21bfd95fc3
4 changed files with 84 additions and 70 deletions

View File

@@ -52,7 +52,6 @@ proxmox-template-clone/
```yaml
- community.general >= 8.0.0
- community.proxmox >= 0.1.0
- ansible.posix >= 1.5.0
```

View File

@@ -2,7 +2,5 @@
collections:
- name: community.general
version: ">=8.0.0"
- name: community.proxmox
version: ">=0.1.0"
- name: ansible.posix
version: ">=1.5.0"

View File

@@ -12,20 +12,32 @@
ansible.builtin.set_fact:
next_vmid: "{{ nextid_response.json.data }}"
- name: Clone LXC from template
community.proxmox.proxmox:
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: "{{ lxc_template_node }}"
clone: "{{ lxc_template_id }}"
- 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 }}"
vmid: "{{ next_vmid }}"
full: true
full: "1"
storage: "local-lvm"
timeout: 300
register: cloned_lxc
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:
@@ -55,28 +67,25 @@
when: lxc_template_node != target_node
- name: Configure LXC resources
community.proxmox.proxmox:
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 }}"
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 }}"
netif:
net0: "name=eth0,bridge=vmbr0,ip=dhcp"
state: present
validate_certs: false
- name: Start the LXC container
community.proxmox.proxmox:
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
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:

View File

@@ -12,21 +12,32 @@
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 }}"
- name: Clone VM from template via API
ansible.builtin.uri:
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ vm_template_node }}/qemu/{{ vm_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 }}"
full: true
name: "{{ vm_hostname }}"
full: "1"
storage: "local-lvm"
timeout: 300
register: cloned_vm
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/{{ vm_template_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: 60
delay: 5
- name: Migrate VM to target node
ansible.builtin.uri:
@@ -57,28 +68,25 @@
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 }}"
ansible.builtin.uri:
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ 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 }}"
net:
net0: "virtio,bridge=vmbr0"
update: true
validate_certs: false
- 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
ansible.builtin.uri:
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/qemu/{{ 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: