# SPDX-License-Identifier: MIT
---
- name: Ensure ansible_facts used by role
  setup:
    gather_subset: "{{ __mssql_required_facts_subsets }}"
  when: __mssql_required_facts |
    difference(ansible_facts.keys() | list) | length > 0

- name: Set platform/version specific variables
  include_vars: "{{ __mssql_vars_file }}"
  loop:
    - "{{ ansible_os_family }}.yml"
    - "{{ ansible_distribution }}.yml"
    - "{{ ansible_distribution }}_{{ ansible_distribution_major_version }}.yml"
    - "{{ ansible_distribution }}_{{ ansible_distribution_version }}.yml"
  vars:
    __mssql_vars_file: "{{ role_path }}/vars/{{ item }}"
  when: __mssql_vars_file is file

- name: Determine if system is booted with systemd
  when: __mssql_is_booted is not defined
  block:
    - name: Run systemctl
      # noqa command-instead-of-module
      command: systemctl is-system-running
      register: __is_system_running
      changed_when: false
      failed_when: false

    - name: Require installed systemd
      fail:
        msg: "Error: This role requires systemd to be installed."
      when: '"No such file or directory" in __is_system_running.msg | d("")'

    - name: Set flag to indicate that systemd runtime operations are available
      set_fact:
        # see https://www.man7.org/linux/man-pages/man1/systemctl.1.html#:~:text=is-system-running%20output
        __mssql_is_booted: "{{ __is_system_running.stdout != 'offline' }}"
