---
- name: Create work_dir
  tags: ["always"]
  command: |
    if [ ! -d "{{ .work_dir }}" ]; then
      mkdir -p {{ .work_dir }}
    fi

- name: Extract artifact to work_dir
  tags: ["always"]
  command: |
    if [ -f "{{ .artifact_file }}" ]; then
      mkdir -p {{ .work_dir }}/kubekey
      tar -zxvf {{ .artifact_file }} -C {{ .work_dir }}/kubekey
    fi
  when: and .artifact_file (ne .artifact_file "")

- name: Download binaries
  block:
    # the binaries which download by curl
    - include_tasks: download_by_curl.yaml
    # the binaries which download by helm
    - include_tasks: download_by_helm.yaml
    # download remote images to local
    - name: Download images
      image:
        pull:
          manifests: "{{ .image_manifests | toJson }}"
      when: .image_manifests | default list | len | lt 0

- include_tasks: pki.yaml
  tags: ["certs"]

- name: Chown work_dir to sudo
  tags: ["always"]
  command: |
    chown -R ${SUDO_UID}:${SUDO_GID} {{ .work_dir }}
