---

# The following variables will be set by the runner of this playbook:
# cleanup_dirs: ['/tmp/path/private_data_dir/', '/tmp//path/proot_temp_dir/']
# private_data_dir: '/tmp/path/private_data_dir/'

- name: Clean up from isolated job run.
  hosts: all
  gather_facts: false

  tasks:

    - name: cancel the job
      command: "ansible-runner stop {{private_data_dir}}"
      ignore_errors: true

    - name: remove build artifacts
      file:
        path: '{{item}}'
        state: absent
      register: result
      with_items: "{{cleanup_dirs}}"
      until: result is succeeded
      ignore_errors: true
      retries: 3
      delay: 5

    - name: fail if build artifacts were not cleaned
      fail:
        msg: 'Unable to cleanup build artifacts'
      when: not result is succeeded
