---

- name: Create the archive dir
  file: path={{ consul_root_dir }}/archives/{{ consul_current_version }} state=directory
  tags: consul

- name: Snapshot consul data
  command: "{{ consul_root_dir }}/bin/consul snapshot save {{ consul_root_dir }}/archives/{{ consul_current_version }}/consul.snap"
  args:
    creates: "{{ consul_root_dir }}/archives/{{ consul_current_version }}/consul.snap"
  when:
    - consul_conf.server
    - not consul_conf.acl.enabled or consul_mgm_token is defined
  environment:
    CONSUL_TOKEN: "{{ consul_mgm_token | default('') }}"
  tags: consul

- name: Backup previous version
  synchronize:
    src: "{{ consul_root_dir }}/{{ item }}"
    dest: "{{ consul_root_dir }}/archives/{{ consul_current_version }}/"
    compress: False
  delegate_to: "{{ inventory_hostname }}"
  loop:
    - bin
  tags: consul

