From 678625e634f968e9f251c7ba7de39048332a221e Mon Sep 17 00:00:00 2001 From: jerick Date: Mon, 2 Feb 2026 15:17:08 -0500 Subject: [PATCH] Back to ansible collections --- README.md | 1 + requirements.yml | 2 + roles/proxmox_lxc/tasks/main.yml | 117 +++++++++--------------------- roles/proxmox_vm/tasks/main.yml | 119 +++++++++---------------------- 4 files changed, 72 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index 1d1ed77..d749798 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ proxmox-template-clone/ ```yaml - community.general >= 8.0.0 +- community.proxmox >= 0.1.0 - ansible.posix >= 1.5.0 ``` diff --git a/requirements.yml b/requirements.yml index 3834074..751839b 100644 --- a/requirements.yml +++ b/requirements.yml @@ -2,5 +2,7 @@ collections: - name: community.general version: ">=8.0.0" + - name: community.proxmox + version: ">=0.1.0" - name: ansible.posix version: ">=1.5.0" diff --git a/roles/proxmox_lxc/tasks/main.yml b/roles/proxmox_lxc/tasks/main.yml index 8b766b9..f10188c 100644 --- a/roles/proxmox_lxc/tasks/main.yml +++ b/roles/proxmox_lxc/tasks/main.yml @@ -1,92 +1,43 @@ --- -- 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: 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 }}" + hostname: "{{ vm_hostname }}" + full: true + storage: "local-lvm" + target: "{{ target_node }}" + timeout: 600 + register: cloned_lxc - 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 }}" + 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: "{{ cloned_lxc.vmid }}" + cores: "{{ cpu_cores }}" + memory: "{{ ram_gb | int * 1024 }}" + netif: net0: "name=eth0,bridge=vmbr0,ip=dhcp" - validate_certs: false + state: present - 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 + 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: "{{ cloned_lxc.vmid }}" + state: started - name: Set VMID fact for later use ansible.builtin.set_fact: - created_vmid: "{{ next_vmid }}" + created_vmid: "{{ cloned_lxc.vmid }}" diff --git a/roles/proxmox_vm/tasks/main.yml b/roles/proxmox_vm/tasks/main.yml index 751342c..5799333 100644 --- a/roles/proxmox_vm/tasks/main.yml +++ b/roles/proxmox_vm/tasks/main.yml @@ -1,93 +1,44 @@ --- -- 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 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 }}" - name: "{{ 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/{{ 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: - 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: 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 }}" + full: true + storage: "local-lvm" + target: "{{ target_node }}" + timeout: 600 + register: cloned_vm - name: Configure VM resources - 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 }}" + 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: "{{ cloned_vm.vmid }}" + cores: "{{ cpu_cores }}" + memory: "{{ ram_gb | int * 1024 }}" + net: net0: "virtio,bridge=vmbr0" - validate_certs: false + update: true - name: Start the VM - 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 + 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: "{{ cloned_vm.vmid }}" + state: started - name: Set VMID fact for later use ansible.builtin.set_fact: - created_vmid: "{{ next_vmid }}" + created_vmid: "{{ cloned_vm.vmid }}"