---
# file: roles/git_generic_deploy_files/tasks/main.yml

- name: "packages"
  apt:
    name: "unzip"
  tags: git_generic_deploy_files

- name: "set fact for ansible_running_user"
  set_fact:
    ansible_running_user: "{{ lookup('env', 'USER') }}"
  delegate_to: localhost
  check_mode: no
  tags: git_generic_deploy_files

- name: "[localhost] create the temporary directory /tmp/{{ ansible_running_user }}/ansible/git_generic_deploy_files/ansible_git_generic_deploy_files_archives/"
  become: no
  file:
    path: /tmp/{{ ansible_running_user }}/ansible/git_generic_deploy_files/ansible_git_generic_deploy_files_archives/
    state: directory
  delegate_to: localhost
  check_mode: no
  tags: git_generic_deploy_files

- name: "[localhost] git clone and create archives"
  become: no
  git:
    dest: "/tmp/{{ ansible_running_user }}/ansible/git_generic_deploy_files/ansible_git_generic_deploy_files_git/{{ item.repository_url | md5 }}_{{ item.branch | replace('/', '_') }}/"
    repo: "{{ item.repository_url }}"
    version: "{{ item.branch }}"
    archive: "/tmp/{{ ansible_running_user }}/ansible/git_generic_deploy_files/ansible_git_generic_deploy_files_archives/{{ item.repository_url | md5 }}_{{ item.branch | replace('/', '_') }}.zip"
    force: "{{ item.force | default(False) }}"
  with_items: "{{ git_generic_deploy_files_list }}"
  delegate_to: localhost
  check_mode: no
  tags: git_generic_deploy_files

- name: "www directories"
  file:
    path: "{{ item.deploy_directory }}"
    owner: "{{ item.deploy_directory_owner | default(omit) }}"
    group: "{{ item.deploy_directory_group | default(omit) }}"
    mode: "{{ item.deploy_directory_mode | default(omit) }}"
    state: directory
  with_items: "{{ git_generic_deploy_files_list }}"
  tags: git_generic_deploy_files

- name: "unarchive to destination"
  unarchive:
    src: "/tmp/{{ ansible_running_user }}/ansible/git_generic_deploy_files/ansible_git_generic_deploy_files_archives/{{ item.repository_url | md5 }}_{{ item.branch | replace('/', '_') }}.zip"
    dest: "{{ item.deploy_directory }}"
    owner: "{{ item.owner | default(omit) }}"
    group: "{{ item.group | default(omit) }}"
    mode: "{{ item.mode | default(omit) }}"
  with_items: "{{ git_generic_deploy_files_list }}"
  tags: git_generic_deploy_files

- name: "copy file(s)"
  copy:
    backup: "{{ item.backup | default(omit) }}"
    content: "{{ item.content | default(omit) }}"
    dest: "{{ item.dest | default(omit) }}"
    owner: "{{ item.owner | default(omit) }}"
    group: "{{ item.group | default(omit) }}"
    mode: "{{ item.mode | default(omit) }}"
    src: "{{ item.src | default(omit) }}"
  with_items: '{{ git_generic_deploy_copy }}'
  when: git_generic_deploy_copy is defined
  tags: git_generic_deploy_files
