---

# The following variables will be set by the runner of this playbook:
# src: /tmp/some/path/private_data_dir/

- name: Poll for status of active job.
  hosts: all
  gather_facts: false

  tasks:

    - name: Determine if daemon process is alive.
      shell: "awx-expect is-alive {{src}}"
      register: is_alive
      ignore_errors: true

    - name: Copy artifacts from the isolated host.
      synchronize:
        src: "{{src}}/artifacts/"
        dest: "{{src}}/artifacts/"
        mode: pull
        recursive: yes

    - name: Fail if previous check determined that process is not alive.
      fail:
        msg: "isolated task is still running"
      when: "is_alive.rc == 0"

    - stat: path="{{src}}/facts/"
      register: fact_cache

    - name: Copy gathered facts from the isolated host.
      synchronize:
        src: "{{src}}/facts/"
        dest: "{{src}}/facts/"
        delete: yes  # delete fact cache records that go missing via clear_facts
        mode: pull
      when: fact_cache.stat.exists
