---

- name: Install needed tools
  package:
    name: "{{ vault_packages }}"
  tags: vault

# Migrate from the old vault role
- name: Check if vault is a link
  stat: path=/usr/local/bin/vault
  register: vault_link
  tags: vault

- when: vault_link.stat.islnk is defined and vault_link.stat.islnk
  block:

    - name: Remove vault link
      file: path=/usr/local/bin/vault state=absent

    - set_fact: vault_install_mode='upgrade'

  tags: vault

- when: vault_install_mode != 'none'
  block:
    - name: Download vault
      get_url:
        url: "{{ vault_archive_url }}"
        dest: /tmp
        checksum: sha256:{{ vault_archive_sha256 }}

    - name: Extract the archive
      unarchive:
        src: /tmp/vault_{{ vault_version }}_linux_amd64.zip
        dest: /usr/local/bin
        include: vault
        remote_src: True
        mode: 755

    - name: Remove ZIP archive
      file: path=/tmp/vault_{{ vault_version }}_linux_amd64.zip state=absent

  tags: vault

- name: Install bash completion support
  copy:
    content: |
      complete -C /usr/local/bin/vault vault
    dest: /etc/bash_completion.d/vault
    mode: 0644
  tags: vault

