---

- name: Install needed tools
  package:
    name: "{{ consul_packages }}"
  tags: consul

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

- when: consul_link.stat.islnk is defined and consul_link.stat.islnk
  block:
    - name: Remove consul link
      file: path=/usr/local/bin/consul state=absent
    - set_fact: consul_install_mode='upgrade'
  tags: vault

- when: consul_install_mode != 'none'
  block:
    - name: Download consul
      get_url:
        url: "{{ consul_archive_url }}"
        dest: /tmp
        checksum: sha256:{{ consul_archive_sha256 }}

    - name: Extract the archive
      unarchive:
        src: "/tmp/consul_{{ consul_version }}_linux_amd64.zip"
        dest: /usr/local/bin
        include: consul
        mode: 755
        remote_src: True

  tags: consul

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

