---

- block:

  - name: Ensure service facts have been gathered
    service_facts:
    tags: stop_zoox_system_services

  - name: Check for zcore and zsystem
    set_fact:
        services_present:
            zcore: "{{ 'zcore' in services }}"
            zsystem: "{{ 'zsystem' in services }}"
            zlog: True # unconditionally shut down zlog
    tags: stop_zoox_system_services

  - name: Make sure at least one of zsystem and zcore is installed
    fail:
      msg: "Could not detect zsystem or zcore service"
    when: not services_present['zcore'] and not services_present['zsystem']
    tags: stop_zoox_system_services

  # zlog depends on zcore, so stop zlog first and start it second.
  - name: Stop all zoox system services
    service:
      name: "{{ item }}"
      state: stopped
    with_items:
      - zlog
      - zcore
      - zsystem
    when: services_present[item]
    tags: stop_zoox_system_services

  - name: Start zoox system services
    service:
      name: "{{ item }}"
      state: started
    with_items:
      - zsystem
      - zlog
    tags: start_zoox_system_services

  become: yes
