---
# SPDX-License-Identifier: GPL-3.0-only

- name: Ensure ansible_facts used by role
  setup:
    gather_subset: "{{ __logging_required_facts_subsets }}"
  when: __logging_required_facts |
    difference(ansible_facts.keys() | list) | length > 0

- name: Determine if system is ostree and set flag
  when: not __logging_is_ostree is defined
  block:
    - name: Check if system is ostree
      stat:
        path: /run/ostree-booted
      register: __ostree_booted_stat

    - name: Set flag to indicate system is ostree
      set_fact:
        __logging_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

- name: Set platform/version specific variables
  include_vars: "{{ item }}"
  loop:
    - "{{ role_path }}/vars/default.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['os_family'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}_\
      {{ ansible_facts['distribution_major_version'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}_\
      {{ ansible_facts['distribution_version'] }}.yml"
  when: item is file

- name: "Include /vars/main.yml from {{ role_path }}"
  include_vars: "{{ item }}"
  loop: "{{ lookup('lines',
         '/bin/ls -1 {{ role_path }}/vars/main.yml
          {{ role_path }}/vars/*/*/main.yml').split(',') }}"
  when: __snapshot_gather_vars is defined and
        __snapshot_gather_vars | bool
