---

# Load distribution specific variables
- include_vars: "{{ item }}"
  with_first_found:
    - "{{ role_path }}/vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ role_path }}/vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ role_path }}/vars/{{ ansible_distribution }}.yml"
    - "{{ role_path }}/vars/{{ ansible_os_family }}.yml"
  tags: vault

- set_fact:
    vault_install_mode: 'none'
  tags: vault

- name: Detect if vault is installed
  stat: path=/usr/local/bin/vault
  register: vault_bin
  tags: vault

- when: not vault_bin.stat.exists
  set_fact: vault_install_mode='install'
  tags: vault

- when: vault_bin.stat.exists
  block:
    - name: Detect installed version
      shell: /usr/local/bin/vault version | perl -pe 's/Vault v(\d+(\.\d+)*)\s.*/$1/'
      changed_when: False
      register: vault_current_version
    - set_fact:
        vault_current_version: "{{ vault_current_version.stdout }}"
  tags: vault

- when: vault_bin.stat.exists and vault_current_version != vault_version
  set_fact: vault_install_mode='upgrade'
  tags: vault

