---
- name: Ensure facts, test variables used by role
  vars:
    __selinux_test_facts:
      - distribution
      - distribution_major_version
      - os_family
      - python_version
    __selinux_test_facts_regex: "{{ '^(' ~
      (__selinux_test_facts | join('|')) ~ ')$' }}"
    __selinux_test_facts_subsets: "{{ ['!all', '!min'] +
      __selinux_test_facts }}"
  block:
    - name: Ensure ansible_facts used by tests
      setup:
        gather_subset: "{{ __selinux_test_facts_subsets }}"
      when: __selinux_test_facts |
        difference(ansible_facts.keys() | list) | length > 0

    - name: Ensure SELinux testing packages
      include_role:
        name: linux-system-roles.selinux
        tasks_from: ensure_selinux_packages.yml

    - name: Ensure selinux-policy-targeted
      package:
        name: selinux-policy-targeted
        state: present
        use: "{{ (__selinux_is_ostree | d(false)) |
                 ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
      when: __selinux_need_policy_targeted | d(false)

    - name: Ensure findmnt
      package:
        name: "{{ findmnt_pkg }}"
        state: present
        use: "{{ (__selinux_is_ostree | d(false)) |
                 ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
      when: __selinux_need_findmnt | d(false)
      vars:
        findmnt_pkg: "{{ 'util-linux-core'
          if (ansible_facts['os_family'] == 'RedHat' and
              ansible_facts['distribution_major_version'] is version('8', '>'))
          else 'util-linux' if ansible_facts['os_family'] == 'RedHat'
          else 'util-linux' }}"

    - name: Set is_el6 flag
      set_fact:
        __selinux_is_el6: "{{ ansible_facts['os_family'] == 'RedHat' and
          ansible_facts['distribution_major_version'] is version('7', '<') }}"
      when: not __selinux_is_el6 is defined

    - name: Get test facts
      when: __selinux_get_test_facts | d(true)
      block:
        - name: Get local modifications - boolean
          command: /usr/sbin/semanage boolean -l -n -C
          changed_when: false
          register: selinux_role_boolean

        - name: Get local modifications - port
          command: /usr/sbin/semanage port -l -n -C
          changed_when: false
          register: selinux_role_port

        - name: Get local modifications - login
          command: /usr/sbin/semanage login -l -n -C
          changed_when: false
          register: selinux_role_login

        - name: Get local modifications - fcontext
          command: /usr/sbin/semanage fcontext -l -n -C
          changed_when: false
          register: selinux_role_fcontext
