---

- name: Create the archive dir
  file: path={{ nomad_root_dir }}/archives/{{ nomad_current_version }} state=directory
  tags: nomad

- name: Snapshot nomad data
  command: "{{ nomad_root_dir }}/bin/nomad operator snapshot save {{ nomad_root_dir }}/archives/{{ nomad_current_version }}/nomad.snap"
  args:
    creates: "{{ nomad_root_dir }}/archives/{{ nomad_current_version }}/nomad.snap"
  when:
    - nomad_conf.server.enabled
    - nomad_sys_services.ansible_facts.services['nomad.service'] is defined
    - nomad_sys_services.ansible_facts.services['nomad.service'].state == 'started'
  environment:
    NOMAD_TOKEN: "{{ nomad_mgm_token | default(omit) }}"
  tags: nomad

- name: Backup previous version
  synchronize:
    src: "{{ nomad_root_dir }}/{{ item }}"
    dest: "{{ nomad_root_dir }}/archives/{{ nomad_current_version }}/"
    compress: False
  delegate_to: "{{ inventory_hostname }}"
  loop:
    - bin
    - plugins
  tags: nomad