---
- name: Check if Helm binary exists.
  stat:
    path: "{{ helm_bin_path }}"
  register: helm_check

- name: Check Helm version.
  command: "{{ helm_bin_path }} version"
  failed_when: false
  changed_when: false
  register: helm_existing_version

- name: Download helm.
  unarchive:
    src: https://get.helm.sh/helm-{{ helm_version }}-{{ helm_platform }}-{{ helm_arch }}.tar.gz
    dest: /tmp
    remote_src: true
  register: helm_download
  when: >
    not helm_check.stat.exists
    or helm_version not in helm_existing_version.stdout

- name: Copy helm binary into place.
  copy:
    src: "/tmp/{{ helm_platform }}-{{ helm_arch }}/helm"
    dest: "{{ helm_bin_path }}"
    mode: 0755
    remote_src: true
  become: true
  when: helm_download is changed
