---

# 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: consul,vault

- set_fact: consul_tpl_install_mode='none'
  tags: consul,vault

- name: Detect if consul_tpl is installed
  stat: path=/usr/local/bin/consul-template
  register: consul_tpl_bin
  tags: consul,vault

- when: not consul_tpl_bin.stat.exists
  set_fact: consul_tpl_install_mode='install'
  tags: consul,vault

- when: consul_tpl_bin.stat.exists
  block:
    - name: Detect installed version
      shell: /usr/local/bin/consul-template -version | perl -pe 's/consul\-template v(\d+(\.\d+)*).*/$1/'
      changed_when: False
      register: consul_tpl_current_version
    - set_fact: consul_tpl_current_version={{ consul_tpl_current_version.stdout }}
  tags: consul,vault

- when: consul_tpl_bin.stat.exists and consul_tpl_current_version != consul_tpl_version
  set_fact: consul_tpl_install_mode='upgrade'
  tags: consul,vault

