---

- set_fact: consul_install_mode='none'
  tags: consul

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

- when: not consul_bin.stat.exists
  set_fact: consul_install_mode='install'
  tags: consul

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

- when: consul_bin.stat.exists and consul_current_version != consul_version
  set_fact: consul_install_mode='upgrade'
  tags: consul

