---
- name: "STRESS-SETUP: Create .env file"
  hosts: all
  gather_facts: no
  vars_files:
    - "group_vars/all/generated_vars.stress.yml"
    - "group_vars/all/vault.yml"
  tasks:
    - name: Define base directory for node
      ansible.builtin.set_fact:
        base_dir: "{{ airflow_master_dir if inventory_hostname in groups['airflow_master'] else airflow_worker_dir }}"

    - name: Create .env file for stress test environment
      ansible.builtin.template:
        src: templates/.env.stress.j2
        dest: "{{ base_dir }}/.env"
        owner: "{{ ansible_user }}"
        group: "{{ deploy_group }}"
        mode: '0644'
      become: yes

    - name: Ensure REDIS_PORT is set in .env file
      ansible.builtin.lineinfile:
        path: "{{ base_dir }}/.env"
        line: "REDIS_PORT={{ redis_port }}"
        regexp: "^REDIS_PORT="
        state: present
        create: yes
        owner: "{{ ansible_user }}"
        group: "{{ deploy_group }}"
        mode: '0644'
      become: yes
