---

- set_fact:
    vault_install_mode: 'none'
    vault_status: {}
  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
     #- command: /usr/local/bin/vault status -format=json -tls-skip-verify
     #  changed_when: False
     #  register: vault_status
     #  failed_when: False # do not fail if vault is not running
    - set_fact:
        vault_current_version: "{{ vault_current_version.stdout }}"
     #   vault_status: "{{ (vault_status.rc == 0) | ternary(vault_status.stdout | from_json, {}) }}"
  tags: vault

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

- name: Check the state of the services
  service_facts:
  register: vault_sys_services
  tags: vault

