Files
proxmox-template-clone/roles/proxmox_lxc/tasks/main.yml

87 lines
3.0 KiB
YAML

---
- name: Get available storage on target node (filtered for rootdir content)
ansible.builtin.uri:
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/storage?content=rootdir&enabled=1"
method: GET
headers:
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
validate_certs: false
register: storage_info
- name: Debug available storage
ansible.builtin.debug:
msg: "Available storage on {{ target_node }}: {{ storage_info.json.data | map(attribute='storage') | list }}"
- name: Set storage backend (prefer local-zfs if available)
ansible.builtin.set_fact:
target_storage: "{{ 'local-zfs' if storage_info.json.data | selectattr('storage', 'equalto', 'local-zfs') | list | length > 0 else 'local-lvm' }}"
- name: Display selected storage
ansible.builtin.debug:
msg: "Target node {{ target_node }} will use storage: {{ target_storage }}"
- name: Get list of LXC containers on target node
ansible.builtin.uri:
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ target_node }}/lxc"
method: GET
headers:
Authorization: "PVEAPIToken={{ proxmox_api_user }}!{{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
validate_certs: false
register: lxc_list
- name: Find template VMID by name
ansible.builtin.set_fact:
lxc_template_id: "{{ (lxc_list.json.data | selectattr('name', 'equalto', lxc_template_name) | first).vmid }}"
- name: Display found template
ansible.builtin.debug:
msg: "Found template '{{ lxc_template_name }}' with VMID {{ lxc_template_id }} on {{ target_node }}"
- name: Clone LXC from template on target node
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 }}"
clone: "{{ lxc_template_id }}"
hostname: "{{ vm_hostname }}"
clone_type: "full"
storage: "{{ target_storage }}"
timeout: 600
register: cloned_lxc
- name: Wait for LXC to unlock
ansible.builtin.wait_for:
timeout: 30
- 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: "{{ cloned_lxc.vmid }}"
hostname: "{{ vm_hostname }}"
cores: "{{ cpu_cores }}"
memory: "{{ ram_gb | int * 1024 }}"
netif:
net0: "name=eth0,bridge=vmbr0,ip=dhcp"
state: present
update: true
- 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: "{{ cloned_lxc.vmid }}"
state: started
- name: Set VMID fact for later use
ansible.builtin.set_fact:
created_vmid: "{{ cloned_lxc.vmid }}"