---
- name: Check for Bigboot service state log presence
  ansible.builtin.stat:
    path: "{{ bigboot_disabled_services_log }}"
  register: bigboot_disabled_services_log_stat

- name: Read state log and restore service state
  when:
    - bigboot_disabled_services_log_stat['stat']['exists'] | bool
  block:
    - name: Read service state from log
      community.general.read_csv:
        path: "{{ bigboot_disabled_services_log }}"
        fieldnames: service,state,enabled
        delimiter: ','
      register: bigboot_service_state_contents

    - name: Restore service state
      ansible.builtin.service:
        name: "{{ item['service'] }}"
        state: "{{ item['state'] }}"
        enabled: "{{ item['enabled'] | bool }}"
      loop: "{{ bigboot_service_state_contents['list'] }}"
      loop_control:
        label: "{{ item['service'] }}"
