- name: Cleanup all NGINX cache directories
  become: true
  ansible.builtin.file:
    path: "{{ item.value }}"
    state: absent
  when:
    - mode_cleanup | bool
    - run_once_nginx_reverse_proxy is not defined
  loop: "{{ nginx.directories.cache | dict2items }}"
  loop_control:
    label: "{{ item.key }}"

- name: Ensure all NGINX cache directories exist
  become: true
  ansible.builtin.file:
    path: "{{ item.value }}"
    state: directory
    owner: "{{ nginx.user }}"
    group: "{{ nginx.user }}"
    mode: '0700'
      
  when: run_once_nginx_reverse_proxy is not defined
  loop: "{{ nginx.directories.cache | dict2items }}"
  loop_control:
    label: "{{ item.key }}"

- name: run the nginx_reverse_proxy tasks once
  set_fact:
    run_once_nginx_reverse_proxy: true
  when: run_once_nginx_reverse_proxy is not defined