- name: Remover o diretório /tmp/stack-arr/bazarr/kubernetes-files
  ansible.builtin.file:
    path: /tmp/stack-arr/bazarr/kubernetes-files
    state: absent

- name: Criar diretório temporário no remoto
  file:
    path: /tmp/stack-arr/bazarr/kubernetes-files
    state: directory
    mode: '0755'

- name: Copy file with owner and permissions
  ansible.builtin.copy:
    src: ../files
    dest: /tmp/stack-arr/bazarr/kubernetes-files
    owner: fenix
    group: root
    mode: '0644'


- name: Obter várias notas do Bitwarden
  shell: |
    echo "unlock"
    BW_SESSION=$(bw unlock {{ bw_password }} --raw)
    echo "get item"
    bw get item "{{ item.id }}" --session $BW_SESSION | jq -r '.notes' > {{ item.dest }}
  loop:
    - { id: "iac.ansible.stackarr.bazarr.secret", dest: "/tmp/stack-arr/bazarr/kubernetes-files/files/bazarr-secret.yaml" }
  args:
    executable: /bin/bash
  environment:
    BW_PASSWORD: "{{ BW_PASSWORD }}"


- name: Listar conteúdo do diretório remoto
  shell: ls -l /tmp/stack-arr/bazarr/kubernetes-files/files
  register: resultado_ls


- name: Mostrar resultado do ls
  debug:
    var: resultado_ls.stdout_lines


- name: Aplicar o stolon
  become: yes
  become_user: fenix 
  shell: |
    kubectl apply -f /tmp/stack-arr/bazarr/kubernetes-files/files/
  environment:
    KUBECONFIG:  /home/fenix/.kube/config