- name: Calculate Settings
  set_fact:
    download_dir:  "{{ base_dir }}/downloads"
    download_url:  "https://examples.com/releases/v{{ app_version }}.tar.gz"
    artifact_file: "{{ base_dir }}/downloads/{{ app_version }}.tar.gz"
    latest_link:   "{{ base_dir }}/releases/latest"
    release_dir:   "{{ base_dir }}/releases/{{ app_version }}"
    binary_dir:    "/usr/local/bin"

- name: Create Directories
  file:
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ download_dir }}"
    - "{{ release_dir }}"

- name: Download the Artifact
  get_url:
    url: "{{ download_url }}"
    dest: "{{ artifact_file }}"

- name: Extract Artifact to Release Directory
  unarchive:
    copy: no
    src: "{{ artifact_file }}"
    dest: "{{ release_dir }}"

- name: Update the Link to the Latest Release
  file:
    src: "{{ release_dir }}"
    dest: "{{ latest_link }}"
    state: link

- name: Make the Link to the latest binaries
  file:
    src: "{{ latest_link }}/bin/{{ item }}"
    dest: "{{ binary_dir }}/{{ item }}"
    state: link
  with_items:
    - cmd1
    - cmd2