---
# Variables affecting this role:
#
#  tools:
#    - final_name: the path to the tool to check for
#      completion: look for bash_completion or no
#      completion_file: the name of the bash completion file
#  clusters:
#   just a simple list of clusters to check
#
- name: Check for tools
  file:
    path: "{{ tools[item].final_name }}"
    state: file
  loop: "{{ tools.keys() | list }}"

- name: Check for completion files
  file:
    path: /etc/bash_completion.d/{{ tools[item].completion_file }}
    state: file
  when: tools[item].completion
  loop: "{{ tools.keys() | list }}"

- name: Make sure kubeconfig is there on utility
  delegate_to: utility.lab.example.com
  file:
    path: /home/lab/{{ item }}/auth/kubeconfig
    state: file
  loop: "{{ clusters }}"

- name: Create a temp dir
  file:
    path: tmp
    state: directory

- name: Copy over the kubeconfig
  delegate_to: utility.lab.example.com
  fetch:
    src: /home/lab/{{ item }}/auth/kubeconfig
    dest: tmp/kubeconfig-{{ item }}
    flat: yes
  loop: "{{ clusters }}"

- name: "We need python-kubernetes >= 12"
  become: yes
  pip:
    name: kubernetes>=12.0.0

# XXX This won't do if you install it using pip.
- name: We also need python38-jmespath
  become: yes
  package:
    name: python38-jmespath
    state: latest

- name: We need java-17-openjdk-headless, too
  become: yes
  package:
    name: java-17-openjdk-headless
    state: latest

- name: Get download confirmation code from Google Drive
  uri:
    url: "https://drive.google.com/uc?export=download&id={{ amq_file_id }}"
    creates: "{{ ansible_facts['user_dir'] }}/Downloads/amq-broker-{{ amq_z }}-bin.zip"
    return_content: yes
    status_code: 200
  register: amq_dl_cc

- name: Store conf code as a fact
  set_fact:
    amq_src_url: "{{ amq_dl_cc.content | regex_replace('^.*action=\"', '') | regex_replace('\".*$', '') | regex_replace('amp;', '') }}"
  when: amq_dl_cc.content is defined

- name: Download AMQ broker
  get_url:
    url: "{{ amq_src_url }}"
    dest: "{{ ansible_facts['user_dir'] }}/Downloads/amq-broker-{{ amq_z }}-bin.zip"
    mode: 0664
  when: amq_src_url is defined

- name: Extract AMQ broker to /opt
  become: yes
  unarchive:
    src: "{{ ansible_facts['user_dir'] }}/Downloads/amq-broker-{{ amq_z }}-bin.zip"
    remote_src: yes
    creates: /opt/amq
    dest: /opt
  register: amq_ex_act

- name: Rename wherever the archive extracted into /opt/amq
  become: yes
  command: mv /opt/{{ amq_extracted_dir }} /opt/amq
  when:
    - amq_ex_act is defined
    - amq_ex_act.changed

- name: Make sure /opt/amq/bin is in PATH
  lineinfile:
    path: "{{ ansible_facts['user_dir'] }}/.bashrc"
    line: 'PATH="${PATH}:/opt/amq/bin"'
    regexp: '^PATH=.*/opt/amq/bin'
    insertafter: "^# User specific environment$"
    state: present

- name: Verify cluster connectivity
  kubernetes.core.k8s_cluster_info:
    kubeconfig: tmp/kubeconfig-{{ item }}
    validate_certs: no
  loop: "{{ clusters }}"
...