---
- name: Copy instance config-file
  copy:
    content: "{{ item.config }}"
    dest: "{{ louketo_config_path }}/{{ item.name }}.yaml"
  when: item.state != "absent"
  register: copy_config

- name: Remove instance config-file
  file:
    path: "{{ louketo_config_path }}/{{ item.name }}.yaml"
    state: absent
  when: item.state == "absent"

- name: Enable & start instance
  service:
    name: "louketo@{{ item.name }}"
    state: started
    enabled: yes
  when: item.state != "absent"

- name: Disable & stop instance
  service:
    name: "louketo@{{ item.name }}"
    state: stopped
    enabled: no
  when: item.state == "absent"

- name: Restart instance
  service:
    name: "louketo@{{ item.name }}"
    state: restarted
  when:
    - ((not stat_version_file.stat.exists) or (slurp_version_file.content | b64decode != louketo_version) or (copy_config is changed)) and item.state != "absent"