---
- name: Bootstrap | Check if bootstrap is needed
  raw: stat /opt/bin/.bootstrapped
  register: need_bootstrap
  failed_when: false
  tags: facts

- name: Bootstrap | Run bootstrap.sh
  script: bootstrap.sh
  when: (need_bootstrap | failed)

- set_fact:
    ansible_python_interpreter: "/opt/bin/python"
  tags: facts

- name: Bootstrap | Check if we need to install pip
  shell: "{{ansible_python_interpreter}} -m pip --version"
  register: need_pip
  failed_when: false
  changed_when: false
  check_mode: no
  when: (need_bootstrap | failed)
  tags: facts

- name: Bootstrap | Copy get-pip.py
  copy:
    src: get-pip.py
    dest: ~/get-pip.py
  when: (need_pip | failed)

- name: Bootstrap | Install pip
  shell: "{{ansible_python_interpreter}} ~/get-pip.py"
  when: (need_pip | failed)

- name: Bootstrap | Remove get-pip.py
  file:
    path: ~/get-pip.py
    state: absent
  when: (need_pip | failed)

- name: Bootstrap | Install pip launcher
  copy:
    src: runner
    dest: /opt/bin/pip
    mode: 0755
  when: (need_pip | failed)

- name: Install required python modules
  pip:
    name: "{{ item }}"
  with_items: "{{pip_python_modules}}"
