- delegate_to: localhost
  block:
  - name: Create archive
    command: "tar -cf safe-symlinks.tar {{ role_path }}/files/safe-symlinks"
    args:
      chdir: "{{ remote_tmp_dir }}"

  - name: Install role successfully
    command: ansible-galaxy role install --roles-path '{{ remote_tmp_dir }}/roles' safe-symlinks.tar
    args:
      chdir: "{{ remote_tmp_dir }}"

  - name: Validate each of the symlinks exists
    stat:
      path: "{{ remote_tmp_dir }}/roles/safe-symlinks.tar/{{ item }}"
    loop:
      - defaults/main.yml
      - handlers/utils.yml
    register: symlink_stat

  - assert:
      that:
        - symlink_stat.results[0].stat.exists
        - symlink_stat.results[0].stat.lnk_source == ((dest, 'roles/safe-symlinks.tar/defaults/common_vars/subdir/group0/main.yml') | path_join)
        - symlink_stat.results[1].stat.exists
        - symlink_stat.results[1].stat.lnk_source == ((dest, 'roles/safe-symlinks.tar/tasks/utils/suite.yml') | path_join)
    vars:
      dest: "{{ remote_tmp_dir | realpath }}"

  always:
  - name: Clean up
    file:
      path: "{{ item }}"
      state: absent
    delegate_to: localhost
    loop:
      - "{{ remote_tmp_dir }}/roles/"
      - "{{ remote_tmp_dir }}/safe-symlinks.tar"
