Files
ansible/issues/005_add_performance_optimizations.md
Tuan-Dat Tran 5a8c7f0248 feat(proxmox): add hosts config
Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
2026-02-28 11:30:58 +01:00

3.2 KiB

Issue: Add Performance Optimizations

Status: Open Priority: Medium Component: proxmox/tasks Assignee: Junior Dev

Description

The Proxmox role could benefit from performance optimizations, particularly for image downloads and repeated operations.

Current Performance Issues

  • Sequential image downloads (no parallelization)
  • No caching of repeated operations
  • No async operations for long-running tasks
  • Inefficient fact gathering

Required Changes

Step 1: Add parallel downloads

Use async for image downloads to run concurrently.

Step 2: Implement caching

Add fact caching for repeated operations.

Step 3: Add conditional execution

Skip tasks when results are already present.

Implementation Steps

Example 1: Parallel Image Downloads

- name: Download Cloud Init Isos in parallel
  ansible.builtin.include_tasks: 42_download_isos.yaml
  loop: "{{ proxmox_cloud_init_images | dict | map(attribute='value') }}"
  loop_control:
    loop_var: distro
  async: 3600  # 1 hour timeout
  poll: 0
  register: download_tasks

- name: Check download status
  ansible.builtin.async_status:
    jid: "{{ item.ansible_job_id }}"
  register: download_results
  until: download_results.finished
  retries: 30
  delay: 10
  loop: "{{ download_tasks.results }}"
  loop_control:
    loop_var: item

Example 2: Add Fact Caching

# In ansible.cfg or playbook
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 86400

# In tasks
- name: Gather facts with caching
  ansible.builtin.setup:
    cacheable: yes

Example 3: Conditional Task Execution

- name: Check if image already exists
  ansible.builtin.stat:
    path: "{{ proxmox_dirs.isos }}/{{ distro.name }}"
  register: image_stat
  changed_when: false

- name: Download image only if missing
  ansible.builtin.get_url:
    url: "{{ distro.url }}"
    dest: "{{ proxmox_dirs.isos }}/{{ distro.name }}"
    mode: "0644"
  when: not image_stat.stat.exists
  register: download_result

- name: Skip conversion if raw image exists
  ansible.builtin.stat:
    path: "{{ proxmox_dirs.isos }}/{{ raw_image_name }}"
  register: raw_image_stat
  changed_when: false

- name: Convert to raw only if needed
  ansible.builtin.command:
    cmd: "qemu-img convert -O raw {{ proxmox_dirs.isos }}/{{ distro.name }} {{ proxmox_dirs.isos }}/{{ raw_image_name }}"
  when:
    - download_result is changed or not raw_image_stat.stat.exists
    - image_stat.stat.exists

Example 4: Batch VM Operations

- name: Create VMs in batches
  ansible.builtin.include_tasks: 55_create_vm.yaml
  loop: "{{ vms | batch(3) | flatten }}"
  loop_control:
    loop_var: "vm"
  throttle: 3

Testing Requirements

  • Measure performance before and after changes
  • Verify parallel operations don't cause conflicts
  • Test caching works correctly
  • Confirm conditional execution skips appropriately

Acceptance Criteria

  • Image downloads run in parallel
  • Fact caching implemented and working
  • Tasks skip when results already exist
  • Performance metrics show improvement
  • No race conditions in parallel operations
  • Documentation updated with performance notes