---
- name: Deploy Airflow DL Worker Stack
  hosts: airflow_workers
  vars_files:
    - group_vars/all.yml
    - group_vars/all/vault.yml
  tasks:

    - name: Ensure worker directory exists
      file:
        path: "{{ airflow_worker_dir }}"
        state: directory
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"

    - name: Template .env.worker
      template:
        src: templates/.env.worker.j2
        dest: "{{ airflow_worker_dir }}/.env"
        mode: '0600'

    - name: Copy docker-compose-dl.yaml
      copy:
        src: airflow/docker-compose-dl.yaml
        dest: "{{ airflow_worker_dir }}/docker-compose.yaml"
        remote_src: yes

    - name: Symlink compose file
      file:
        src: "{{ airflow_worker_dir }}/docker-compose.yaml"
        dest: "{{ airflow_worker_dir }}/docker-compose-dl.yaml"
        state: link

    - name: Generate dynamic configs (camoufox + envoy)
      community.docker.docker_compose:
        project_src: "{{ airflow_worker_dir }}"
        files:
          - docker-compose.config-generate.yaml
        services:
          - config-generator
        state: present

    - name: Pull latest images
      community.docker.docker_compose:
        project_src: "{{ airflow_worker_dir }}"
        files:
          - docker-compose.yaml
        pull: yes

    - name: Start worker services
      community.docker.docker_compose:
        project_src: "{{ airflow_worker_dir }}"
        files:
          - docker-compose.yaml
        state: present
