---
# tasks file for packer

- name: Check if packer is installed
  stat:
    path: "{{ packer_binary_install_fullpath }}"
  register: packer_binary_stat

- name: Packer
  block:
    - name: "Create download directory {{ download_directory }}"
      file:
        path: "{{ download_directory }}"
        state: directory

    - name: "Download Packer {{ packer_version }}"
      get_url:
        url: "{{ packer_download_url }}"
        checksum: "sha256:{{ packer_zip_sha256_checksum }}"
        dest: "{{ download_directory }}"
      register: download_packer
      retries: 3
      until: download_packer is succeeded

    - name: Unzip Packer {{ packer_version }}
      unarchive:
        src: "{{ download_directory }}/{{ packer_zip_filename }}"
        dest: "{{ download_directory }}"
        remote_src: yes
      when: download_packer is succeeded

    - name: Install Packer {{ packer_version }}
      copy:
        src: "{{ download_directory }}/packer"
        dest: "{{ packer_binary_install_fullpath }}"
        mode: "0755"
      when: download_packer is succeeded
      become: yes
  when: packer_binary_stat.stat.exists == False


