---
# Install and configure Incus + OVN for Veza V5 Ultra deployment (local testing)
# Single-host setup with OVN networking

- name: Install Incus and OVN (local testing)
  hosts: edge
  become: true
  gather_facts: true
  connection: local

  pre_tasks:
    - name: Update package cache
      apt:
        update_cache: true
        cache_valid_time: 3600

    - name: Install Incus via snap
      snap:
        name: incus
        state: present
        classic: true

    - name: Wait for snapd to be ready
      wait_for:
        timeout: 30
      delegate_to: localhost

  tasks:
    - name: Initialize Incus (standalone mode)
      command: incus init --auto
      register: incus_init_result
      failed_when: false

    - name: Display Incus init result
      debug:
        var: incus_init_result.stdout_lines
      when: incus_init_result.stdout_lines is defined

    - name: Create OVN network for Veza
      command: |
        incus network create veza-ovn \
          --type=ovn \
          --config network=veza-ovn \
          --config ipv4.address=10.10.0.1/24 \
          --config ipv4.nat=true \
          --config ipv6.address=fd42:veza::1/64 \
          --config ipv6.nat=true
      register: ovn_network_result
      failed_when: false

    - name: Display OVN network creation result
      debug:
        var: ovn_network_result.stdout_lines
      when: ovn_network_result.stdout_lines is defined

    - name: Verify Incus is running
      command: incus list
      register: incus_status
      failed_when: false

    - name: Display Incus status
      debug:
        var: incus_status.stdout_lines
      when: incus_status.stdout_lines is defined

    - name: Verify OVN network exists
      command: incus network list
      register: network_list
      failed_when: false

    - name: Display network list
      debug:
        var: network_list.stdout_lines
      when: network_list.stdout_lines is defined

  post_tasks:
    - name: Show Incus version
      command: incus version
      register: incus_version

    - name: Display Incus version
      debug:
        var: incus_version.stdout_lines
